Results 1 to 4 of 4

Thread: how to delete a widget

  1. #1
    Join Date
    Oct 2015
    Posts
    27
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default how to delete a widget

    ACtually im having 2 Qwidgets say "Widget" , "NewWidget". im calling "NewWidget" in "Widget" keypress event and viced versa,

    void Widget::keyPressEvent(QKeyEvent *qv)
    {
    // i want to delete "Widget" here before i call "NewWidget"
    NewWidget *newWidget = new NewWidget();
    newWidget->setStyleSheet("background-color:black;");
    newWidget->setGeometry(0,0,640,480);
    newWidget->show();
    }

    I want to delete or destroy the "Widget" before calling "NewWidget"

  2. #2
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to delete a widget

    Your trying to delete this object in its own function which is dangerous(object self destruction).

    May be you can use this->deleteLater(); in this case.

    But destroying self object is bad idea.

    What is your requirement.?
    Thanks :-)

  3. #3
    Join Date
    Oct 2015
    Posts
    27
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to delete a widget

    im running this executable in ARM based board which is having very less memory.Actually im using 4 widgets in my application,so if i delete the widget while calling another , i can save the memory thats the reason im looking for.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to delete a widget

    As prasad_N said, use deleteLater() if you do it inside the widget.
    If you need to delete the current widget before you create the new one, then you need to emit a signal, connect it to some slot using a Qt::QueuedConnection and in the slot first delete the current widget, then create the new one.

    Cheers,
    _

Similar Threads

  1. Can't delete widget returned by QLayoutItem->widget()
    By davethomaspilot in forum Qt Programming
    Replies: 6
    Last Post: 1st June 2014, 23:39
  2. Delete single widget from QLayoutGrid
    By KeineAhnung in forum Newbie
    Replies: 8
    Last Post: 13th May 2014, 14:45
  3. Replies: 2
    Last Post: 19th July 2013, 17:24
  4. Replies: 2
    Last Post: 15th March 2012, 08:48
  5. Delete content widget in layout
    By Hogwarts in forum Qt Programming
    Replies: 1
    Last Post: 17th December 2010, 17:59

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.