Re: QTableWidgetItem no text
I guess you are not setting the column count for tables, you have do set a column count before setting the items.
Code:
...
ui->tblAge6->setColumnCount(3);
...
Re: QTableWidgetItem no text
Thank you very much! That was the missing point. Works fine now.
You saved my day!
Re: QTableWidgetItem no text
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
Code:
QT += core gui
TARGET = table
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS +=
mainwindow.h
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtGui>
#include <QFile>
#include <QColor>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
main.cpp
Code:
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
MainWindow w;
w.resize(800,500);
w.show();
return a.exec();
}
mainwindow.cpp
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
dataTableWidget->resize(243,250);
dataTableWidget->move(25,180);
dataTableWidget->setColumnCount(2);
headers << "Velocity" << "Time";
dataTableWidget->setHorizontalHeaderLabels(headers);
dataTableWidget->show();
QString path
= "/home/filip/Documents/TestScript.csv";
{
}
while (!in.atEnd())
{
dataTableWidget->insertRow(dataTableWidget->rowCount());
qDebug()<<"rowNumber = " << dataTableWidget->rowCount();
int commaIndex = line.indexOf(",");
QString velString
= line.
left(commaIndex
);
qDebug()<< "accString = " << velString;
QString timeString
= line.
mid(commaIndex
+1);
qDebug()<< "timeString = " << timeString;
newVelItem->setText(velString);
newVelItem->setTextColor("black");
dataTableWidget->setItem(dataTableWidget->rowCount(),1,newVelItem);
qDebug()<< "newVelItem = " << newVelItem->text();
newTimeItem->setText(timeString);
newTimeItem->setTextColor("black");
dataTableWidget->setItem(dataTableWidget->rowCount(),2,newTimeItem);
qDebug()<< "newTimeItem = " << newTimeItem->text();
}
dataTableWidget->update();
}
MainWindow::~MainWindow()
{
delete ui;
}
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
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.
Code:
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
//dataTableWidget->resize(243,250); //Not Required, if setCentralWidget(dataTableWidget);
//dataTableWidget->move(25,180); //Not Required, if using setCentralWidget(dataTableWidget);
dataTableWidget->setColumnCount(2);
headers << "Velocity" << "Time";
dataTableWidget->setHorizontalHeaderLabels(headers);
dataTableWidget->show(); //Remove, setCentralWidget(dataTableWidget);
QString path
= "C:/Data/TestScript.csv";
{
while (!in.atEnd())
{
int row = dataTableWidget->rowCount(); //Added
dataTableWidget->insertRow(row); //Added, Insert a row, and set the items on same row
qDebug()<<"rowNumber = " << row;
int commaIndex = line.indexOf(",");
QString velString
= line.
left(commaIndex
);
qDebug()<< "accString = " << velString;
QString timeString
= line.
mid(commaIndex
+1);
qDebug()<< "timeString = " << timeString;
newVelItem->setText(velString);
newVelItem->setTextColor("black");
dataTableWidget->setItem(row, 0, newVelItem); //Modified, Column 0
qDebug()<< "newVelItem = " << newVelItem->text();
newTimeItem->setText(timeString);
newTimeItem->setTextColor("black");
dataTableWidget->setItem(row, 1, newTimeItem); //Modified, Column 1
qDebug()<< "newTimeItem = " << newTimeItem->text();
}
//dataTableWidget->update(); //Not Required, as it will be updated when show() is called on MainWindow
file.close(); //Added, just to close the open file
}
setCentralWidget(dataTableWidget); //Added, this does the layout magic
}