PDA

View Full Version : update items in QTableWidget



darshan
21st February 2009, 11:46
Hello,

I was wondering how i would go about updating values in a QTableWidgetItem. At the moment a menu item is clicked and the table and items are displayed. But i have to close and reopen the window for the updated values to be displayed. Is there a way to keep the table open and see the values change automatically?

thanks
Darshan

wysota
21st February 2009, 13:26
You shouldn't need to do that. Can we see the code used to update the items?

darshan
21st February 2009, 13:29
heres the code


void MainWindow::setupSummaryTable()
{

s_table = new QTableWidget(0,5);

QStringList labels;
labels << tr("IP Addresses") << tr("Received") << tr("Sent")
<< tr("Total") << tr("# Packets");

s_table->setHorizontalHeaderLabels(labels);
s_table->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
s_table->horizontalHeader()->setResizeMode(1, QHeaderView::Stretch);
s_table->verticalHeader()->hide();
s_table->setShowGrid(true);
s_table->setSelectionBehavior(QAbstractItemView::SelectRows );
s_table->setEditTriggers(QAbstractItemView::NoEditTriggers) ;
s_table->setSelectionMode(QAbstractItemView::SingleSelectio n);

QStringList list;

list << packetTable->getKeys();


for(int j=0;j<list.size();j++)
{
s_table->insertRow(j);

item = new QTableWidgetItem(list.at(j));

QTableWidgetItem *item1 = packetTable->getIncoming(list.at(j));

QTableWidgetItem *item2 = packetTable->getPacketCount(list.at(j));

QTableWidgetItem *item3 = packetTable->getOutgoing(list.at(j));

QTableWidgetItem *item4 = packetTable->getTotalBytes(list.at(j));

s_table->setItem(j,0,item);

s_table->setItem(j,1,item1);

s_table->setItem(j,2,item3);

s_table->setItem(j,3,item4);

s_table->setItem(j,4,item2);

}


}

this functions a slot so is called whenever the menu item is clicked.

oh btw this is an mdi application so the table are put into subwindows :)

wysota
21st February 2009, 14:26
Where do item1 through item4 come from? Are you sure they are valid items? For me you are doing it the wrong way. Try this:

QStringList list;
//...
fo(int i=0;i<list.size();i++){
const QString &str = list.at(i);
QTreeWidgetItem *item = new QTreeWidgetItem;
item->setText(0, str);
item->setText(1, getIncomingStr(str));
item->setText(2, getTotalBytesStr(str));
item->setText(3, getPacketCountStr(str));
item->setText(4, getOutgoingStr(str));
s_table->addTopLevelItem(item);
}
...where *Str() methods return strings containing values you want placed in each column.

Now if you want to update items, get rid of line 5 and substitute it with:

QTreeWidgetItem *item = topLevelItem(i);
and remove the line adding the item to the tree.

darshan
21st February 2009, 14:40
hey,

its a tableWidget btw

the items are being called from another class which holds the data.



QTableWidgetItem *PacketTable::getOutgoing(QString ip)
{

QTableWidgetItem *outgoingItem;

QHashIterator<QString, Packet*> i(hash);
while (i.hasNext()){
i.next();

if(i.key()==ip){

outgoingItem = new QTableWidgetItem(QString::number(i.value()->getOutgoing()));
return outgoingItem;
}

}
return 0;
}

wysota
21st February 2009, 14:44
Tree or table, what's the difference :)

It's important that you don't create new items but instead work on those already there.

darshan
21st February 2009, 15:03
i thought i would need to call a signal to refresh the widget to be able to get the latest values from QTableWidgetItem methods?

wysota
21st February 2009, 15:13
No, when you change any of the items inside the widget, the change will be propagated to the internal model of the view that will let it know this particular item needs refreshing.

darshan
26th February 2009, 22:53
Tree or table, what's the difference :)

It's important that you don't create new items but instead work on those already there.

im only creating the item once but still i need to reopen the subwindow to see the new values in the table. Any suggestions? :confused:

wysota
27th February 2009, 00:30
Can you prepare a minimal compilable example reproducing the problem?

darshan
28th February 2009, 10:30
the code is similar to the above.

The calling object class has this method which gets a value associated with the given string argument...this value is always being updated in the Packet class which is stored in the hash.


int PacketTable::getPacketCount(QString ip)
{
int count;

QHashIterator<QString, Packet*> i(hash);
while (i.hasNext()){
i.next();

if(i.key()==ip){

count = i.value()->getCount();
return count;
}

}
return 0;
}

then i call this method in my main window class to retrieve the value and put it up in a table to display like this:


for(int i=0;i<lists.size();i++)
{
s_table->setItem(i,4,new QTableWidgetItem(QString::number(packetTable->getPacketCount(str))));
}

but the value is only updated when i close and reopen the subwindow which displays the table widget :mad:

wysota
28th February 2009, 12:31
I'm not sure why you have to be doing it your own way. What happens if you use setText() instead of setItem()? Also what is outside this for loop? Please provide a compilable example reproducing the problem. The point of doing it is that you have to move the code to a new project and see if the problem persists. I just want to do qmake, make, run the application and see the problem.

darshan
3rd March 2009, 11:12
i have tried setText() but i still get the same result. I am only getting integer values from another class which are always being updated. I am adding these values to the table like this


netTable->setItem(0,4,new QTableWidgetItem(QString::number(table->getDefault())));

table being the class and getDefault() being the method with the value.

Whenver i open a table i get this message in the terminal:

QPainter::begin: Cannot paint on a null pixmap
QPainter::setRenderHint: Painter must be active to set rendering hints

could it be because of this?

wysota
3rd March 2009, 22:43
Could you prepare a minimal compilable example reproducing the problem?