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. How to get the event source of a QMenu which is bind to some QPushButtons?
Forum Updated to NodeBB v4.3 + New Features

How to get the event source of a QMenu which is bind to some QPushButtons?

Scheduled Pinned Locked Moved General and Desktop
17 Posts 6 Posters 8.2k Views 1 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.
  • A Offline
    A Offline
    andywei
    wrote on last edited by
    #1

    I shared a QMenu between some QPushbutton, it's very simple to make the QPushbutton to a menu button. Now I want to adjust the menu according to the button when it's about to show, but how could i know which button fire the menu?

    Thanks for your time.

    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by
      #2

      have a look at the docs : signals and slots

      bq. Advanced Signals and Slots Usage:
      For cases where you may require information on the sender of the signal, Qt provides the QObject::sender() function, which returns a pointer to the object that sent the signal.

      in your case qsignalmapper could be an even better solution.

      Qt Certified Specialist
      www.edalsolutions.be

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andywei
        wrote on last edited by
        #3

        Thank you very much :)

        I'll try it tomorrow.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          If you create a menu button, you don't use signal/slot with custom slots, you attach the menu direct to the push button. So QSignalMapper does not help here.

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andywei
            wrote on last edited by
            #5

            yes, you are right, i use the button's Hasfocus method to indicate it. it's ugly but it works.
            many thanks:)

            1 Reply Last reply
            0
            • G Offline
              G Offline
              garthh
              wrote on last edited by
              #6

              The hasFocus() method doesn't work for me either. Anyone have any more suggestions? Since the QPushButton::setMenu() function exists, someone must know how to use it contextually..

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                If you reuse a QMenu for two buttons, it's supposed to be the same. If this is not true, use two different menus, probably sharing the same actions.

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  I'm not sure I totally agree with that statement. I think it makes sense to want to attach a context to a menu (-action). Considder a context menu for a list of items. The menu contains a set of actions that can operate on the items. I think it makes sense to

                  reuse the same menu and the same actions as implementation for the operations, and

                  wish to know at the action level what the context of the operation is.

                  This case is not that different.

                  I can see a case for having some kind of context for actions, but no ready-made solution for it.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #9

                    Two pushbuttons are static. I just would go the easy way and create two menus. And we're not talking about context menus (these are easy to manipulate via the Qt::ContextMenuPolicy and the customContextMenuRequested signal), but a "regular" button menu.

                    One could try to subclass QPushButton and override slot showMenu. Albeit that one is not virtual by design, all slots are inherently virtual when called via the metacall/signal-slot mechanism. I would not bet that this works out in all cases, though.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      garthh
                      wrote on last edited by
                      #10

                      The problem is, when triggered() is called by a particular menu, whether the same menu is used in two buttons or you have different menus, you cannot work out which QPushButton the triggering menu is attached to.

                      I tried looking at the parent() of the menus, but they are NULL. If I set the parent of them upon creation, then the menus are displayed when the buttons are drawn instead of when clicked.

                      Even doing a pointer comparison between the event's sending QMenu and each QPushButton::menu() don't match.

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        goetz
                        wrote on last edited by
                        #11

                        Maybe you should describe exactly what you want to achieve - not the implementation approach, but what your UI should look and behave like. Maybe we can come up with a less problematic solution.

                        http://www.catb.org/~esr/faqs/smart-questions.html

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          garthh
                          wrote on last edited by
                          #12

                          Sure.

                          I've got a bunch of buttons that, when clicked, display the same menu:
                          "Copy URL"
                          "Play"

                          When the event occurs, the menu enacts one of those methods, contextual to the button it's dropping down from.

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            goetz
                            wrote on last edited by
                            #13

                            The number of buttons is fixed?

                            http://www.catb.org/~esr/faqs/smart-questions.html

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              garthh
                              wrote on last edited by
                              #14

                              At the moment.

                              1 Reply Last reply
                              0
                              • G Offline
                                G Offline
                                goetz
                                wrote on last edited by
                                #15

                                So, when an action of the menu fires, the connected slot should use the URL associated with that button, right?

                                If you still do not want to use separate menus and actions - which I think is the easiest and less problematic approach - you might want to connect to the pressed() signal of the buttons (use a [[Doc::QSignalMapper]] to associate an id or the like). I don't know if that works reliably in all and every corner cases (activation with the keyboard instead of the mouse comes into mind).

                                http://www.catb.org/~esr/faqs/smart-questions.html

                                1 Reply Last reply
                                0
                                • G Offline
                                  G Offline
                                  garthh
                                  wrote on last edited by
                                  #16

                                  Thanks for all your help. I've resorted to hiding the URL text within the menu object using QObject::setProperty() and pulling it out again in the event. A bit of a fudge, but works.

                                  1 Reply Last reply
                                  0
                                  • A Offline
                                    A Offline
                                    andre
                                    wrote on last edited by
                                    #17

                                    You could considder an alternative approach:
                                    How about you subclass QPushButton. In this custom class, you can attach the menu already. Then, you give your button a set of signals corresponding with the items in the menu. You can give these signals an identifier argument to identify the instance of the button.

                                    That way, you will have no need to fiddle with sender() or with QSignalMapper.

                                    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