Hi,

I just started using QT a few days ago and I have the same problem. I can't get the QTableWidget to display data (items). Although it does display the right amount of cells.

Unfortunately setting the column count didn't help in my case.
Any other ideas.. Please?

Thank you,
Filip

The code I am using:

table.pro
Qt Code:
  1. QT += core gui
  2.  
  3. TARGET = table
  4. TEMPLATE = app
  5.  
  6.  
  7. SOURCES += main.cpp\
  8. mainwindow.cpp
  9.  
  10. HEADERS += mainwindow.h
  11.  
  12. FORMS +=
To copy to clipboard, switch view to plain text mode 

mainwindow.h
Qt Code:
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5. #include <QtGui>
  6. #include <QFile>
  7. #include <QColor>
  8. namespace Ui {
  9. class MainWindow;
  10. }
  11.  
  12. class MainWindow : public QMainWindow
  13. {
  14. Q_OBJECT
  15.  
  16. public:
  17. explicit MainWindow(QWidget *parent = 0);
  18. ~MainWindow();
  19. QTableWidget * dataTableWidget;
  20.  
  21. private:
  22. Ui::MainWindow *ui;
  23. };
  24.  
  25. #endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode 

main.cpp
Qt Code:
  1. #include <QtGui/QApplication>
  2. #include "mainwindow.h"
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6. QApplication a(argc, argv);
  7. MainWindow w;
  8. w.resize(800,500);
  9. w.show();
  10.  
  11.  
  12.  
  13. return a.exec();
  14. }
To copy to clipboard, switch view to plain text mode 

mainwindow.cpp
Qt Code:
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4. MainWindow::MainWindow(QWidget *parent) :
  5. QMainWindow(parent),
  6. ui(new Ui::MainWindow)
  7. {
  8. ui->setupUi(this);
  9.  
  10. dataTableWidget = new QTableWidget(this);
  11.  
  12. dataTableWidget->resize(243,250);
  13. dataTableWidget->move(25,180);
  14. dataTableWidget->setColumnCount(2);
  15. QStringList headers;
  16. headers << "Velocity" << "Time";
  17. dataTableWidget->setHorizontalHeaderLabels(headers);
  18. dataTableWidget->show();
  19. QString path = "/home/filip/Documents/TestScript.csv";
  20. QFile file(path);
  21. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  22. {
  23.  
  24. }
  25.  
  26.  
  27. QTextStream in(&file);
  28. while (!in.atEnd())
  29. {
  30. dataTableWidget->insertRow(dataTableWidget->rowCount());
  31. qDebug()<<"rowNumber = " << dataTableWidget->rowCount();
  32.  
  33. QString line = in.readLine();
  34.  
  35. int commaIndex = line.indexOf(",");
  36.  
  37. QString velString = line.left(commaIndex);
  38. qDebug()<< "accString = " << velString;
  39.  
  40. QString timeString = line.mid(commaIndex+1);
  41. qDebug()<< "timeString = " << timeString;
  42.  
  43. newVelItem->setText(velString);
  44. newVelItem->setTextColor("black");
  45. dataTableWidget->setItem(dataTableWidget->rowCount(),1,newVelItem);
  46. qDebug()<< "newVelItem = " << newVelItem->text();
  47.  
  48. QTableWidgetItem *newTimeItem = new QTableWidgetItem;
  49. newTimeItem->setText(timeString);
  50. newTimeItem->setTextColor("black");
  51. dataTableWidget->setItem(dataTableWidget->rowCount(),2,newTimeItem);
  52. qDebug()<< "newTimeItem = " << newTimeItem->text();
  53. }
  54. dataTableWidget->update();
  55. }
  56.  
  57. MainWindow::~MainWindow()
  58. {
  59. delete ui;
  60. }
To copy to clipboard, switch view to plain text mode 

The csv file I am parsing (TestScript.csv)
0,0
0.5,1
1,2
1.5,3
2,4
2.5,5
3,6
3,7
3,8
3,9
3,10
3,11
3,12
3,13
3,14
3,15
3,16
3,17
3,18
3,19
3,20
3,21
3,22
3,23
3,24
3,25
3,26
3,27
3,28
3,29
3,30