Results 1 to 2 of 2

Thread: Load CSV file into QTableWidget

  1. #1
    Join Date
    May 2015
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Load CSV file into QTableWidget

    Hi all.

    I've been using Qt for a few days now and having trouble populating a QTableWidget with data from an CSV file. My code is as follows:

    Input text file:

    Qt Code:
    1. Test1
    2. Heyheyhey25
    To copy to clipboard, switch view to plain text mode 

    Code for inserting the file into QTableWidget:

    Qt Code:
    1. {
    2. QFile file("filename");
    3.  
    4. // QString fileName = QFileDialog::getOpenFileName(this, ("Open File"), "/home", ("csv File(*.csv)"));
    5. // QFile file(fileName);
    6.  
    7. QString data;
    8. QStringList rowOfData;
    9. QStringList rowData;
    10. data.clear();
    11. rowOfData.clear();
    12. rowData.clear();
    13.  
    14. if (file.open(QFile::ReadOnly))
    15. {
    16. data = file.readAll();
    17. rowOfData = data.split("\n");
    18. file.close();
    19. }
    20.  
    21. qDebug()<<rowOfData;
    22.  
    23. for (int x = 0; x < rowOfData.size(); x++)
    24. {
    25. rowData = rowOfData.at(x).split(";");
    26. qDebug()<<rowData;
    27. for (int y = 0; y < rowData.size(); y++)
    28. {
    29. ui->tableWidgetScenarios->item(x,y)->setText(rowData[y]);
    30. }
    31. }
    32. }
    To copy to clipboard, switch view to plain text mode 

    It crashes whenever I try to insert my data into the tablewidget. Here's the debugger output and error message:

    Qt Code:
    1. Debugging starts
    2. ("Test1", "Heyheyhey25")
    3. ("Test1")
    4.  
    5. qtablewidget.h:181: error: Exception at 0x646e55e3, code: 0xc0000005: read access violation at: 0x0, flags=0x0 (first chance)
    To copy to clipboard, switch view to plain text mode 

    I've tried with multiple files, same result. Do I need to initialize the widget in some way or what am I doing wrong?

    Thanks in advance, regards.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Load CSV file into QTableWidget

    Quote Originally Posted by thecml View Post
    Qt Code:
    1. for (int x = 0; x < rowOfData.size(); x++)
    2. {
    3. rowData = rowOfData.at(x).split(";");
    4. qDebug()<<rowData;
    5. for (int y = 0; y < rowData.size(); y++)
    6. {
    7. ui->tableWidgetScenarios->item(x,y)->setText(rowData[y]);
    8. }
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    It crashes whenever I try to insert my data into the tablewidget. Here's the debugger output and error message:
    Assuming you don't have code elsewhere that creates table widget items, then
    Qt Code:
    1. ui->tableWidgetScenarios->item(x,y)->setText(rowData[y]);
    To copy to clipboard, switch view to plain text mode 
    tries to access an item that does not exist. I.e. item(x, y) returns 0 and you are then calling a method on a null pointer.

    See QTableWidget::setItem().

    Cheers,
    _

Similar Threads

  1. Can Qwebview load xml/xsl file?
    By richardander in forum Qt Programming
    Replies: 3
    Last Post: 26th August 2015, 22:36
  2. Load pls file with QMediaPlaylist
    By KeineAhnung in forum Newbie
    Replies: 1
    Last Post: 19th August 2014, 12:43
  3. not able to load images to qtablewidget
    By rashmi in forum Newbie
    Replies: 8
    Last Post: 3rd December 2010, 21:39
  4. Replies: 3
    Last Post: 17th March 2010, 10:31
  5. Load ActiveX from file
    By Passer_dj in forum Qt Programming
    Replies: 1
    Last Post: 13th August 2007, 23:24

Tags for this Thread

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.