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.3k 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
    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