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. How to ensure qtablewidget gets updated before another signal
Forum Update on Monday, May 27th 2025

How to ensure qtablewidget gets updated before another signal

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 4 Posters 4.2k Views
  • 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.
  • SGaistS SGaist

    Also, cellChanged is emitted after the data has changed.

    How is your dialog supposed to be working ?

    Q Offline
    Q Offline
    qt_keen_learner
    wrote on last edited by
    #6

    @SGaist Exactly, my dialog does not work.
    I have to shamely tell the users that they have to 'enter' or click elsewhere to change the data and then click the button.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #7

      That's why we would need to take a look at your dialog code as well as how you are using that dialog.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Q 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        There is no way to specify the order of signals.
        your issue is that user dont finish editing first and hence the signal is not sent.

        To work around this, you can override
        focusOutEvent(QFocusEvent* event) for the table

        This event should come before the button click - as table editor
        loses focus when user click the button.

        Alternatively, disable button untill user finished editing.

        Q Offline
        Q Offline
        qt_keen_learner
        wrote on last edited by
        #8

        @mrjj Thanks for proposing the workaround. It looks like the only solution.

        The button is only an example and there might be some other widgets (more buttons, clickable links, etc), and even I disable them all for a while, it is still risky to add more widgets and forget to disable them while the tableWidget is under edit.

        I'll try your workaround and get back to update with my result.

        This is a pretty general question for years, so I'm surprised I couldn't find any clue by googling.
        Haven't the Qt original dialog and widgets like tableWidget been revised to support such common user behavior?

        1 Reply Last reply
        0
        • SGaistS SGaist

          That's why we would need to take a look at your dialog code as well as how you are using that dialog.

          Q Offline
          Q Offline
          qt_keen_learner
          wrote on last edited by
          #9

          @SGaist I'm using Qt4.8, Qt Creator 3.0.1

          1. Add Qt Designer Form class with the name QtForm

          2. In the form ui edit, add a tableWidget and a pushbutton onto that Form

          3. In the constructor of form class

          ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection); // only accept one row selection
          ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); // selection on row basis
          ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "cellData");
          ui->tableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);

          4. In the form ui editor, select the tableWidget, 'Go to slot...' -> cellChanged(int,int) to add  the slot to this signal, and then add debug code
          

          qDebug() << "entered cellChanged"

          5.  In the form ui edit, select the pushButton, add slot to the signal 'clicked()' with the debug code
          

          qDebug() << "entered buttonClicked"

          
          From the trace info for the problem scenario, you can see the clicked() signal arrives first.
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #10

            Are you calling exec on that dialog ?

            On a side note, unless you're locked to 4.8, you should move at least to Qt 5.6 which is the LTS or the more recent version. Qt 4 as seen its last release with 4.8.7 and won't get any new patch unless it's a critical security issue.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            Q 1 Reply Last reply
            0
            • SGaistS SGaist

              Are you calling exec on that dialog ?

              On a side note, unless you're locked to 4.8, you should move at least to Qt 5.6 which is the LTS or the more recent version. Qt 4 as seen its last release with 4.8.7 and won't get any new patch unless it's a critical security issue.

              Q Offline
              Q Offline
              qt_keen_learner
              wrote on last edited by qt_keen_learner
              #11

              @SGaist
              Thanks for advice.
              Yes, I used 'exec' to create this model dialog.
              I have to use 4.8 because much more code was developed using Qt4.8 convention and I'd like to save the troubles of porting the code to Qt5 and upwards, though it might be smooth.

              I understand Qt4.8 is planned to be a stable version, so I'll adopt the workaround and see how it goes.

              Thanks again.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #12

                Qt 4.8 is not a stable version, it has reached end of life. The current "stable" as in "long term support" is Qt 5.6

                And unless you're unlucky, the port from Qt 4 to Qt 5 is pretty painless, the old signals/slots syntax works exactly the same as before.

                Back to your problem, what do you do exactly with that slot connected to the clicked signal ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                Q 1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #13
                  1. In the form ui editor, select the tableWidget, 'Go to slot...' -> cellChanged(int,int) to add the slot to this signal, and then add debug code
                  2. In the form ui edit, select the pushButton, add slot to the signal 'clicked()' with the debug code

                  check the generated moc_[className].h and see if the connect for cellChanged comes before or after the one for clicked. (it should come before).

                  To have a quick and dirty fix add Qt::QueuedConnection as argument to the connect of the clicked signal

                  "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

                  Q 1 Reply Last reply
                  2
                  • SGaistS SGaist

                    Qt 4.8 is not a stable version, it has reached end of life. The current "stable" as in "long term support" is Qt 5.6

                    And unless you're unlucky, the port from Qt 4 to Qt 5 is pretty painless, the old signals/slots syntax works exactly the same as before.

                    Back to your problem, what do you do exactly with that slot connected to the clicked signal ?

                    Q Offline
                    Q Offline
                    qt_keen_learner
                    wrote on last edited by
                    #14

                    @SGaist
                    what I did in the slot to the 'clicked' signal is to use the cell data, which is a variable (say., tableValue) updated in the slot function to 'cellChanged' .
                    Because the variable has yet been updated, the value of tableValue is not correct (it is still the value before update).

                    A very simple usage is

                    send(tableValue)
                    

                    in the 'clicked' slot, to send the value out. Because tableValue has not been updated, the value sent out is incorrect.

                    1 Reply Last reply
                    0
                    • VRoninV VRonin
                      1. In the form ui editor, select the tableWidget, 'Go to slot...' -> cellChanged(int,int) to add the slot to this signal, and then add debug code
                      2. In the form ui edit, select the pushButton, add slot to the signal 'clicked()' with the debug code

                      check the generated moc_[className].h and see if the connect for cellChanged comes before or after the one for clicked. (it should come before).

                      To have a quick and dirty fix add Qt::QueuedConnection as argument to the connect of the clicked signal

                      Q Offline
                      Q Offline
                      qt_keen_learner
                      wrote on last edited by
                      #15

                      @VRonin
                      Thanks for help.
                      I don't see moc_<class>.h, but moc_<class>.cpp instead.

                      In this file, the slots are not 'connect'. They're dispatched by the id, something like

                      void QtForm::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
                      {
                          if (_c == QMetaObject::InvokeMetaMethod) {
                              Q_ASSERT(staticMetaObject.cast(_o));
                              QtForm*_t = static_cast<QtForm*>(_o);
                              switch (_id) {
                              case 0: _t->on_tableWidget_cellChanged((*reinterpret_cast< int(*)>(_a[1])),(*reinterpret_cast< int(*)>(_a[2]))); break;
                              case 1: _t->on_pushButton_clicked(); break;
                              default: ;
                                      }
                          }
                      }
                      

                      So there is no way to add the quick-and-dirty fix with Qt::QueuedConnection

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

                        sorry, my bad, it's not in moc but in ui_[classname] it's the file uic creates

                        "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

                        Q 1 Reply Last reply
                        1
                        • VRoninV VRonin

                          sorry, my bad, it's not in moc but in ui_[classname] it's the file uic creates

                          Q Offline
                          Q Offline
                          qt_keen_learner
                          wrote on last edited by qt_keen_learner
                          #17

                          @VRonin
                          Thanks for the clarification.
                          I couldn't find any connect call in the ui_<class>.h, either.
                          Something looks like the function required:

                          QMetaObject::connectSlotsByName(QtFrame);
                          

                          This is in the setupUi.

                          mrjjM 1 Reply Last reply
                          0
                          • Q qt_keen_learner

                            @VRonin
                            Thanks for the clarification.
                            I couldn't find any connect call in the ui_<class>.h, either.
                            Something looks like the function required:

                            QMetaObject::connectSlotsByName(QtFrame);
                            

                            This is in the setupUi.

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by mrjj
                            #18

                            @qt_keen_learner
                            Hi , If you use Creator to create/hook up signal and slot then no
                            connect is generated.
                            Its based on auto connect feature where
                            it looks for slots that matches

                            on_ObjectName_SignalName

                            So there is no connects in setupUI() :)

                            Update:
                            To do it yourself, simply rename slot to not follow syntax and
                            you can hook it up after setupUI and append t::QueuedConnection as wanted.

                            Q 1 Reply Last reply
                            2
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #19

                              Can you describe the workflow of that dialog ?

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • mrjjM mrjj

                                @qt_keen_learner
                                Hi , If you use Creator to create/hook up signal and slot then no
                                connect is generated.
                                Its based on auto connect feature where
                                it looks for slots that matches

                                on_ObjectName_SignalName

                                So there is no connects in setupUI() :)

                                Update:
                                To do it yourself, simply rename slot to not follow syntax and
                                you can hook it up after setupUI and append t::QueuedConnection as wanted.

                                Q Offline
                                Q Offline
                                qt_keen_learner
                                wrote on last edited by
                                #20

                                @mrjj
                                Thanks for the update.
                                After learning QueuedConnection, I found it is related with multi-thread programing, but my case is single-thread, so I slightly suspect if this can work.

                                I also tried another workaround:
                                I implemented the slot to 'itemClicked', using a flag to call on_tableWidget_cellChanged. i.e.,
                                when the slot on_tableWidgetItem_cellClicked is called, a bool variable 'toUpdate' is set to true;
                                so then when the slot to button_clicked is called:

                                As shown in the code below, the problem is, if I explicitly call on_tableWidget_cellChanged(prevRow, prevCol), the value is the old value rather than the text I edited and about to take effect!
                                So, this workaround still does not work. There is no way for me to get the new text before the cellChanged signal is sent by the Qt system :-(

                                void QtForm::on_pushButton_clicked() {
                                    if (toUpdate)
                                        this->on_tableWidget_cellChanged(m_nPrevRow, m_nPrevCol);
                                    send(cellData);
                                }
                                
                                void QtForm::on_tableWidget_cellClicked(int row, int col) {
                                    m_nPrevRow = row, m_nPrevCol = col;
                                    toUpdate = true;
                                }
                                
                                void QtForm::on_tableWidget_cellChanged(int row, int col) {
                                    // update cellData
                                    toUpdate = false;
                                }
                                
                                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