Results 1 to 2 of 2

Thread: Mapping menu actions to handlers

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2010
    Posts
    99
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3

    Default Mapping menu actions to handlers

    This is sort of a Qt specific question, but mainly i am just having trouble with the design of my menu system. I want to have context menus on items in a tree view, where each menu item (QAction to be more specific) may operate on the object that tree item represents or on the item itself. For example, one action is to recursively expand all sub items of the selected item, whereas another action is to view the properties of the object.

    If there were only one menu then that would be fine. I'd just connect the signals to slots like you normally would. But i also want to use context menus elsewhere, such as in a list of objects in the search results dialog. The menu here should also have actions on the selected object, but it would not make sense to have the "expand all" action because this list is not a tree. But instead, another useful action would be to locate and highlight that object in the tree view (which is on my main window).

    So the problem is that i don't want duplicate code to handle the action events everywhere i use a context menu. And sometimes i may not even use a menu but would have buttons on a toolbar (which also use QActions). Yet i need to have actions specific to each menu/toolbar.

    I tried to make an ActionManager class which has a function for each action and has a mapping of QActions to function pointers so that i can call the appropriate function when the menu item is selected. But i realized that wouldn't work for the "expand all" action since it would need a reference to the tree item. I need to use some sort of signal/slot mechanism, possible QSignalMapper, but i am not sure how.

    The source code in question is spread out over numerous classes and it's hard to pull out the code specific to this question. I have uploaded my source at SourceForge, here.
    The main classes in question are:
    /ui/ActionManager.h (and .cpp)
    /ui/MainWindow.h (and .cpp)
    /ui/custom_widgets/WindowTree.h (and .cpp)

    The WindowTree widget is used both as a tree view in the main window and also as a list (just a tree with no child nodes) in the search results window. These two uses need menus which have mostly the same actions (on the actual object the tree/list holds) but have some different actions.


    Any help would be appreciated. thanks
    Last edited by xtal256; 24th August 2010 at 01:02.
    [Window Detective] - Windows UI spy utility
    [War Thunder]

Similar Threads

  1. Adding actions to a context menu
    By aarelovich in forum Qt Programming
    Replies: 4
    Last Post: 7th December 2011, 13:47
  2. mapping menu item with QPushButton
    By navi1084 in forum Qt Programming
    Replies: 2
    Last Post: 15th October 2010, 22:56
  3. Menu Actions
    By GrahamLabdon in forum Newbie
    Replies: 9
    Last Post: 16th June 2010, 01:41
  4. Toolbar/menu actions with radio behavior?
    By brcain in forum Qt Programming
    Replies: 1
    Last Post: 15th January 2010, 23:10
  5. menu actions calling a single slot
    By jayw710 in forum Qt Programming
    Replies: 2
    Last Post: 28th June 2007, 20:19

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
  •  
Qt is a trademark of The Qt Company.