Qt checkbox stateChanged event handler

Sta*_*tan 4 checkbox qt visual-studio-2005 callback windows-7

In my Qt application, I want to use a checkbox to do A when it's toggled to unchecked, and do B when toggle to checked. The checkbox is hooked to foo(int).

connect(myCB, SIGNAL(stateChanged(int)), this, SLOT(foo(int)));
Run Code Online (Sandbox Code Playgroud)

在完整性检查失败时出现问题(例如,某些变量得到无效值),我只想显示错误消息并保持所有内容不变.所以我再次切换复选框以将其恢复到原来的位置.但似乎这个动作会再次触发回调函数foo(int),这会弄乱一切.我不希望它在这种情况下触发回调.我应该怎么做?或者,还有更好的方法?请参阅下面的伪代码.

void foo(int checkState)
{
  if (checkState == Qt::Unchecked) {
    if (!passSanityCheck()) {
      // show error message
      checkBoxHandle->toggle();
      return;
    }

    // do A when it's unchecked
  }
  else {
    if (!passSanityCheck()) {
      // show error message
      checkBoxHandle->toggle();
      return;
    }

    // do B when it's checked
  }
  return;
}
Run Code Online (Sandbox Code Playgroud)

thu*_*uga 9

QCheckBox :: clicked(bool checked)信号连接到您的插槽:

QCheckBox *cb = new QCheckBox(this);
connect(cb, SIGNAL(clicked(bool)), this, SLOT(toggled(bool)));
Run Code Online (Sandbox Code Playgroud)

如果你打电话setDown(),setChecked()或不发出此信号toggle().