Prevent Firing Signals in Qt

50,508

Solution 1

You can use the clicked signal because it is only emitted when the user actually clicked the check box, not when you manually check it using setChecked.

If you just don't want the signal to be emitted at one specific time, you can use QObject::blockSignals like this:

bool oldState = checkBox->blockSignals(true);
checkBox->setChecked(true);
checkBox->blockSignals(oldState);

The downside of this approach is that all signals will be blocked. But I guess that doesn't really matter in case of a QCheckBox.

Solution 2

You can always block signal emission on QObjects using QObject::blockSignals(). Note that to be correct about things, you should remember the old state (returned from the function call), and restore it when you are done.

At my job, we prefer RAII for this sort of thing. A simple class to do so might look like this:

class SignalBlocker
{
public:
    SignalBlocker( QObject *obj ) : m_obj( obj ), m_old( obj->blockSignals( true ) )
    {
    }

    ~SignalBlocker()
    {
        m_obj->blockSignals( m_old );
    }

private:
    QObject *m_obj;
    bool m_old;
};

Edit: Starting with Qt 5.3, see QSignalBlocker (h/t to HappyCactus in comments)

Solution 3

While learning Qt, I ran into this problem with a set of interconnected widgets that I wanted to update "atomically". I liked @cjhuitt's solution, but found that it goes even better with a bit of syntactic sugar based on proxy objects. Here's the approach that I used...

First, I defined a class template for a blocker proxy object. Like Caleb's, this blocks the signals on construction, and then restores their previous state on destruction. However, it also overloads the -> operator to return a pointer to the blocked object:

template<class T> class Blocker {
    T *blocked;
    bool previous;
public:
    Blocker(T *blocked)
        : blocked(blocked),
          previous(blocked->blockSignals(true)) {}
    ~Blocker() { blocked->blockSignals(previous); }
    T *operator->() { return blocked; }
};

Next, I defined a small template function to construct and return a Blocker:

template<class T> inline Blocker<T> whileBlocking(T *blocked) {
    return Blocker<T>(blocked);
}

Putting this all together, I'd use it like this:

whileBlocking(checkBox)->setChecked(true);

or

whileBlocking(xyzzySpin)->setValue(50);

This gets me all the benefits of RAII, with automatically paired blocking and restore around the method call, but I don't need to name any wrapper or state flags. It's nice, easy, and pretty darn foolproof.

Solution 4

You can QObject::disconnect to remove the corresponding signal-slot connection and can QObject::connect again once you are done...

Solution 5

In QObject derived classes, you can call blockSignals(bool) to prevent the object from emitting signals. So for example:

void customChangeState(bool checked)
{
    blockSignals(true);
    ui->checkBox->setCheckState(Qt::Checked);
    // other work
    blockSignals(false);
}

The above method would change the check state without clicked, stateChanged, or any other signals being emitted.

Share:
50,508
metdos
Author by

metdos

"The spiritual quest for elegance can turn the hacker into an artist."

Updated on July 08, 2022

Comments

  • metdos
    metdos almost 2 years

    We have a QCheckBox object, when user checks it or removes check we want to call a function so we connect our function to stateChanged ( int state ) signal. On the other hand, according to some condition we also change the state of QCheckBox object inside code, and this causes the unwanted signal.

    Is there any way to prevent firing signal under some conditions?