Results 1 to 5 of 5

Thread: QPushButton deleting itself on click

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QPushButton deleting itself on click

    I'm trying to implement little close buttons on widgets, so that when the close button is pressed the widget is deleted. I connected the clicked() signal of the button to a slot of the widget.

    Qt Code:
    1. void MyWidget::delete()
    2. {
    3. delete this;
    4. }
    To copy to clipboard, switch view to plain text mode 

    This works really well too. The widget is deleted from its parent and from the layout and the layout is redone. Except that I get a warning:

    QObject: Do not delete object, 'unnamed', during its event handler!

    I suppose it's because the clicked() signal hasn't returned yet and the destructor of the button is called, since the parent widget is deleted. But then, how can a button ever delete itself on click?
    Last edited by Cruz; 23rd January 2009 at 08:38. Reason: updated contents

  2. #2
    Join Date
    Aug 2008
    Posts
    60
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPushButton deleting itself on click

    one way is: hide this button in 'MyWidget::delete(){}' and later delete this object.

  3. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPushButton deleting itself on click

    I would make this a little different. I'd set Qt::WA_DeleteOnClose for a widget and by clicking on button just close the window. Qt::WA_DeleteOnClose -- means that a widget will be delete (the dtor will be called and memory will be free) when it closes.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    Cruz (23rd January 2009)

  5. #4
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QPushButton deleting itself on click

    Awesome! Like spirit suggested, this is all it takes:

    Qt Code:
    1. MyWidget::MyWidget(QWidget *parent) :
    2. QWidget(parent)
    3. {
    4. QPushButton *deleteButton = new QPushButton(this);
    5. setAttribute(Qt::WA_DeleteOnClose);
    6. connect(deleteButton, SIGNAL(clicked()), this, SLOT(close()));
    7. }
    To copy to clipboard, switch view to plain text mode 

    The widget is also removed from its parent and the layout. It flickers a bit (why isn't double buffering kicking in?), but it's worth the simplicity.

  6. #5
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QPushButton deleting itself on click

    Even better:

    Qt Code:
    1. MyWidget::MyWidget(QWidget *parent) :
    2. QWidget(parent)
    3. {
    4. QPushButton *deleteButton = new QPushButton(this);
    5. connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteLater()));
    6. }
    To copy to clipboard, switch view to plain text mode 

    Simpler and no more flickering.

    I love how better and better solutions converge towards simplicity.
    Last edited by Cruz; 23rd January 2009 at 09:19. Reason: updated contents

Similar Threads

  1. Is it bad to delete an object during its event handler?
    By codeslicer in forum Qt Programming
    Replies: 3
    Last Post: 30th December 2008, 16:14
  2. Changing default event handler name
    By mabeeh in forum Newbie
    Replies: 1
    Last Post: 21st April 2008, 15:18
  3. event handler
    By mattia in forum Newbie
    Replies: 10
    Last Post: 8th November 2007, 12:54
  4. c++, placement delete upon exception
    By stinos in forum General Programming
    Replies: 6
    Last Post: 31st October 2006, 15:38
  5. delete custom event
    By Dmitry in forum Qt Programming
    Replies: 1
    Last Post: 15th October 2006, 16: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.