Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved QPushButton::clicked(bool) use

    General and Desktop
    4
    5
    6175
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ivanicy
      ivanicy last edited by

      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!

      JonB 1 Reply Last reply Reply Quote 0
      • M
        mostefa last edited by mostefa

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

        1 Reply Last reply Reply Quote 5
        • JonB
          JonB @ivanicy last edited by

          @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?!

          1 Reply Last reply Reply Quote 3
          • VRonin
            VRonin last edited by

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

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply Reply Quote 5
            • ivanicy
              ivanicy last edited by

              Thank you very much, I forgot this detail!!

              1 Reply Last reply Reply Quote 0
              • First post
                Last post