Results 1 to 3 of 3

Thread: QTableWidget will delete its QMenu twice?

  1. #1
    Join Date
    Aug 2010
    Posts
    7
    Thanks
    8

    Question QTableWidget will delete its QMenu twice?

    I have a piece of code like pseudo code below:

    Qt Code:
    1. class ImIncludeTable{
    2. mMainTable = new QTableWidget(30,4,this);//This class contains a QTableWidget
    3. ....
    4. mAction1 = new QAction(tr("Book/Cancel"),mMainTable);
    5. ....
    6. mMainTable->addAction(mAction1);//add an action to this QTableWidget
    7. mMainTable->setContextMenuPolicy(Qt::ActionsContextMenu);
    8. ....
    9. connect(mAction1,SIGNAL(trigger()),this,SLOT(OpenADialog));//Open a Dialog, like a Qt::ApplicationModal Dialog while the action triggered
    10. ....
    11. if(OpenADialog->result()) delete this;//if the dialog says true, the object will be called to delete itself. It's not a direct call, but a call by other class.
    12. }
    To copy to clipboard, switch view to plain text mode 

    When the code run to the delete object line, a runtime error pops up.
    But if I don't use a right-clicked context, everything is fine.

    So, will QTableWidget delete its QMenu twice?
    Or, because the QMenu is deleted by itself(when disappearing) after the delete object call, so it is deleted twice?

  2. #2
    Join Date
    Dec 2010
    Posts
    33
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: QTableWidget will delete its QMenu twice?

    delete this ?
    Maybe you want call QObject::deleteLater () ?

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

    liuyanghejerry (16th June 2011)

  4. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: QTableWidget will delete its QMenu twice?

    As Wong replied, it would be better to use QObject::deleteLater(), (in case you are able to it)

    as far as I understand you can still use "delete this", as long as "this" (the object) is created on heap (Created using new operaator).
    If it was created on stack (local variable, or class member variable), then the you application will raise runtime errors, as it will tried to be deleted twice, In this case even deleteLater() may also raise errors (Not sure of this though)

  5. The following user says thank you to Santosh Reddy for this useful post:

    liuyanghejerry (16th June 2011)

Similar Threads

  1. Does QTableWidget::clear() delete pointers?
    By Slewman in forum Qt Programming
    Replies: 5
    Last Post: 21st January 2011, 17:09
  2. Replies: 2
    Last Post: 13th May 2009, 11:35
  3. Replies: 4
    Last Post: 19th February 2009, 12:10
  4. How to delete header on QTableWidget
    By jlbrd in forum Qt Programming
    Replies: 2
    Last Post: 18th July 2006, 22:00
  5. Qmenu Stable close on QTableWidget
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 17th July 2006, 11:03

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.