Results 1 to 4 of 4

Thread: Context Menu on Item Delegate

  1. #1
    Join Date
    Nov 2009
    Posts
    44
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Context Menu on Item Delegate

    I have an Item Delegate for a TreeView that has several widgets inside a frame for editors at each node. One of the widgets is a QTextEdit that needs to display the 'standard' context menu and also a custom item that unhides a docking window. The docking window is defined at the main window level using the ui designer. I've set the contextmenu policy for the QTextEdit widget to Qt::NoContextMenu, and I get the event in the main window in the ContextMenuEvent. Now, I want to interrogate the event to see if it's from my QTextEdit, and if it is, display the standard context menu with my extra item and if not, display another context menu. I'm stuck on finding out which widget received the right-click. I have tried using :
    QTextEdit *txt = qobject_cast<QTextEdit*>( this->childAt( event->globalPos() ) ); and also
    QTextEdit *txt = qobject_cast<QTextEdit*>( this->view->childAt( mapFromGlobal( event->globalPos() ) ) ); and several other combinations. But the cast is returning a 0 pointer every time. I need to find out how to use the globalPos() to get the calling widget... but I don't know how to figure it out. Can anyone help? Or suggest a smarter way to approach the issue? Thanks in advance for any assistance.

  2. #2
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Context Menu on Item Delegate

    Quote Originally Posted by LynneV View Post
    I have an Item Delegate for a TreeView that has several widgets inside a frame for editors at each node. One of the widgets is a QTextEdit that needs to display the 'standard' context menu and also a custom item that unhides a docking window. The docking window is defined at the main window level using the ui designer.
    It might be better to subclass the QTextEdit, re-implementing QTextEdit::contextMenuEvent as described to add an action. If you’re using signals and slots, you could use one of the QMenu::addAction overloads that take a QString, a QObject address and a the name of a slot to have the new menu item signal an appropriate slot in your main window, which could then show the docking window. Alternatively, you could register a custom QEvent and send it to the main window if the call to QMenu::exec returns the action you added.

  3. #3
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Context Menu on Item Delegate

    Quote Originally Posted by LynneV View Post
    I have tried using :
    QTextEdit *txt = qobject_cast<QTextEdit*>( this->childAt( event->globalPos() ) ); and also
    QTextEdit *txt = qobject_cast<QTextEdit*>( this->view->childAt( mapFromGlobal( event->globalPos() ) ) ); and several other combinations. But the cast is returning a 0 pointer every time. I need to find out how to use the globalPos() to get the calling widget.
    This is only a guess, as I haven’t had occasion to use it... but I think QWidget::childAt returns only the immediate child at the given position, not the topmost descendant at that position. You’d have to iterate, translating the global position to the correct widget coordinates for each descendant and working downward until either you either found a QTextEdit or got 0 back from QWidget::childAt.

    But I’m thinking QApplication::widgetAt might give you what you want directly.

  4. The following user says thank you to Coises for this useful post:

    LynneV (12th February 2010)

  5. #4
    Join Date
    Nov 2009
    Posts
    44
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Context Menu on Item Delegate

    Thanks so much for the ideas. I actually got about 95% of the way down your first suggestion...but didn't get to the point of being able to have the slot execute the function. I'm going to try both suggestions and see which one works 'best'. Thanks again

Similar Threads

  1. Context menu
    By dejvis in forum Newbie
    Replies: 2
    Last Post: 20th September 2009, 22:02
  2. Replies: 5
    Last Post: 10th August 2009, 10:50
  3. Qwt and context menu
    By giusepped in forum Qwt
    Replies: 1
    Last Post: 9th December 2008, 08:55
  4. QItemDelegate Context Menu
    By aekilic in forum Qt Programming
    Replies: 16
    Last Post: 3rd December 2008, 09:29
  5. Context Menu
    By RY in forum Newbie
    Replies: 1
    Last Post: 10th September 2008, 07:59

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.