Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableView inside QTabWidget does not update UI after model data changes until user clicks inside the tab widget
Forum Updated to NodeBB v4.3 + New Features

QTableView inside QTabWidget does not update UI after model data changes until user clicks inside the tab widget

Scheduled Pinned Locked Moved General and Desktop
11 Posts 5 Posters 4.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jmalicke
    wrote on last edited by
    #1

    I have a form with a QTabWidget and a QPushButton.

    Inside the QTabWidget I have a QWidget with a QTableView.

    Clicking the QPushButton updates the values in the table model.

    The problem is that after clicking the button, the table UI does not show any changes. Once you click anywhere inside the QTabWidget, that's when the table updates its display to show the new values. So in other words, the changes in the model are happening but they are not being updated until the user clicks somewhere inside the QTabWidget.

    I have tried using setFocus() on the QTabWidget and its child tab QWidget but neither have worked.

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      how exactly do you update the data in the table model?
      You need to make sure that you emit the dataChanged() signal out of the model in order to tell the view that it needs to repaint a cell.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jmalicke
        wrote on last edited by
        #3

        I update the data with model()->setData(). The model does indeed emit dataChanged(). Like I said, the table data instantly updates in the UI once you click anywhere in the tab widget. The data is set but not shown.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jmalicke
          wrote on last edited by
          #4

          I had to use QTableView::repaint() to solve the problem.

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            [quote author="jmalicke" date="1422403767"]I had to use QTableView::repaint() to solve the problem.[/quote]
            But you should avoid such calls in general, since it is an indicator for false code design if you need to do this.

            Please post the code of the setData() method.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • L Offline
              L Offline
              loopless
              wrote on last edited by
              #6

              This is and old one, but I see the same issue. TableView inside tab widget. QT 5.11.1
              I have a "reset" push button that changes the model data then emits dataChanged(). But the table will not redraw until , for example, I move the mouse from the button into the TableView region or otherwise interact with the TableView.
              Google searches show that this is occurring to other people as well.

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                Can't replicate the issue. Can you try the minimal example below?

                #include <QApplication>
                #include <QTableView>
                #include <QStandardItemModel>
                #include <QTabWidget>
                #include <QLabel>
                #include <QVBoxLayout>
                #include <QPushButton>
                int main(int argc, char **argv)
                {
                    QApplication app(argc,argv);
                    QStandardItemModel model;
                    model.insertRows(0,5);
                    model.insertColumns(0,3);
                    for(int i=0;i<5;++i){
                        for(int j=0;j<3;++j){
                            model.setData(model.index(i,j),QStringLiteral("%1,%2").arg(i+1).arg(j+1));
                        }
                    }
                
                    QWidget wid;
                    QVBoxLayout* mianLay = new QVBoxLayout(&wid);
                    QTabWidget* tabWid = new QTabWidget(&wid);
                    tabWid->addTab(new QLabel(QStringLiteral("Hello World!"),&wid),QStringLiteral("Hello"));
                    QTableView* tableView = new QTableView(&wid);
                    tableView->setModel(&model);
                    tabWid->addTab(tableView,QStringLiteral("table"));
                    mianLay->addWidget(tabWid);
                    QPushButton* changeDataButton = new QPushButton(QStringLiteral("Change"),&wid);
                    QObject::connect(changeDataButton,&QPushButton::clicked,&model,[&model]()->void{
                                         model.setData(model.index(2,1),QStringLiteral("Changed"));
                                     });
                    mianLay->addWidget(changeDataButton);
                    wid.show();
                    return app.exec();
                }
                

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                L 1 Reply Last reply
                1
                • VRoninV VRonin

                  Can't replicate the issue. Can you try the minimal example below?

                  #include <QApplication>
                  #include <QTableView>
                  #include <QStandardItemModel>
                  #include <QTabWidget>
                  #include <QLabel>
                  #include <QVBoxLayout>
                  #include <QPushButton>
                  int main(int argc, char **argv)
                  {
                      QApplication app(argc,argv);
                      QStandardItemModel model;
                      model.insertRows(0,5);
                      model.insertColumns(0,3);
                      for(int i=0;i<5;++i){
                          for(int j=0;j<3;++j){
                              model.setData(model.index(i,j),QStringLiteral("%1,%2").arg(i+1).arg(j+1));
                          }
                      }
                  
                      QWidget wid;
                      QVBoxLayout* mianLay = new QVBoxLayout(&wid);
                      QTabWidget* tabWid = new QTabWidget(&wid);
                      tabWid->addTab(new QLabel(QStringLiteral("Hello World!"),&wid),QStringLiteral("Hello"));
                      QTableView* tableView = new QTableView(&wid);
                      tableView->setModel(&model);
                      tabWid->addTab(tableView,QStringLiteral("table"));
                      mianLay->addWidget(tabWid);
                      QPushButton* changeDataButton = new QPushButton(QStringLiteral("Change"),&wid);
                      QObject::connect(changeDataButton,&QPushButton::clicked,&model,[&model]()->void{
                                           model.setData(model.index(2,1),QStringLiteral("Changed"));
                                       });
                      mianLay->addWidget(changeDataButton);
                      wid.show();
                      return app.exec();
                  }
                  
                  L Offline
                  L Offline
                  loopless
                  wrote on last edited by
                  #8

                  @VRonin Of course your example works fine:)
                  The differences are that I am using a ProxyModel and modifying the underlying private data then "emit model->dataChanged(QModelIndex(), QModelIndex());" .
                  In the end the only way I could fix the issue was a sledgehammer approach (as others I found in the google search mentioned as well). update() or repaint() do nothing.
                  table->setModel(model)
                  In every other way the tableview works perfectly fine.

                  It's quite bizarre. I click the 'change' button (no visible update) and I can slowly pixel by pixel move the mouse pointer from the button, as soon as it moves into the TableView widget then the TableView updates

                  JonBJ VRoninV 2 Replies Last reply
                  0
                  • L loopless

                    @VRonin Of course your example works fine:)
                    The differences are that I am using a ProxyModel and modifying the underlying private data then "emit model->dataChanged(QModelIndex(), QModelIndex());" .
                    In the end the only way I could fix the issue was a sledgehammer approach (as others I found in the google search mentioned as well). update() or repaint() do nothing.
                    table->setModel(model)
                    In every other way the tableview works perfectly fine.

                    It's quite bizarre. I click the 'change' button (no visible update) and I can slowly pixel by pixel move the mouse pointer from the button, as soon as it moves into the TableView widget then the TableView updates

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by
                    #9

                    @loopless
                    I don't find "Google searches show that this is occurring to other people as well". I only find this thread (and on qtcentre). Do you have any references?

                    It's hard to know without seeing code. Maybe you and the OP have something similar going on, we don't know what's in either code.

                    1 Reply Last reply
                    0
                    • L loopless

                      @VRonin Of course your example works fine:)
                      The differences are that I am using a ProxyModel and modifying the underlying private data then "emit model->dataChanged(QModelIndex(), QModelIndex());" .
                      In the end the only way I could fix the issue was a sledgehammer approach (as others I found in the google search mentioned as well). update() or repaint() do nothing.
                      table->setModel(model)
                      In every other way the tableview works perfectly fine.

                      It's quite bizarre. I click the 'change' button (no visible update) and I can slowly pixel by pixel move the mouse pointer from the button, as soon as it moves into the TableView widget then the TableView updates

                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by VRonin
                      #10

                      @loopless said in QTableView inside QTabWidget does not update UI after model data changes until user clicks inside the tab widget:

                      emit model->dataChanged(QModelIndex(), QModelIndex());

                      Is this your actual code? if so the problem is right there. If you don't pass a valid index to the signal the view can't know which item changed. This problem would have been spotted by the model test this is why I always recommend running it whenever a model gets implemented

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      JonBJ 1 Reply Last reply
                      2
                      • VRoninV VRonin

                        @loopless said in QTableView inside QTabWidget does not update UI after model data changes until user clicks inside the tab widget:

                        emit model->dataChanged(QModelIndex(), QModelIndex());

                        Is this your actual code? if so the problem is right there. If you don't pass a valid index to the signal the view can't know which item changed. This problem would have been spotted by the model test this is why I always recommend running it whenever a model gets implemented

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by
                        #11

                        @VRonin I unthinkingly assumed that "range" meant "all indexes"... :(

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved