Important: Please read the Qt Code of Conduct -

buttons to enable if conditions

  • Hi all,

    I have two conditions inside a method. I also have two Radio Buttons, buttonOne and buttonTwo.

    My requirement is if I click buttonOne AND the other conditions are true , eventOne is true, else if I click buttonTwo and other conditons are true eventTwo is true

    void MainWindow::mousePressEvent(QMouseEvent *event)
        QPoint a = event->pos();
       if (distance ( a,b) < 20 && ( a.x() > b.x()  && a.x() < c.x() )) {
           eventOne = true;
        else if (distance ( a,e) < 20 && ( a.x() > e.x()  &&  a.x() < f.x() )) {
            eventTwo = true;

    How do I implement this?

    Thank you

  • Lifetime Qt Champion

    But MainWindow is not involved in clicking on the radio buttons as such.
    Radio Buttons has their own signal for that and you connect that to a slot to handle that.
    MousePress for MainWindow will not fire when you click on other widgets. Only when you click on MainWindow directly.

    But in mousePressEvent you can check their values of cause. so if u click first radio and then MainWidnow, you can check it there.

  • @mrjj Thank you.
    What confusing me is how to check radiobutton value inside mousePressEvent. Could you show me an example?

  • Lifetime Qt Champion

    Its just
    bool checked = ui->radioButtonName->isChecked();

    But you must click it first then on MainWin

  • @viniltc

    Instead of using the MouseClick-event, you can add both of your RadioButtons to a QButtonGroup, connect the QButtonGroup::buttonToggled signal to your function (check if your other condition is true) and check which RadioButton was "clicked" / toggled (has the "dot").

    QButtonGroup *btnGrp = new QButtonGroup(this);
    btnGrp->addButton(radioBtn_1, 0);
    btnGrp->addButton(radioBtn_2, 1);
    void (QButtonGroup::*signal)(int, bool) = &QButtonGroup::buttonToggled; // Send rButton ID and state (true / false) with signal
    connect(btnGrp, signal, this, &MainWindow::YOUR_SLOT);

    Your slot could look like this:

    void MainWindow::checkCondition(int id, bool checked)
        if(checked && condition) // bool condition, if your other condition (calculation thing) is true
            case 0:
                // RadioBtn 1 + Condition = true
            case 1:
                // RadioBtn 2 + Condition = true

  • Thanks a lot for your feedback @mrjj , @Pl45m4

Log in to reply