Results 1 to 2 of 2

Thread: Loading csv data to tablewideget (I am completely stuck!)

  1. #1
    Join Date
    Aug 2013
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Loading csv data to tablewideget (I am completely stuck!)

    Hello guys,

    I am very new to Qt. Trying to load csv data to my simple tablewideget. Here is

    Data.csv:
    John Kl;34;1335532;CA;0444344
    Kuma jo;54;44432;NY;0322355
    Lebal ho;24;44022;NY;0110004
    Here is what i am trying:
    Qt Code:
    1. #include <QApplication>
    2. #include <QMainWindow>
    3. #include <QTableWidget>
    4. #include <QTableWidgetItem>
    5. #include <QFile>
    6. #include <QString>
    7. #include <QStringList>
    8. #include <QMessageBox>
    9. #include <QTextStream>
    10. #include <QDebug>
    11.  
    12. int main(int argc, char **argv)
    13. {
    14. QApplication app(argc, argv);
    15. QMainWindow *window = new QMainWindow();
    16.  
    17. QFile file("Data.txt");
    18. if(!file.open(QIODevice::ReadOnly))
    19. msg.information(0,"Error!","Error opening file!",0);
    20.  
    21. QTextStream in(&file);
    22. QStringList loadCsv;
    23.  
    24. QTableWidget *myTable=new QTableWidget();
    25.  
    26.  
    27.  
    28. while(!file.atEnd()){
    29.  
    30. loadCsv<<in.readLine().split(";");
    31.  
    32. myTable->setColumnCount(loadCsv.size());
    33. for(int row=0;row<in.readLine().size();++row){
    34. myTable->setRowCount(loadCsv.size()); //It reading only first line.
    35. for(int col=0;col<loadCsv.size();++col){
    36.  
    37. myTable->setColumnCount(loadCsv.size());
    38. QTableWidgetItem *Items= new QTableWidgetItem(loadCsv[col]);
    39. myTable->setItem(row,col,Items);
    40.  
    41. }
    42. }
    43. }
    44. qDebug()<<loadCsv;
    45. window->setCentralWidget(myTable);
    46. window->show();
    47. return app.exec();
    48. }
    To copy to clipboard, switch view to plain text mode 
    I know this code is not okay as it is not working at all and i don't know how to fix it, I guess i am doing something wrong with the loop. but I just don't get an easy way to make this work. If someone can help me to load the csv to load in the QTableWideget then it will be a greate resource for me to study.
    Last edited by qtgraphical; 24th August 2013 at 19:37. Reason: updated contents

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Loading csv data to tablewideget (I am completely stuck!)

    If someone can help me to load the csv to load in the QTableWideget then it will be a greate resource for me to study.
    Ok, study this and find you at least 5 differences from the your code.

    Qt Code:
    1. int main(int argc, char **argv)
    2. {
    3. QApplication app(argc, argv);
    4. QMainWindow *window = new QMainWindow();
    5.  
    6. QFile file("Data.txt");
    7. if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
    8. msg.information(0,"Error!","Error opening file!",0);
    9. else
    10. {
    11. QTextStream in(&file);
    12. QStringList loadCsv;
    13.  
    14. QTableWidget *myTable=new QTableWidget();
    15. myTable->setColumnCount(5);
    16. int row=0;
    17.  
    18. while(!in.atEnd()){
    19. loadCsv<<in.readLine().split(";");
    20.  
    21. if(!loadCsv.isEmpty())
    22. {
    23. myTable->insertRow(myTable->rowCount());
    24.  
    25. for(int col=0;col<5;++col){
    26. if(col < loadCsv.size())
    27. {
    28. QTableWidgetItem *Items= new QTableWidgetItem(loadCsv[col]);
    29. myTable->setItem(row,col,Items);
    30. }
    31. }
    32. row++;
    33. loadCsv.clear();
    34. }
    35. }
    36. file.close();
    37. window->setCentralWidget(myTable);
    38. }
    39. window->show();
    40. return app.exec();
    41. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Asynchronous loading from HTTP (as data stream)
    By shestero in forum Qt Programming
    Replies: 0
    Last Post: 21st October 2012, 10:45
  2. Slow loading of data into QTableWidget
    By KenJustKen in forum Qt Programming
    Replies: 5
    Last Post: 11th April 2012, 15:50
  3. Replies: 5
    Last Post: 2nd April 2011, 12:03
  4. Loading data in a thread
    By Cruz in forum Qt Programming
    Replies: 23
    Last Post: 28th October 2010, 17:18
  5. Loading a QPixmap from raw data
    By pherthyl in forum Qt Programming
    Replies: 1
    Last Post: 27th January 2008, 10:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.