2012-05-16 3 views
1

QTextStream에 텍스트 파일을로드하면 어떻게 데이터로 TableWidget을 채울 수 있습니까? 텍스트 파일은 탭으로 구분됩니다.Qt의 테이블 위젯에 텍스트 파일로드

#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 
+0

'process_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 } 

내가 가진 텍스트 파일의 샘플 일하고 있어요된다 : 여기에 지금까지 가지고 무엇인가? 텍스트 파일의 모든 개별 QString을 어디에 저장하고 있습니까? – cmannett85

답변

3
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])); 
    } 
} 
+2

split에 "skipEmptyParts"를 전달해야하는지 잘 모르겠습니다. 탭으로 구분되어 있으므로 빈 열은 건너 뛰지 않고 비워 두어야합니다. –

+0

텍스트 항목을 반복하여 표에 삽입해야하는 부분에 어려움을 겪고 있습니다. 데이터를 삽입하기 전에 setRowCount 및 setColumnCount를 입력해야합니다. 파싱 ​​할 전체 텍스트 파일에 대해이 작업을 수행하는 올바른 방법은 무엇입니까? –

+0

먼저 테이블의 크기를 확인하려면 파일을 구문 분석해야합니다. – cmannett85

관련 문제