PDA

View Full Version : Please help with QTableWidget.



chris_helloworld
1st July 2010, 11:03
I have a QTableWidget that is populated and want to change the data in one of the cells. I do something like,

QTableWidgetItem *item = mytable->item(row, col);
item->setText("new data");

/* or item->setData(Qt::DisplayRole, somevariant) */

However the QTableWidget is not correctly updated. Resizing the table or simply clicking in it, does cause the data to be correctly refreshed and shows the new value.

Is there something I need to do to tell QTableWidget that the data has been changed ?

Thanks in advance,
Chris.

tbscope
1st July 2010, 11:19
You could try:



update(indexFromItem(item));

Zlatomir
1st July 2010, 11:32
You can use signals and slots: emit some signal whenever you change something, and call update (http://doc.trolltech.com/4.6/qwidget.html#update) or repaint (http://doc.trolltech.com/4.6/qwidget.html#repaint)

chris_helloworld
1st July 2010, 11:42
Thanks guys,
It tried
update(indexFromItem(item));

and also
repaint();

but neither of these approaches seemed to work :(

tbscope
1st July 2010, 11:47
That's weird.

Normally, you shouldn't even need to call update or repaint yourself as changing an item calls this internally.

And now you don't even see the updated item even if you directly call update or repaint. To me this sounds like the problem is somewhere else. Can you post the complete code please?

Zlatomir
1st July 2010, 12:05
I don't know what can be wrong with your code, but i can recommend an example: the code from 2nd Edition of C++ GUI Programming with Qt4 (http://www.informit.com/store/product.aspx?isbn=0132354160) contains an example of a Spreadsheet maybe that code will help you correct the issue in your code (you can download the source code from the link)

chris_helloworld
1st July 2010, 12:15
Thanks,
I will try producing a small example app that reproduces the problem.

Cheers

chris_helloworld
1st July 2010, 13:26
The following code demos the problem.

mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui>

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
QTableWidget *table;
QPushButton *b1;
public slots:
void inc();
};

#endif // MAINWINDOW_H

mainwindow.cpp


#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QVBoxLayout *l = new QVBoxLayout();
table = new QTableWidget(1, 1);
table->verticalHeader()->setDefaultSectionSize(16);
table->verticalHeader()->hide();

table->setItem(0, 0, new QTableWidgetItem("1"));

b1 = new QPushButton("Increment");

l->addWidget(table);
l->addWidget(b1);

QWidget *w = new QWidget();
w->setLayout(l);
setCentralWidget(w);

connect(b1, SIGNAL(clicked()), this, SLOT(inc()));
}

void MainWindow::inc()
{
QTableWidgetItem *i = table->item(0, 0);
i->setText(QString::number(i->text().toInt() + 1));
}


Clicking the button should increment the value in the table, but doesnt until the table is clicked on or resized etc..

Zlatomir
1st July 2010, 13:32
That example code works fine on my system (win 7 x64), i can test it on ubuntu 10.4 and debian 5 a little later.

chris_helloworld
1st July 2010, 13:37
I'm on Win XP SP3 and using Qt 4.5.3

Zlatomir
1st July 2010, 13:40
I use Qt 4.6.3,
have you tried, to force the repainting of table?

connect(b1, SIGNAL(clicked()), table, SLOT(repaint()));

chris_helloworld
1st July 2010, 13:47
Yes, and also tried simply calling table->repaint() directly after setting the data.
Neither helps :(

Chris.

Zlatomir
1st July 2010, 13:51
Update Qt, it is a bug (http://bugreports.qt.nokia.com/browse/QTBUG-5042), resolved in the newer releases of Qt

chris_helloworld
1st July 2010, 13:54
Ok, many thanks for your help - dont suppose you have a reference or web link to any info on the bug ?

chris_helloworld
1st July 2010, 14:00
Ah, just realised you included the link, thanks again :)