QPushButton::clicked(bool) use



  • Hello!!

    I know this is a silly question but, I can't use correctly the QPushButton::clicked(bool) slot. I have a QMenu associated to this button. This is my code:

    void MainWindow::on_upDownButton_clicked(bool checked)
    {
        if (checked) {
            menuUpDown->hide();
        } else {
            menuUpDown->show(); // <-- trick to force layout of the menu before height() is called
            menuUpDown->popup(mapToGlobal(ui->upDownButton->pos() - QPoint(0, menuUpDown->height())));
        }
    }
    

    But here, checked is always false, so, I can't hide the QMenu.

    What's wrong here?

    Thank you very much!



  • @ivanicy

    It's normal when you click on your QPushbutton to have always :

    clicked = true on your button.

    Refer to this doc

    If the button is checkable, checked is true if the button is checked, or false if the button is unchecked.

    If you want have checkable button you need to add this line:

    yourButton->setCheckable(true);



  • @ivanicy
    http://doc.qt.io/qt-5/qabstractbutton.html#clicked:

    If the button is checkable, checked is true if the button is checked, or false if the button is unchecked.

    Is your QPushButton even a checkable one?!



  • Slightly unrelated: you don't need to do it manually: http://doc.qt.io/qt-5/qpushbutton.html#setMenu



  • Thank you very much, I forgot this detail!!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.