Results 1 to 15 of 15

Thread: Please help with QTableWidget.

  1. #1
    Join Date
    Feb 2010
    Posts
    52
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Please help with QTableWidget.

    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:isplayRole, 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.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: Please help with QTableWidget.

    You could try:

    Qt Code:
    1. update(indexFromItem(item));
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: Please help with QTableWidget.

    You can use signals and slots: emit some signal whenever you change something, and call update or repaint

  4. #4
    Join Date
    Feb 2010
    Posts
    52
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: Please help with QTableWidget.

    Thanks guys,
    It tried
    update(indexFromItem(item));

    and also
    repaint();

    but neither of these approaches seemed to work

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: Please help with QTableWidget.

    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?

  6. #6
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: Please help with QTableWidget.

    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 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)

  7. #7
    Join Date
    Feb 2010
    Posts
    52
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: Please help with QTableWidget.

    Thanks,
    I will try producing a small example app that reproduces the problem.

    Cheers

  8. #8
    Join Date
    Feb 2010
    Posts
    52
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: Please help with QTableWidget.

    The following code demos the problem.

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui>
    5.  
    6. class MainWindow : public QMainWindow
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. MainWindow(QWidget *parent = 0);
    12. QTableWidget *table;
    13. public slots:
    14. void inc();
    15. };
    16.  
    17. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. MainWindow::MainWindow(QWidget *parent)
    4. : QMainWindow(parent)
    5. {
    6. table = new QTableWidget(1, 1);
    7. table->verticalHeader()->setDefaultSectionSize(16);
    8. table->verticalHeader()->hide();
    9.  
    10. table->setItem(0, 0, new QTableWidgetItem("1"));
    11.  
    12. b1 = new QPushButton("Increment");
    13.  
    14. l->addWidget(table);
    15. l->addWidget(b1);
    16.  
    17. QWidget *w = new QWidget();
    18. w->setLayout(l);
    19. setCentralWidget(w);
    20.  
    21. connect(b1, SIGNAL(clicked()), this, SLOT(inc()));
    22. }
    23.  
    24. void MainWindow::inc()
    25. {
    26. QTableWidgetItem *i = table->item(0, 0);
    27. i->setText(QString::number(i->text().toInt() + 1));
    28. }
    To copy to clipboard, switch view to plain text mode 

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

  9. #9
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: Please help with QTableWidget.

    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.

  10. #10
    Join Date
    Feb 2010
    Posts
    52
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: Please help with QTableWidget.

    I'm on Win XP SP3 and using Qt 4.5.3

  11. #11
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: Please help with QTableWidget.

    I use Qt 4.6.3,
    have you tried, to force the repainting of table?
    Qt Code:
    1. connect(b1, SIGNAL(clicked()), table, SLOT(repaint()));
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Feb 2010
    Posts
    52
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: Please help with QTableWidget.

    Yes, and also tried simply calling table->repaint() directly after setting the data.
    Neither helps

    Chris.

  13. #13
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: Please help with QTableWidget.

    Update Qt, it is a bug, resolved in the newer releases of Qt

  14. The following user says thank you to Zlatomir for this useful post:

    chris_helloworld (1st July 2010)

  15. #14
    Join Date
    Feb 2010
    Posts
    52
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: Please help with QTableWidget.

    Ok, many thanks for your help - dont suppose you have a reference or web link to any info on the bug ?

  16. #15
    Join Date
    Feb 2010
    Posts
    52
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: Please help with QTableWidget.

    Ah, just realised you included the link, thanks again

Similar Threads

  1. QtableWidget
    By lucasbemo in forum Newbie
    Replies: 3
    Last Post: 17th March 2010, 14:25
  2. Help with QTableWidget!
    By bottin in forum Qt Programming
    Replies: 1
    Last Post: 24th July 2009, 15:10
  3. QTableWidget
    By rick_st3 in forum Newbie
    Replies: 12
    Last Post: 26th June 2008, 11:38
  4. QTableWidget
    By dragon in forum Qt Programming
    Replies: 14
    Last Post: 18th April 2007, 19:15
  5. QTableWidget
    By chak_med in forum Qt Programming
    Replies: 1
    Last Post: 17th May 2006, 15:53

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
  •  
Qt is a trademark of The Qt Company.