Qt checkbox stateChanged event handler

In my Qt application, I want to use a checkbox to make A when it switches to unchecked, and do B when switching to check. The checkbox is bound to foo (int).

connect(myCB, SIGNAL(stateChanged(int)), this, SLOT(foo(int))); 

There is a problem when the health check does not work (for example, some variable received invalid values), I just want to show the error message and stay the same. So I switch the checkbox again to get it back to where it was. But it looks like this action will call the foo (int) callback function again, which will ruin everything. I do not want him to call a callback in this situation. How should I do it? Or is there a better way? See pseudo code below.

 void foo(int checkState) { if (checkState == Qt::Unchecked) { if (!passSanityCheck()) { // show error message checkBoxHandle->toggle(); return; } // do A when it unchecked } else { if (!passSanityCheck()) { // show error message checkBoxHandle->toggle(); return; } // do B when it checked } return; } 
+4
source share
1 answer

Connect QCheckBox :: clicked (bool checked) to your slot:

 QCheckBox *cb = new QCheckBox(this); connect(cb, SIGNAL(clicked(bool)), this, SLOT(toggled(bool))); 

This signal is not emitted if you call setDown() , setChecked() or toggle() .

+7
source

Source: https://habr.com/ru/post/1500898/


All Articles