Home > Back-end >  Radio button moving cyclically
Radio button moving cyclically

Time:11-12

Can anyone please help on below requirement, how to implement.

When I click on radio buttons with keyboard up and down arrows it is moving up and down normally .

But I need ,when we click on propulsion button and then if I press down arrow radio button should go to off. And when we are on off, if we press up arrow rado button should go to propulsion.enter image description here

CodePudding user response:

here is a minimal Example on how you can specify navigation with arrowkeys like you want it, you can use the "KeyNavigation.X" property to tell the next focus item when you press the defined key. - hope this helps:

   Column {
    Label {
        text: qsTr("Radio:")
    }

    RadioButton {
        id: test1
        checked: true
        text: qsTr("DAB")
        KeyNavigation.down: test2
        KeyNavigation.up: test3
    }

    RadioButton {
        id:test2
        text: qsTr("FM")
        KeyNavigation.down: test3
        KeyNavigation.up: test1
    }

    RadioButton {
        id:test3
        text: qsTr("AM")
        KeyNavigation.down: test1
        KeyNavigation.up: test2
    }
}

CodePudding user response:

Implemented the above using below code

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if(obj == ui->mGbPowerModes)
    {
        if(event->type() == QEvent::KeyPress)
        {
            QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);

            if(keyEvent->key() == Qt::Key_Up)
            {
                if(ui->mBtnOff->isChecked())
                {
                    on_mBtnPropulsion_clicked();
                    ui->mBtnPropulsion->setFocus();
                    ui->mBtnPropulsion->setChecked(true);
                }
                return true;
            }
            else if(keyEvent->key() == Qt::Key_Down)
            {
                if(ui->mBtnPropulsion->isChecked())
                {
                    on_mBtnOff_clicked();
                    ui->mBtnOff->setFocus();
                    ui->mBtnOff->setChecked(true);
                }
                return true;
            }
        }
        return false;
    }
    return MainWindow::eventFilter(obj, event);
}
  • Related