QPushButton::clicked(bool) use
Solved
General and Desktop
-
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!
-
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);
-
Slightly unrelated: you don't need to do it manually: http://doc.qt.io/qt-5/qpushbutton.html#setMenu