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 loses its click event due to the set menu
Forum Updated to NodeBB v4.3 + New Features

QPushbutton loses its click event due to the set menu

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 1.3k Views 2 Watching
  • 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.
  • KaguroK Offline
    KaguroK Offline
    Kaguro
    wrote on last edited by
    #1

    Hi Guys!
    I have a QPushbutton and i added a Qmenu to it. But this QPushbutton has a clicked event. When i clicked the button the menu is working but the event is not.
    My code:

        QPushButton *pBackgroundImg= new QPushButton("Things");
        pBackgroundImg->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    
        QAction *a1 = new QAction("1 item");
        QAction *a2 = new QAction("2 item");
        QAction *a3 = new QAction("3 item");
    
        QMenu *menu = new QMenu;
        menu->addAction(a1);
        menu->addAction(a2);
        menu->addAction(a3);
        menu->setNoReplayFor(pBackgroundImg);
    
        pBackgroundImg->setMenu(menu);
    
        connect(pBackgroundImg,&QPushButton::clicked, [=] { pBackgroundImgClicked(); });
    

    Any ideas?

    JonBJ Pl45m4P 2 Replies Last reply
    0
    • KaguroK Kaguro

      Hi Guys!
      I have a QPushbutton and i added a Qmenu to it. But this QPushbutton has a clicked event. When i clicked the button the menu is working but the event is not.
      My code:

          QPushButton *pBackgroundImg= new QPushButton("Things");
          pBackgroundImg->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
      
          QAction *a1 = new QAction("1 item");
          QAction *a2 = new QAction("2 item");
          QAction *a3 = new QAction("3 item");
      
          QMenu *menu = new QMenu;
          menu->addAction(a1);
          menu->addAction(a2);
          menu->addAction(a3);
          menu->setNoReplayFor(pBackgroundImg);
      
          pBackgroundImg->setMenu(menu);
      
          connect(pBackgroundImg,&QPushButton::clicked, [=] { pBackgroundImgClicked(); });
      

      Any ideas?

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

      @Kaguro
      I guess it's an either-or: either you have a button with no menu actions and it has a clicked, or it has menu actions in which case the click is swallowed to open the menu.

      Why do you want the button click as well as the actions anyway? If you really, really have to, and you don't get a better answer, you might still be able to find the click via an eventFilter on something like the pushbutton, worth a try.

      1 Reply Last reply
      3
      • KaguroK Kaguro

        Hi Guys!
        I have a QPushbutton and i added a Qmenu to it. But this QPushbutton has a clicked event. When i clicked the button the menu is working but the event is not.
        My code:

            QPushButton *pBackgroundImg= new QPushButton("Things");
            pBackgroundImg->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        
            QAction *a1 = new QAction("1 item");
            QAction *a2 = new QAction("2 item");
            QAction *a3 = new QAction("3 item");
        
            QMenu *menu = new QMenu;
            menu->addAction(a1);
            menu->addAction(a2);
            menu->addAction(a3);
            menu->setNoReplayFor(pBackgroundImg);
        
            pBackgroundImg->setMenu(menu);
        
            connect(pBackgroundImg,&QPushButton::clicked, [=] { pBackgroundImgClicked(); });
        

        Any ideas?

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #3

        @Kaguro

        The click on your button gets intercepted and opens your QMenu.
        Your QPushButton is still a QPushButton but kinda lost the push-button behavior since it's now a menu button.

        This turns the button into a menu button, which in some styles will produce a small triangle to the right of the button's text.

        (https://doc.qt.io/qt-5/qpushbutton.html#setMenu)


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        KaguroK 1 Reply Last reply
        2
        • Pl45m4P Pl45m4

          @Kaguro

          The click on your button gets intercepted and opens your QMenu.
          Your QPushButton is still a QPushButton but kinda lost the push-button behavior since it's now a menu button.

          This turns the button into a menu button, which in some styles will produce a small triangle to the right of the button's text.

          (https://doc.qt.io/qt-5/qpushbutton.html#setMenu)

          KaguroK Offline
          KaguroK Offline
          Kaguro
          wrote on last edited by
          #4

          @Pl45m4 said in QPushbutton loses its click event due to the set menu:

          The click on your button gets intercepted and opens your QMenu.
          Your QPushButton is still a QPushButton but kinda lost the push-button behavior since it's now a menu button.

          Oh i see! TY for your help! :)

          Pl45m4P 1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            Well it would be sort of confusing to the user if it still did its Clicked thing
            as it would then open the menu, indicating its waiting for user input but
            at the same did also did something in the background.

            KaguroK 1 Reply Last reply
            2
            • KaguroK Kaguro

              @Pl45m4 said in QPushbutton loses its click event due to the set menu:

              The click on your button gets intercepted and opens your QMenu.
              Your QPushButton is still a QPushButton but kinda lost the push-button behavior since it's now a menu button.

              Oh i see! TY for your help! :)

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by
              #6

              @Kaguro

              Try this:

                     connect(menu, &QMenu::aboutToShow, [=](){
                         pBackgroundImgClicked();
                     });
              

              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              KaguroK 1 Reply Last reply
              2
              • mrjjM mrjj

                Hi
                Well it would be sort of confusing to the user if it still did its Clicked thing
                as it would then open the menu, indicating its waiting for user input but
                at the same did also did something in the background.

                KaguroK Offline
                KaguroK Offline
                Kaguro
                wrote on last edited by
                #7

                @mrjj Yeah i understend it you are right. Other Question for this menu: How can i trigger the menu (drop down) with mouse hover event? Because I can only activate it if I click on it. Ty for your and others help! :)

                1 Reply Last reply
                0
                • Pl45m4P Pl45m4

                  @Kaguro

                  Try this:

                         connect(menu, &QMenu::aboutToShow, [=](){
                             pBackgroundImgClicked();
                         });
                  
                  KaguroK Offline
                  KaguroK Offline
                  Kaguro
                  wrote on last edited by
                  #8

                  @Pl45m4 said in QPushbutton loses its click event due to the set menu:

                  connect(menu, &QMenu::aboutToShow, ={
                  pBackgroundImgClicked();
                  });

                  haha and it works! You are my hero!

                  Pl45m4P 1 Reply Last reply
                  0
                  • KaguroK Kaguro

                    @Pl45m4 said in QPushbutton loses its click event due to the set menu:

                    connect(menu, &QMenu::aboutToShow, ={
                    pBackgroundImgClicked();
                    });

                    haha and it works! You are my hero!

                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by
                    #9

                    @Kaguro

                    Nice, so mark this topic as solved :)


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    1 Reply Last reply
                    1

                    • Login

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