Results 1 to 2 of 2

Thread: Memory management of QAction

  1. #1
    Join Date
    Jul 2016
    Posts
    19
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Memory management of QAction

    Hello,

    I have a question with the following piece of code that can be found here

    Qt Code:
    1. QMenu *MainWindow::createColorMenu(const char *slot, QColor defaultColor)
    2. {
    3. QList<QColor> colors;
    4. colors << Qt::black << Qt::white << Qt::red << Qt::blue << Qt::yellow;
    5. QStringList names;
    6. names << tr("black") << tr("white") << tr("red") << tr("blue")
    7. << tr("yellow");
    8.  
    9. QMenu *colorMenu = new QMenu(this);
    10. for (int i = 0; i < colors.count(); ++i) {
    11. QAction *action = new QAction(names.at(i), this);
    12. action->setData(colors.at(i));
    13. action->setIcon(createColorIcon(colors.at(i)));
    14. connect(action, SIGNAL(triggered()), this, slot);
    15. colorMenu->addAction(action);
    16. if (colors.at(i) == defaultColor)
    17. colorMenu->setDefaultAction(action);
    18. }
    19. return colorMenu;
    20. }
    To copy to clipboard, switch view to plain text mode 

    According to the doc of QWidget::addAction(QAction *action), the ownership of action is not transferred to the QWidget, so I wonder if the memory of each action created are correctly released.

    Thank you

  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: Memory management of QAction

    I wonder if the memory of each action created are correctly released.
    You create your QAction instances using "this" as the parent object instance. So when your MainWindow goes out of scope (presumably when your program exits, if you create it in main() as is the usual case), these instances will be deleted since they are children of MainWindow.

    If you create them without a parent (eg. "new QAction( "Some text" );") then they will not be deleted until the runtime cleans up dangling memory when your program exits.

    The reason why QAction ownership is not transferred to the widget when setting them on it is because QActions are designed to be shared - the same QAction instance can be assigned to a menu, toolbar, or anything else that can accept actions. This wouldn't be possible if there was a strict ownership hierarchy based on the widget to which the action was assigned.
    <=== 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.

Similar Threads

  1. Memory management with QAudioOutput
    By hackerNovitiate in forum Newbie
    Replies: 1
    Last Post: 20th January 2012, 01:41
  2. Memory management in Qt?
    By wookoon in forum Qt Programming
    Replies: 7
    Last Post: 6th November 2010, 18:20
  3. Memory Management Reg
    By BalaQT in forum Newbie
    Replies: 10
    Last Post: 4th February 2010, 12:43
  4. Memory management
    By cyberboy in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2008, 20:48
  5. Memory management in QT
    By Gayathri in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2006, 07:21

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.