Results 1 to 3 of 3

Thread: QTreeView and dynamic context menu

  1. #1
    Join Date
    Jul 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QTreeView and dynamic context menu

    Hello,

    I have a fairly common requirement: provide a QTreeView with context menu depending of what type of element in the tree is selected, some of the actions creating child items in the QTreeView.

    I have therefore set the context menu of my QTreeView to Qt::CustomContextMenu and I am creating the menu and actions in a 'showContextMenu(const QPoint& pnt)' slot. Great so far.

    What seems like a clean design is then to call in this slot a 'createMenu()' method on the model item and via polymorphism or static policies this item class would return the proper menu with proper actions etc. Ok so far.

    Now what makes things cumbersome to create child items is that I have to connect the actions 'triggered' signal to some slot:
    - If the slot is in my specialized item class it is better loose coupling, but it means that to actually add a child to the QTreeView I would need to pass the model or the selection model to the item to call 'beginInsertRows' etc... Weird!
    - If the slot is in my QTreeView derivate class, I would have to have as much slots as all the menu actions required by all the item classes appearing in the QTreeView, which in terms of coupling seems not that good ...
    - If I setup some 'a la visitor pattern' call where a generic 'addChild()' slot in my QTreeView derivative class calls the item 'addChild()' method for specific behavior, I cannot determine which action was triggered except by checking sender of signal against all the actions available ... same coupling as in the previous point.

    If someone has a hint on how to proceed I would be grateful if you could help me.

    Thanks a lot for your answers!

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTreeView and dynamic context menu

    If you use the second solution, I think you could implement this using a single slot, and use QSignalMapper to funnel all of the child signals through one slot. Read up on that to see if it might work.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jul 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default [SOLVED] Re: QTreeView and dynamic context menu

    Hello,

    Thank you, the QSignalMapper trail was useful.

    All in all I implemented a factory for the QMenu objects and the signal mapper dispatches signals to create the right menu.

    Regards,
    Jean-Paul

Similar Threads

  1. Qt Context menu
    By jeyanthinatarajan in forum Newbie
    Replies: 3
    Last Post: 4th April 2012, 22:26
  2. Replies: 1
    Last Post: 4th November 2011, 11:25
  3. Context menu
    By dejvis in forum Newbie
    Replies: 2
    Last Post: 20th September 2009, 22:02
  4. Custom context menu in QTreeView
    By ttvo in forum Qt Programming
    Replies: 5
    Last Post: 3rd April 2009, 22:29
  5. Qwt and context menu
    By giusepped in forum Qwt
    Replies: 1
    Last Post: 9th December 2008, 08:55

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.