将文本文件加载到Qt中的表小部件中
如果我将文本文件加载到QTextStream中,那么我将如何用它的数据填充TableWidget?文本文件以制表符分隔。以下是我到目前为止的情况:将文本文件加载到Qt中的表小部件中,qt,parsing,user-interface,text,stream,Qt,Parsing,User Interface,Text,Stream,如果我将文本文件加载到QTextStream中,那么我将如何用它的数据填充TableWidget?文本文件以制表符分隔。以下是我到目前为止的情况: void MainWindow::startParsing() { QStringList stringList; int countRows; QTextStream in(&textFile); QString line;
void MainWindow::startParsing()
{
QStringList stringList;
int countRows;
QTextStream in(&textFile);
QString line;
if (textFile.open(QIODevice::ReadOnly))
{
do
{
line = in.readLine();
stringList << line.split("\t", QString::SkipEmptyParts);
}
while (!in.atEnd());
}
QSet<QString> set = stringList.toSet();
foreach (const QString &value, set)
qDebug() << value;
countRows = stringList.count();
//--- define the table's shape ---
ui->tableWidget_inputPreview->setRowCount(countRows);
ui->tableWidget_inputPreview->setColumnCount(6);
//--- create the horizontal (column) headers ---
QStringList horzHeaders;
horzHeaders << "HostName" << "Host IP" << "Area" << "Host Interface"
<< "Router to Ext Network" << "Ext Routes";
ui->tableWidget_inputPreview->setHorizontalHeaderLabels( horzHeaders );
//--- create the vertical (row) headers ---
QStringList vertHeaders;
ui->tableWidget_inputPreview->setVerticalHeaderLabels( vertHeaders );
//--- populate the table widget with data from txt file ---
// TODO: insert data into table
}
QTextStream在(&file)中;
QList列表;
QString线;
做{
line=in.readLine();
列出了process\u line(line)
do做什么?您将文本文件中的所有单个qstring存储在哪里?我不确定“skipmptyparts”应该传递给split。由于它是tab delimed,那么空列应该是空的,而不是skippedI。我正在努力处理我需要循环文本条目以将它们插入表中的部分。在插入任何数据之前,似乎必须输入setRowCount和setColumnCount。正确的方法是什么e我正在解析的整个文本文件?您需要首先解析该文件,以查看表需要有多大。我尝试使用您示例中tableWidget的setItem函数,但没有向表中插入任何内容。我更新了代码示例,现在我知道正在读取什么了(虽然我假设你想让它像原始文件一样排列,如果我错了,请纠正我。)@KamilKlimek是绝对正确的。
#TEST 1
#HostName HostIP Area Host Interface Router to Ext NW Number of Ext Routes
test1 9.1.1.1 0.0.0.0
OMG_LOL_101 128.12.101.2 0.0.0.0
OMG_LOL_102 128.12.102.9 0.0.0.0 128.112.102.9 128.112.102.10 100
WTF_BBQ_149 128.20.180.2 0.0.0.0
#HITL 2
#HostName HostIP Area Host Interface Router to Ext NW Number of Ext Routes
test2 9.1.1.2 0.0.0.0
WTF_BBQ_111 128.15.110.2 0.0.0.0
WTF_BBQ_112 128.15.111.2 0.0.0.0
WTF_BBQ_113 128.15.112.2 0.0.0.0 128.115.112.9 128.115.112.10 100
QTextStream in(&file);
QList< QStringList > lists;
QString line;
do {
line = in.readLine();
lists << line.split("\t");
} while (!line.isNull())
// Set the table size (assuming the rows are all the same length).
tableWidget.setRowCount( lists.size() );
tableWidget.setColumnCount( lists[0].size() );
for ( int row = 0; row < lists.size(); ++row ) {
for ( int column = 0; column < lists[row].size(); ++column ) {
tableWidget.setItem(row, column, new QTableWidgetItem(lists[row][column]));
}
}