Results 1 to 8 of 8

Thread: QMenu::addAction ownership

  1. #1
    Join Date
    Jul 2012
    Posts
    223
    Thanks
    18
    Thanked 14 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QMenu::addAction ownership

    Hi,

    In a widget of mine, I create a contextmenu like this


    Qt Code:
    1. QMenu menu(this);
    2. QAction* act = menu.addAction("menuaction");
    3. act->setStatusTip("fff");
    4. ...
    To copy to clipboard, switch view to plain text mode 


    But! I realized the statusTip doesnt work. That's because the QAction needs to be a direct child of the QMainWindow containing the statusBar, as far as I can tell. And indeed, if I add act->setParent(my_main_window), then the statusTip works!


    But now I am wondering if I leak the QActions. The documentation says "QMenu takes ownership of the returned QAction." But is this ownership expressed in a qobject-child-parent relationship, or does the menu explicitly delete the action?

    Does the QMenu still own the QAction when I call setParent(my_main_window)?

    As far as I can tell this is indeed the case.


    Second question: Is it possible to add a statusTip to a submenu?


    Qt Code:
    1. QMenu menu(this);
    2. QMenu* submenu = menu.addMenu("submenue");
    3. QAction* act = submenu->addAction("menuaction");
    4.  
    5. submenu->setStatusTip("fff"); //doesnt work!
    6. ...
    To copy to clipboard, switch view to plain text mode 


    It doesnt work, even if I force its parent to my mainwindow (which is not possible bc it messes with the submenues location).

    Any way to accomplish this?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,294
    Thanks
    36
    Thanked 1,521 Times in 1,472 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMenu::addAction ownership

    Quote Originally Posted by tuli View Post
    But is this ownership expressed in a qobject-child-parent relationship, or does the menu explicitly delete the action?
    As far as I can tell only by QObject relationship
    https://code.woboq.org/qt5/qtbase/sr...ionERK7QString

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    tuli (23rd February 2019)

  4. #3
    Join Date
    Jul 2012
    Posts
    223
    Thanks
    18
    Thanked 14 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMenu::addAction ownership

    Hm, that sucks. Am I correct that the setStatusTip indeed only works if the QAction is a direct child of the MainWindow?

    That puts me in a difficult position to use statusTips for menus that are dynamically created and need to be generated anew for each menu show.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,976
    Thanks
    232
    Thanked 627 Times in 617 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QMenu::addAction ownership

    You can create QAction instances as children of any QObject (using new QAction( parent )). In that case, they are "owned" by that parent. You can then add the QAction to any QMenu, QToolBar, or other QWidget with ownership remaining with the original parent. In this case, you're calling QWidget::addAction() with the QAction pointer. When you create the QAction, you make the connection between its triggered() signal and whatever slot you want executed, independently of which QWidget you have added it to. Therefore, you can add the same QAction instance to multiple QWidgets (using addAction()) and have the same slot executed.
    <=== 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.

  6. The following user says thank you to d_stranz for this useful post:

    tuli (23rd February 2019)

  7. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,294
    Thanks
    36
    Thanked 1,521 Times in 1,472 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMenu::addAction ownership

    Quote Originally Posted by tuli View Post
    Am I correct that the setStatusTip indeed only works if the QAction is a direct child of the MainWindow?
    Looking at the code says that the action always sends a status tip event to its widget or parent, but essentially only the main window reacts to it.

    You could try setting the main window as the parent of each action, or you could install an event filter in the QApplication object and forward all status tip events to the main window.

    Cheers,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    tuli (23rd February 2019)

  9. #6
    Join Date
    Jul 2012
    Posts
    223
    Thanks
    18
    Thanked 14 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMenu::addAction ownership

    Thanks. Yes, it seems they have to be direct parents indeed.

    In my case the menu is different every time it's invoked, so it doesnt make sense to store actions permanently.
    Seems I have to go for a workaround hack, like storing all actions and deleting or reusing them on next menu invokation or something like that.

  10. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,294
    Thanks
    36
    Thanked 1,521 Times in 1,472 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMenu::addAction ownership

    I think the easiest way is the event filter approach.

    Something like
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    2. {
    3. QApplication::instance()->installEventFilter(this);
    4. }
    5.  
    6. bool MainWindow::eventFilter(QObject *watched, QEvent *event)
    7. {
    8. // Status tip sent to any widget other than the main window gets rerouted to "this"
    9. if (event->type() == QEvent::StatusTip && watched != this) {
    10. QMainWindow::event(event);
    11. return true;
    12. }
    13.  
    14. return QMainWindow::eventFilter(watched, event);
    15. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  11. The following user says thank you to anda_skoa for this useful post:

    tuli (24th February 2019)

  12. #8
    Join Date
    Jul 2012
    Posts
    223
    Thanks
    18
    Thanked 14 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMenu::addAction ownership

    Thanks for the sample code, that's probably the best solution.

    The problem with submenues and tooltips seems to be a longstanding bug btw:

    https://bugreports.qt.io/browse/QTBU...mmentId=307223

Similar Threads

  1. menuBar()->addAction broken on Android?
    By sedi in forum Qt Programming
    Replies: 0
    Last Post: 9th July 2014, 22:01
  2. ownership of QMenu after calling addMenu()
    By cic in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2013, 17:27
  3. using invokeMethod for menu addAction
    By dwizard in forum Newbie
    Replies: 1
    Last Post: 27th June 2013, 06:31
  4. QMenu and addAction problem
    By .:saeed:. in forum Newbie
    Replies: 0
    Last Post: 20th January 2011, 09:04
  5. Replies: 4
    Last Post: 16th September 2010, 16:24

Tags for this Thread

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.