Results 1 to 5 of 5

Thread: QTableWidgetItem no text

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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: QTableWidgetItem no text

    Hi FH

    You have two problems in you code

    1. You are inserting a new row in the table, this is ok, but you are setting the new items on the new row (which does not exists). You use dataTableWidget->rowCount() to insert row, and also to set items, dataTableWidget->rowCount() will give the latest row count, when you insert the a row, this will increase, which is the problem in your case.

    2. You are setting the column count as 2, your column numbers are 0 & 1 (not 1 & 2), so you need to set item on column 0 and column 1

    These will solve you Table problem. Below is modified code for your reference. I have added few more comments inline which will help you improve you code.


    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. dataTableWidget = new QTableWidget(this);
    8.  
    9. //dataTableWidget->resize(243,250); //Not Required, if setCentralWidget(dataTableWidget);
    10. //dataTableWidget->move(25,180); //Not Required, if using setCentralWidget(dataTableWidget);
    11. dataTableWidget->setColumnCount(2);
    12. QStringList headers;
    13. headers << "Velocity" << "Time";
    14. dataTableWidget->setHorizontalHeaderLabels(headers);
    15. dataTableWidget->show(); //Remove, setCentralWidget(dataTableWidget);
    16. QString path = "C:/Data/TestScript.csv";
    17. QFile file(path);
    18.  
    19. if (file.open(QIODevice::ReadOnly | QIODevice::Text)) //Modified
    20. {
    21. QTextStream in(&file);
    22. while (!in.atEnd())
    23. {
    24. int row = dataTableWidget->rowCount(); //Added
    25. dataTableWidget->insertRow(row); //Added, Insert a row, and set the items on same row
    26. qDebug()<<"rowNumber = " << row;
    27.  
    28. QString line = in.readLine();
    29.  
    30. int commaIndex = line.indexOf(",");
    31.  
    32. QString velString = line.left(commaIndex);
    33. qDebug()<< "accString = " << velString;
    34.  
    35. QString timeString = line.mid(commaIndex+1);
    36. qDebug()<< "timeString = " << timeString;
    37.  
    38. newVelItem->setText(velString);
    39. newVelItem->setTextColor("black");
    40. dataTableWidget->setItem(row, 0, newVelItem); //Modified, Column 0
    41. qDebug()<< "newVelItem = " << newVelItem->text();
    42.  
    43. QTableWidgetItem *newTimeItem = new QTableWidgetItem;
    44. newTimeItem->setText(timeString);
    45. newTimeItem->setTextColor("black");
    46. dataTableWidget->setItem(row, 1, newTimeItem); //Modified, Column 1
    47. qDebug()<< "newTimeItem = " << newTimeItem->text();
    48. }
    49. //dataTableWidget->update(); //Not Required, as it will be updated when show() is called on MainWindow
    50. file.close(); //Added, just to close the open file
    51. }
    52.  
    53. setCentralWidget(dataTableWidget); //Added, this does the layout magic
    54. }
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to Santosh Reddy for this useful post:

    F. H. Saad (15th June 2011)

Similar Threads

  1. Extracting text from QTableWidgetItem
    By bizmopeen in forum Newbie
    Replies: 3
    Last Post: 1st September 2009, 17:28
  2. QTableWidgetItem Text Editing
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 17th March 2009, 02:23
  3. Rich Text in QTableWidgetItem
    By joshuajcarson in forum Qt Programming
    Replies: 9
    Last Post: 2nd September 2008, 15:49
  4. QTableWidgetItem Text
    By pytro in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2007, 21:44
  5. Auto text select in QTableWidgetItem
    By tstankey in forum Newbie
    Replies: 2
    Last Post: 5th October 2006, 20:40

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
  •  
Qt is a trademark of The Qt Company.