Results 1 to 6 of 6

Thread: Determine a menu's caller, multiple buttons same menu (updated)

  1. #1
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Determine a menu's caller, multiple buttons same menu (updated)

    Update: I had mis-typed part of my problem description. This is now corrected.

    Consider:
    Qt Code:
    1. //some container class
    2. QPushButton *a,*b,*c;
    3. //initialization code
    4. QAction *superCoolAction; //action initialized to release some signal, SCA_Signal
    5.  
    6. QMenu *sharedMenu;
    7. //initialization code contains superCoolAction, it's parent is this container class
    8.  
    9. a->addMenu(sharedMenu);
    10. b->addMenu(sharedMenu);
    11. c->addMenu(sharedMenu);
    To copy to clipboard, switch view to plain text mode 

    Now, when someone clicks on any of these buttons it will open the menu; and they may click the action (which will emit SCA_Signal), but I'd like this signal to contain data telling me which button released it. Boiling this question down further, I'm curious if there's a "who called me" style function I can use on menus.

    I ask because I know you can do this with Signals, but in that case it would just tell me the "menu" called it I believe. I've considered odd implementations such as releasing a signal when the button is clicked to "pre-empt" the next signal so I can bypass this issue; but that causes a coupling issue. I could instead prep the container class to catch SCA_Signal and clean it up, emiting a similar signal with the necessary information about who called it; but these both feel klugey.

    What's the best way to know which button opened a menu if it's a menu that is used on several buttons?
    Last edited by tescrin; 26th November 2012 at 21:33.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Determine a menu's caller, multiple buttons same menu (updated)

    the best way is to redeisgn so actions aren't dependent on things that they should be agnostic of. I mean children should care about parents, and slots shouldnt care about callers. See 'dependency injection/inversion'. If your action needs to know the caller in order to work correctly, then your action doesnt have enough information to begin with.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Determine a menu's caller, multiple buttons same menu (updated)

    I'll see what I can do on that front. It'd be easy enough to make multiples of the menu; but I had figured making 5 more menus (it has 4 submenus..) and their actions for each button would be needless clutter if I could pass the caller as an argument.

    That said, sharing the menu is a shortcut and will likely just bite me elsewhere. *grumble grumble*
    Thanks on the advice

  4. #4
    Join Date
    Feb 2010
    Posts
    96
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Determine a menu's caller, multiple buttons same menu (updated)

    you can find the sender. I do this in PyQt. Here is an example of a rotation widget that has a slider and a spinbox. They both send their data to the same method and I find the sender so I don't create a recursive error
    Qt Code:
    1. def rotateValue(self, rotate):
    2. if self.mapPage.sender() == self.rotateMenu.slider:
    3. if not self.rotateMenu.setOnce:
    4. self.rotateMenu.spinbox.setValue(rotate)
    5. self.rotateMenu.setOnce = True
    6. if self.mapPage.sender() == self.rotateMenu.spinbox:
    7. if not self.rotateMenu.setOnce:
    8. self.rotateMenu.slider.setValue(rotate)
    9. self.rotateMenu.setOnce = True
    10. self.rotateMenu.setOnce = False
    11. self.itemOffset(rotate); self.setRotation(rotate);
    12. self.mapGuide.updateTable()
    13. self.Rotation = rotate
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Determine a menu's caller, multiple buttons same menu (updated)

    that's all very well and good, but the problem is that the 'sender' the OP is interested in the 'creator' of the menu. The menu will be the 'sender' and this isn't what he wants. In fact that OP says this all in his first post.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Determine a menu's caller, multiple buttons same menu (updated)

    Quote Originally Posted by tescrin View Post
    What's the best way to know which button opened a menu if it's a menu that is used on several buttons?
    If all elses fail, set a dynamic property on the menu (or each of the actions) that will point to the object that created the menu.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 1
    Last Post: 18th May 2012, 11:36
  2. Replies: 2
    Last Post: 11th May 2012, 10:38
  3. Replies: 1
    Last Post: 4th November 2011, 11:25
  4. Contex menu appear multiple times
    By kubas in forum Qt Programming
    Replies: 2
    Last Post: 7th September 2009, 14:55
  5. Replies: 2
    Last Post: 27th June 2008, 19:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.