0

我正在创建一个可以在桌面和一些移动平台上运行的应用程序。

以下示例在释放信号上创建一组纵向/横向按钮并将其连接到插槽。

m_landscapeRadio = new QRadioButton(QObject::tr("Landscape "));
m_portraitRadio = new QRadioButton(QObject::tr("Portrait "));
m_orientationGroup.addButton(m_landscapeRadio, 0);
m_orientationGroup.addButton(m_portraitRadio, 1);
m_orientationGroup.setExclusive(true);
m_landscapeRadio->setChecked(true);
connect(&m_orientationGroup, SIGNAL(buttonReleased(int)), this, SLOT(orientationSlot(int)));

但是我发现了一个奇怪的情况:

假设横向按钮被选中。如果我按下并拖离纵向单选按钮,则会执行插槽操作(对于纵向选项),但未选中纵向按钮。

我希望不执行该操作。

目前...

orientationSlot我测试参数并自己设置检查值......虽然我真的希望按钮知道自己这样做。

但我认为用户更期望的是,如果按下按钮并改变主意,能够拖离按钮而不执行操作。

4

2 回答 2

1

我可以处理验证检查是否真的发生在操作槽中,并根据我认为用户体验更好的方式检查或丢弃操作...

如果我想检查按钮并执行操作:

void MyWidget::orientationSlot(int checked)
{
    if(checked) m_portraitRadio->setChecked(true);
    else        m_landscapeRadio->setChecked(true);

    .... actual actions
}

如果我希望在用户拖离按钮时不执行该操作(我的首选选项):

void MyWidget::orientationSlot(int checked)
{
    if(m_orientationGroup.checkedId() != checked) return;

    .... actual actions
}
于 2016-07-12T23:06:13.060 回答
0

我使用 QRadioButton 并处理鼠标按钮被释放事件,以对正在切换的单选按钮做出反应。它会导致拖动事件完全出现问题。我想要么检查按钮,要么不执行操作。

http://doc.qt.io/qt-5/qradiobutton.html

每当打开或关闭按钮时,它都会发出 toggled() 信号。如果您想在每次按钮更改状态时触发操作,请连接到此信号。使用 isChecked() 查看是否选择了特定按钮。

您将单选按钮显式连接到处理程序或整个组:http ://doc.qt.io/qt-5/qbuttongroup.html#buttonToggled

void QButtonGroup::buttonToggled(QAbstractButton *button, bool 检查)

此信号在给定按钮被切换时发出。如果按钮被选中,checked 为真,如果按钮未被选中,则为假。

注意:信号 buttonToggled 在此类中被重载。要使用函数指针语法连接到这个,您必须在静态转换中指定信号类型,如下例所示:

connect(buttonGroup, static_cast<void(QButtonGroup::*)
      (QAbstractButton *, bool)>(&QButtonGroup::buttonToggled),
    [=](QAbstractButton *button, bool checked) {

     if (button == m_portraitRadio) {
      // Portrait (un)checked
      if (checked)
      {
         // checked!
      }
     }
     /* ... */ });
于 2016-07-12T22:47:16.913 回答