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. QTableWidget still trigger itemChanged event even if signals blocked
Forum Updated to NodeBB v4.3 + New Features

QTableWidget still trigger itemChanged event even if signals blocked

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 1.4k 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.
  • SabracS Offline
    SabracS Offline
    Sabrac
    wrote on last edited by
    #1

    Hi guys,
    As title, i'm facing with the problem that i have blocked all signals of QTableWidget but the setItem function still trigger itemChanged event.

    Is there anyway to prevent itemChanged trigger?

    I just know one solution is wrap before and after setItem function by blockSignals(true) again.
    But its very annoying if my table have too much cell to set.

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Please show how you're using block signals and your connect statement.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      SabracS 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        Please show how you're using block signals and your connect statement.

        SabracS Offline
        SabracS Offline
        Sabrac
        wrote on last edited by
        #3

        @Christian-Ehrlicher said in QTableWidget still trigger itemChanged event even if signals blocked:

        Please show how you're using block signals and your connect statement.

        Hi christ,
        i just block signals like normal

        table->blockSignals(true);
        // ... Some other logics
        table->setItem(0, 0, value0);
        table->setItem(0, 1, value1);
        table->setItem(0, 2, value2);
        table->setItem(0, 3, value3);
        // ... Some other logics
        table->blockSignals(false);
        

        And i using qt designer to create connect (by right click widget and then choose "go to slot..."

        JoeCFDJ 1 Reply Last reply
        0
        • S Offline
          S Offline
          stryga42
          wrote on last edited by
          #4

          Are there any other threads in your app which could un-block the table object in the meantime?

          1 Reply Last reply
          0
          • Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Works fine for me (Qt6):

            #include <QtWidgets>
            
            int main(int argc, char **argv)
            {
              QApplication app(argc, argv);
              bool blockSignals = argc > 1;
              QTableWidget tw;
              tw.show();
              tw.setColumnCount(1);
              tw.setRowCount(1);
              QObject::connect(&tw, &QTableWidget::itemChanged, [&]() { qDebug() << "Item changed!"; });
              QTimer::singleShot(10, &tw, [&]() {
                if (blockSignals)
                  tw.blockSignals(true);
                tw.setItem(0,  0, new QTableWidgetItem("Blub"));
                if (blockSignals)
                  tw.blockSignals(false);
              });
              return app.exec();
            }
            

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            2
            • SabracS Sabrac

              @Christian-Ehrlicher said in QTableWidget still trigger itemChanged event even if signals blocked:

              Please show how you're using block signals and your connect statement.

              Hi christ,
              i just block signals like normal

              table->blockSignals(true);
              // ... Some other logics
              table->setItem(0, 0, value0);
              table->setItem(0, 1, value1);
              table->setItem(0, 2, value2);
              table->setItem(0, 3, value3);
              // ... Some other logics
              table->blockSignals(false);
              

              And i using qt designer to create connect (by right click widget and then choose "go to slot..."

              JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by JoeCFD
              #6

              @Sabrac
              there is a return value from blockSignals(true). Check it out. The return value is the previous value of signalsBlocked().
              https://doc.qt.io/qt-5.15/qobject.html#blockSignals

              There is another way to avoid any unnecessary changes caused by a specific signal.

              disconnect( table, signal, this, slot);
              // ... Some other logics
              table->setItem(0, 0, value0);
              table->setItem(0, 1, value1);
              table->setItem(0, 2, value2);
              table->setItem(0, 3, value3);
              // ... Some other logics
               connect( table, signal, this, slot);
              

              This may work better sometimes since you may not want to block all signals.

              1 Reply Last reply
              2

              • Login

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