简体   繁体   English

在qtablewidget中加载CSV(为什么我的代码无法正常工作?)

[英]loading csv in qtablewidget (Why my code is not working?)

I am self-learner and new to qt(I just want to learn to write program). 我是自学者,是qt的新手(我只想学习编写程序)。 Trying to load the csv file to qtablewidget but it overriding all columns and row with same data. 尝试将csv文件加载到qtablewidget,但它使用相同的数据覆盖所有列和行。 I tried to follow How to import a CSV file to a QTableWidget but i did not get it correctly. 我尝试遵循如何将CSV文件导入QTableWidget,但是我没有正确获取它。

Code: 码:

QFile file("testData.txt");
QTextStream in(&file);
QStringList setDataInrow;
QStringList rowNumbers;
QString allLine;

if(file.open(QIODevice::ReadOnly)){
    allLine=in.readAll();
    rowNumbers=allLine.split("\n");
    file.close();

}

QTableWidget *myTable=new QTableWidget();
myTable->setRowCount(rowNumbers.size());


for(int row=0;row<rowNumbers.count();row++)
{
    setDataInrow=allLine.split(";");


    for(int column=0;column<setDataInrow.count();column++){
        myTable->setColumnCount(setDataInrow.size());
        //myTable->item(row,column)->setText(setDataInrow[column]);
        QTableWidgetItem *item=new QTableWidgetItem(setDataInrow[column]);
        myTable->setItem(row,column,item);


    }
}


qDebug()<<"Numbers of row needed:"<<"\n"<<rowNumbers<<"\n";
qDebug()<<"Set following data to each column as per row:"<<"\n"<<setDataInrow<<setDataInrow.size();

window->setCentralWidget(myTable);
window->show();
return app.exec();

} }

i am trying to load : 我正在尝试加载:

John Kl;34;1335532;CA;0444344
Kuma jo;54;44432;NY;0322355
Lebal ho;24;44022;NY;0110004 

It should be loaded within 3 rows and 5 columns but it is setting 13 columns . 它应该在3行和5列内加载,但要设置13列。 Perhaps i am not able think this correctly. 也许我不能正确地认为这一点。 I need your help with some code example so that i can study about it more. 我需要一些代码示例的帮助,以便我可以对其进行更多研究。

Since my English is not good(Sucks like my codes :)) I have taken an screenshot of the current program which is not working as expected: 由于我的英语不好(像我的代码一样糟糕:))我为当前程序拍摄了一个屏幕截图,该屏幕截图无法正常工作:

http://imageshack.us/a/img801/1601/le59.png http://imageshack.us/a/img801/1601/le59.png

setDataInrow=allLine.split(";");

You do this once for each line. 您每行只需要执行一次。 But you don't split the line, as you intended, but each time the whole file content. 但是,您并没有按照预期的方式分割线,而是每次都分割整个文件内容。

Edit: Don't split allLine. 编辑:不要分割allLine。 You have already a QStringList, which contains your lines: rowNumbers. 您已经有一个QStringList,其中包含您的行:rowNumbers。

This you must split. 这必须拆分。

for(int row=0;row<rowNumbers.count();row++){
  QStringList rowCells = rowNumbers.at(row).split(";");
  .....
}

I hope this gives you the idea. 我希望这能给你这个主意。 No guarantee for details. 不保证细节。

Edit 2: 编辑2:

When you do 当你做

setDataInrow=allLine.split(";");

your stringlist contains: 您的字符串列表包含:

John Kl 34 1335532 CA 0444344 Kuma jo 54 44432 NY 0322355 Lebal ho 24 44022 NY 0110004. John Kl 34 1335532 CA 0444344 Kuma jo 54 44432 NY 0322355 Lebal ho 24 44022 NY 0110004。

This is the whole text in your file. 这是文件中的全文。 The line breaks do not matter. 换行并不重要。 They are just another character. 他们只是另一个角色。 You add all this in one row. 您将所有这些添加到一行中。 And you do it three times. 然后你做三遍。

What you want is first split the text into lines. 您要先将文本分成几行。 You already do this here: rowNumbers=allLine.split("\\n"); 您已经在这里执行了以下操作: rowNumbers=allLine.split("\\n");

With your example data the rowNumbers stringlist contains three entries. 对于示例数据,rowNumbers字符串列表包含三个条目。 Each entry one line from your file. 每个条目距文件一行。 These lines you have to split. 这些行您必须拆分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM