Search code examples
qtmouseeventqradiobutton

Dragging a radio button performes the action but doesn't set the check


I am creating an application that should work on desktop and some mobile platforms.

The following example creates and connects my portrait/landscape buttons, in a group, to a slot, on the release signal.

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)));

But I found a weird situation:

Assume landscape button is checked. If I press and drag away from the portrait radio button, the slot action is performed (for the portrait option) but the portrait button is not checked.

I would like the action not to be performed.

For now...

In the orientationSlot I test the argument and set the checked value myself... Though I really expected the buttons to know to do that themselves.

But I think it is more expected by users that, if the press a button and change their mind, to be able to drag away from the button and not have the action be performed.


Solution

  • I can handle verifying if the check really happened in the action slot, and either check or discard the action depending on how I will think the user experience is better...

    If I want the buttons to be checked and to perform the action as well:

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

    If I want the action not to be performed when the user drags away from the button (my preferred option):

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