拖动单选按钮执行操作但不设置检查
Dragging a radio button performes the action but doesn't set the check
我正在创建一个可以在桌面和一些移动平台上运行的应用程序。
以下示例在释放信号上创建我的 portrait/landscape 按钮并将其连接到一个插槽。
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
中,我测试参数并自己设置检查值...虽然我真的希望按钮知道自己这样做。
但我认为用户更期望的是,如果按下按钮并改变主意,能够拖离按钮而不执行操作。
I use QRadioButton and handle mouse button being released event for reacting
on radio button being switched. It causes problems altogether with dragging event. I would like to either get the button
to be checked, or the action not to be performed.
http://doc.qt.io/qt-5/qradiobutton.html
Whenever a button is switched on or off, it emits the toggled()
signal. Connect to this signal if you want to trigger an action each
time the button changes state. Use isChecked() to see if a particular
button is selected.
将单选按钮显式连接到处理程序或整个组:http://doc.qt.io/qt-5/qbuttongroup.html#buttonToggled
void QButtonGroup::buttonToggled(QAbstractButton *button, bool
checked)
This signal is emitted when the given button is toggled. checked is
true if the button is checked, or false if the button is unchecked.
Note: Signal buttonToggled is overloaded in this class. To connect to
this one using the function pointer syntax, you must specify the
signal type in a static cast, as shown in this example:
connect(buttonGroup, static_cast<void(QButtonGroup::*)
(QAbstractButton *, bool)>(&QButtonGroup::buttonToggled),
[=](QAbstractButton *button, bool checked) {
if (button == m_portraitRadio) {
// Portrait (un)checked
if (checked)
{
// checked!
}
}
/* ... */ });
我可以处理验证检查是否真的发生在操作槽中,并根据我认为用户体验更好的方式检查或丢弃操作...
如果我想要检查按钮并执行操作:
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
}
我正在创建一个可以在桌面和一些移动平台上运行的应用程序。
以下示例在释放信号上创建我的 portrait/landscape 按钮并将其连接到一个插槽。
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
中,我测试参数并自己设置检查值...虽然我真的希望按钮知道自己这样做。
但我认为用户更期望的是,如果按下按钮并改变主意,能够拖离按钮而不执行操作。
I use QRadioButton and handle mouse button being released event for reacting on radio button being switched. It causes problems altogether with dragging event. I would like to either get the button to be checked, or the action not to be performed.
http://doc.qt.io/qt-5/qradiobutton.html
Whenever a button is switched on or off, it emits the toggled() signal. Connect to this signal if you want to trigger an action each time the button changes state. Use isChecked() to see if a particular button is selected.
将单选按钮显式连接到处理程序或整个组:http://doc.qt.io/qt-5/qbuttongroup.html#buttonToggled
void QButtonGroup::buttonToggled(QAbstractButton *button, bool checked)
This signal is emitted when the given button is toggled. checked is true if the button is checked, or false if the button is unchecked.
Note: Signal buttonToggled is overloaded in this class. To connect to this one using the function pointer syntax, you must specify the signal type in a static cast, as shown in this example:
connect(buttonGroup, static_cast<void(QButtonGroup::*)
(QAbstractButton *, bool)>(&QButtonGroup::buttonToggled),
[=](QAbstractButton *button, bool checked) {
if (button == m_portraitRadio) {
// Portrait (un)checked
if (checked)
{
// checked!
}
}
/* ... */ });
我可以处理验证检查是否真的发生在操作槽中,并根据我认为用户体验更好的方式检查或丢弃操作...
如果我想要检查按钮并执行操作:
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
}