Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPushButton::clicked(bool) use
QtWS25 Last Chance

QPushButton::clicked(bool) use

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 7.9k Views
  • 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.
  • ivanicyI Offline
    ivanicyI Offline
    ivanicy
    wrote on last edited by
    #1

    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!

    JonBJ 1 Reply Last reply
    0
    • M Offline
      M Offline
      mostefa
      wrote on last edited by mostefa
      #2

      @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
      5
      • ivanicyI ivanicy

        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!

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @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
        3
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          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
          5
          • ivanicyI Offline
            ivanicyI Offline
            ivanicy
            wrote on last edited by
            #5

            Thank you very much, I forgot this detail!!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved