Results 1 to 14 of 14

Thread: update items in QTableWidget

  1. #1
    Join Date
    Nov 2008
    Posts
    23
    Thanks
    2

    Default update items in QTableWidget

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: update items in QTableWidget

    You shouldn't need to do that. Can we see the code used to update the items?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2008
    Posts
    23
    Thanks
    2

    Default Re: update items in QTableWidget

    heres the code

    Qt Code:
    1. void MainWindow::setupSummaryTable()
    2. {
    3.  
    4. s_table = new QTableWidget(0,5);
    5.  
    6. QStringList labels;
    7. labels << tr("IP Addresses") << tr("Received") << tr("Sent")
    8. << tr("Total") << tr("# Packets");
    9.  
    10. s_table->setHorizontalHeaderLabels(labels);
    11. s_table->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
    12. s_table->horizontalHeader()->setResizeMode(1, QHeaderView::Stretch);
    13. s_table->verticalHeader()->hide();
    14. s_table->setShowGrid(true);
    15. s_table->setSelectionBehavior(QAbstractItemView::SelectRows);
    16. s_table->setEditTriggers(QAbstractItemView::NoEditTriggers);
    17. s_table->setSelectionMode(QAbstractItemView::SingleSelection);
    18.  
    19.  
    20. list << packetTable->getKeys();
    21.  
    22.  
    23. for(int j=0;j<list.size();j++)
    24. {
    25. s_table->insertRow(j);
    26.  
    27. item = new QTableWidgetItem(list.at(j));
    28.  
    29. QTableWidgetItem *item1 = packetTable->getIncoming(list.at(j));
    30.  
    31. QTableWidgetItem *item2 = packetTable->getPacketCount(list.at(j));
    32.  
    33. QTableWidgetItem *item3 = packetTable->getOutgoing(list.at(j));
    34.  
    35. QTableWidgetItem *item4 = packetTable->getTotalBytes(list.at(j));
    36.  
    37. s_table->setItem(j,0,item);
    38.  
    39. s_table->setItem(j,1,item1);
    40.  
    41. s_table->setItem(j,2,item3);
    42.  
    43. s_table->setItem(j,3,item4);
    44.  
    45. s_table->setItem(j,4,item2);
    46.  
    47. }
    48.  
    49.  
    50. }
    To copy to clipboard, switch view to plain text mode 

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: update items in QTableWidget

    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:
    Qt Code:
    1. //...
    2. fo(int i=0;i<list.size();i++){
    3. const QString &str = list.at(i);
    4. item->setText(0, str);
    5. item->setText(1, getIncomingStr(str));
    6. item->setText(2, getTotalBytesStr(str));
    7. item->setText(3, getPacketCountStr(str));
    8. item->setText(4, getOutgoingStr(str));
    9. s_table->addTopLevelItem(item);
    10. }
    To copy to clipboard, switch view to plain text mode 
    ...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:
    Qt Code:
    1. QTreeWidgetItem *item = topLevelItem(i);
    To copy to clipboard, switch view to plain text mode 
    and remove the line adding the item to the tree.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2008
    Posts
    23
    Thanks
    2

    Default Re: update items in QTableWidget

    hey,

    its a tableWidget btw

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

    Qt Code:
    1. QTableWidgetItem *PacketTable::getOutgoing(QString ip)
    2. {
    3.  
    4. QTableWidgetItem *outgoingItem;
    5.  
    6. QHashIterator<QString, Packet*> i(hash);
    7. while (i.hasNext()){
    8. i.next();
    9.  
    10. if(i.key()==ip){
    11.  
    12. outgoingItem = new QTableWidgetItem(QString::number(i.value()->getOutgoing()));
    13. return outgoingItem;
    14. }
    15.  
    16. }
    17. return 0;
    18. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: update items in QTableWidget

    Tree or table, what's the difference

    It's important that you don't create new items but instead work on those already there.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Nov 2008
    Posts
    23
    Thanks
    2

    Default Re: update items in QTableWidget

    i thought i would need to call a signal to refresh the widget to be able to get the latest values from QTableWidgetItem methods?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: update items in QTableWidget

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Nov 2008
    Posts
    23
    Thanks
    2

    Default Re: update items in QTableWidget

    Quote Originally Posted by wysota View Post
    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?

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: update items in QTableWidget

    Can you prepare a minimal compilable example reproducing the problem?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Nov 2008
    Posts
    23
    Thanks
    2

    Default Re: update items in QTableWidget

    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.
    Qt Code:
    1. int PacketTable::getPacketCount(QString ip)
    2. {
    3. int count;
    4.  
    5. QHashIterator<QString, Packet*> i(hash);
    6. while (i.hasNext()){
    7. i.next();
    8.  
    9. if(i.key()==ip){
    10.  
    11. count = i.value()->getCount();
    12. return count;
    13. }
    14.  
    15. }
    16. return 0;
    17. }
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. for(int i=0;i<lists.size();i++)
    2. {
    3. s_table->setItem(i,4,new QTableWidgetItem(QString::number(packetTable->getPacketCount(str))));
    4. }
    To copy to clipboard, switch view to plain text mode 

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

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: update items in QTableWidget

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Nov 2008
    Posts
    23
    Thanks
    2

    Default Re: update items in QTableWidget

    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

    Qt Code:
    1. netTable->setItem(0,4,new QTableWidgetItem(QString::number(table->getDefault())));
    To copy to clipboard, switch view to plain text mode 

    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?

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: update items in QTableWidget

    Could you prepare a minimal compilable example reproducing the problem?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 1
    Last Post: 14th May 2008, 19:35
  2. QTableWidget: set items disappear after new insertion
    By Raccoon29 in forum Qt Programming
    Replies: 6
    Last Post: 16th March 2008, 18:28
  3. How to update scene after removing items
    By nileshsince1980 in forum Qt Programming
    Replies: 4
    Last Post: 20th September 2007, 09:56
  4. adding items in qtablewidget
    By Djony in forum Qt Programming
    Replies: 17
    Last Post: 24th November 2006, 10:03
  5. QTableWidget Update - slow
    By DPinLV in forum Qt Programming
    Replies: 16
    Last Post: 18th August 2006, 21:09

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.