Results 1 to 3 of 3

Thread: Dynamic Menu

  1. #1
    Join Date
    Dec 2007
    Posts
    119
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Dynamic Menu

    I am creating a Hierarchical Dynamic Menu, like for a menu bar.
    It creates itself according to what is read from a configuration file.
    So if for example in my configuration file i have this hierarchical structure:

    Vehicles
    --4Wheel
    ----Cars
    --2Wheel
    ----Motorcycles
    ----Bicycle

    My Menu should look like that structure:

    Products
    Vehicles
    --4Wheel
    ----Cars
    --2Wheel
    ----Motorcycles
    ----Bicycle

    All menu items have to be checkable...

    Any good idea how to develop this dynamic menu?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,325
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Dynamic Menu

    Since you don't know in advance what the menu items will be, then you can't define a unique slot that will get executed when the item is triggered.

    In that case, I would make a new QAction instance for every item in the menu, and use the QAction::setData() method to store some kind of value that uniquely identifies each item in the menu tree. Connect all of the QMenu::triggered() signals to the same slot, and in the slot use the QAction pointer and the data value so you can identify which menu item was invoked.

    You need to build the menu recursively (this is pseudo-code):

    Qt Code:
    1. void MyWindow::buildProductMenu( QMenu * parentMenu, MyProductTreeItem * parentItem )
    2. {
    3. QAction * action = new QAction( parentItem->productName(), this );
    4. action->setCheckable( true );
    5. action->setChecked( false );
    6. action->setData( parentItem->productCode() ); // Some unique identifier
    7.  
    8. parentMenu->addAction( action );
    9. connect( parentMenu, SIGNAL( triggered( QAction * ) ), this, SLOT( productMenuActionTriggered( QAction * ) ) );
    10.  
    11. if ( parentItem->hasChildren() ) // create sub menus for each child
    12. {
    13. // pseudocode
    14. for each child Item in parentItem
    15. {
    16. QMenu * childMenu = parentMenu->addMenu( childItem->productName() );
    17. buildProductMenu( childMenu, childItem );
    18. }
    19. }
    20. }
    21.  
    22. void MyWindow::productMenuActionTriggered( QAction * action )
    23. {
    24. QVariant productID = action->data();
    25.  
    26. // Convert QVariant to whatever it should really be, then use that to look up whatever
    27. // product it refers to. Do something appropriate once you know.
    28. }
    To copy to clipboard, switch view to plain text mode 

    Something like that; you'll need to try this and fix whatever I didn't get right. At the topmost level, you call buildProductMenu() with the QMenu pointer corresponding to "Products" and the pointer to the root item in your Products data tree.

    If your product menu changes during the course of executing the program (that is, the product tree itself is dynamic), then you would need to destroy and rebuild the product menu tree whenever the product tree changes.

  3. #3
    Join Date
    Dec 2007
    Posts
    119
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Dynamic Menu

    First of all...thanks for your aid...your solution seems to work fine, only some open questions:
    as you can see from the attached snap, the menu tree has a QMenu and a QAction with the same name (e.g. Wheels_4 or Cars), how can I avoid this problem in order to have only one menu item with a unique name?
    About the menu leaf, in your opinion how can I have only the associated QAction instead of the coupe QMenu and a QAction?

    snap.JPG

    Thanks.

Similar Threads

  1. Replies: 0
    Last Post: 29th March 2012, 19:56
  2. Dynamic menu
    By folibis in forum Qt Programming
    Replies: 1
    Last Post: 25th November 2011, 10:58
  3. Replies: 1
    Last Post: 4th November 2011, 11:25
  4. Replies: 1
    Last Post: 20th January 2011, 17:17
  5. ListView Dynamic Popup menu
    By raphaelf in forum Newbie
    Replies: 3
    Last Post: 14th October 2006, 19:26

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.