Results 1 to 14 of 14

Thread: [Qt 4.1.3] Child widget not destroyed

  1. #1
    Join Date
    Jan 2006
    Posts
    32
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    1

    Default [Qt 4.1.3] Child widget not destroyed

    I have a problem with Qt 4.1.3.

    I wrote a custom QWidget (say: widget1) that creates another QWidget (say widget2) in another window, but when widget1 is destroyed widget2 is not destroyed. WIth Qt 4.1.2 widget2 is destroyed.

    Here is the code I wrote:

    Qt Code:
    1. void CustomWidget1::someMethod()
    2. {
    3. widget2 = new CustomWidget2(this);
    4. widget2->setWindowFlags(Qt::Window);
    5. widget2->setAttribute(Qt::WA_DeleteOnClose);
    6. }
    To copy to clipboard, switch view to plain text mode 

    I found there are some changes in Qt 4.1.3 regarding QWidget.
    from changes-4.1.3:

    - QWidget
    Made sure that the application does not close if a widget with a
    visible parent exists.
    Fixed issue where scroll() would scroll child widgets in some cases.
    Fixed painting issues when resizing very large child widgets.
    Fixed a bug preventing setCursor() from working with platform-
    dependent cursors.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: [Qt 4.1.3] Child widget not destroyed

    Could you provide a minimal compilable example which reproduces the problem?

  3. #3
    Join Date
    Jan 2006
    Posts
    32
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    1

    Default Re: [Qt 4.1.3] Child widget not destroyed

    test.pro
    Qt Code:
    1. TEMPLATE = app
    2. TARGET = test
    3. DEPENDPATH += .
    4. INCLUDEPATH += .
    5.  
    6. # Input
    7. HEADERS = test.h
    8. SOURCES = test.cpp
    To copy to clipboard, switch view to plain text mode 

    test.h
    Qt Code:
    1. #include <QPushButton>
    2.  
    3. class MyButton : public QPushButton
    4. {
    5. Q_OBJECT
    6. public:
    7. MyButton(QWidget *);
    8.  
    9. public slots:
    10. void mySlot();
    11. };
    To copy to clipboard, switch view to plain text mode 

    test.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include <test.h>
    3.  
    4. MyButton::MyButton(QWidget *parent = NULL) : QPushButton(parent)
    5. {
    6. setText("Test");
    7. connect(this, SIGNAL(clicked()), this, SLOT(mySlot()));
    8. }
    9.  
    10. void MyButton::mySlot()
    11. {
    12. QWidget *w = new QWidget(this);
    13. w->setWindowFlags(Qt::Window);
    14. w->setAttribute(Qt::WA_DeleteOnClose);
    15. w->show();
    16. }
    17.  
    18. int main(int argc, char **argv)
    19. {
    20. QApplication a(argc, argv);
    21. MyButton button;
    22.  
    23. button.show();
    24.  
    25. return a.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 

    run the program, click on the button and then close the main window: if you use Qt-4.1.2 the child window is closed, if you use Qt-4.1.3 it's not.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: [Qt 4.1.3] Child widget not destroyed

    Unfortunately I don't have 4.1.3 compiled yet, so I can't verify it today. Can someone else with Qt 4.1.3 confirm the problem? Does the same happen for non-qwidget based classes too?

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: [Qt 4.1.3] Child widget not destroyed

    What if you set the Qt::WA_DeleteOnClose attribute for the button as well? Oh, and then you might also need to allocate the button on the heap so it won't get destructed twice (when closing and when going out of scope in main).
    J-P Nurmi

  6. #6
    Join Date
    Jan 2006
    Location
    South Carolina, USA
    Posts
    34
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 4 Times in 4 Posts

    Default Re: [Qt 4.1.3] Child widget not destroyed

    Quote Originally Posted by wysota
    Unfortunately I don't have 4.1.3 compiled yet, so I can't verify it today. Can someone else with Qt 4.1.3 confirm the problem? Does the same happen for non-qwidget based classes too?
    Well I compiled his program and it does act the way he says.

  7. #7
    Join Date
    Jan 2006
    Posts
    32
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    1

    Default Re: [Qt 4.1.3] Child widget not destroyed

    Quote Originally Posted by michael
    Well I compiled his program and it does act the way he says.
    On which platform? From your profile I suppose you're running Windows.

    I'm using Linux, so this bug/feature is cross-platform

  8. #8
    Join Date
    Jan 2006
    Location
    South Carolina, USA
    Posts
    34
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 4 Times in 4 Posts

    Default Re: [Qt 4.1.3] Child widget not destroyed

    Quote Originally Posted by Dusdan
    On which platform? From your profile I suppose you're running Windows.

    I'm using Linux, so this bug/feature is cross-platform

    Apparantly as I compiled on WindowsXP machine.

    Best thing I can up with off the top of my head is to create an event handler to catch the close event and close all of the open child windows. Not much of a suggestion but it should work.

    Whatever the case the parent widget isn't getting deleted until the child window is closed.

    Michael

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: [Qt 4.1.3] Child widget not destroyed

    What if you get rid of deleteOnClose and instead delete the widget manually (or using the deleteLater() slot).

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: [Qt 4.1.3] Child widget not destroyed

    Jacek just suggested this might not be a bug after all. You're not deleting the parent widget, just closing it.

    Try this:

    Qt Code:
    1. int main(int argc, char **argv)
    2. {
    3. QApplication a(argc, argv);
    4. MyButton *button = new MyButton(0, Qt::WA_DeleteOnClose); // modify the constuctor to accept flags
    5. button->show();
    6. QPushButton fakeButton;
    7. fakeButton.show(); // to prevent application from closing too soon
    8. return a.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 23rd May 2006 at 20:33.

  11. #11
    Join Date
    Jan 2006
    Location
    South Carolina, USA
    Posts
    34
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 4 Times in 4 Posts

    Default Re: [Qt 4.1.3] Child widget not destroyed

    [QUOTE=wysota]Jacek just suggested this might not be a bug after all. You're not deleting the parent widget, just closing it.
    [quote]

    That is true, but (if I remember correctly) in previous versions closing the parent widget closed the children as well. I could be wrong though.

    On a different note, and something I find strange, is if you change the flag from Qt::Window to Qt:Sheet it works the way the thread starter hopes (or believes) that it should.

  12. #12
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: [Qt 4.1.3] Child widget not destroyed

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MyButton: public QPushButton
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. MyButton(QWidget* parent = 0) : QPushButton("Test", parent)
    9. {
    10. connect(this, SIGNAL(clicked()), this, SLOT(mySlot()));
    11. }
    12.  
    13. private slots:
    14. void mySlot()
    15. {
    16. QWidget* widget = new QWidget(this, Qt::Window);
    17. widget->setAttribute(Qt::WA_DeleteOnClose);
    18. // From docs: "By default this attribute is set for all widgets except
    19. // transient windows such as splash screens, tool windows, and popup menus."
    20. // Clear Qt::WA_QuitOnClose attribute. (Notice the difference if you comment this line out..)
    21. widget->setAttribute(Qt::WA_QuitOnClose, false);
    22. widget->show();
    23. }
    24. };
    25.  
    26. int main(int argc, char *argv[])
    27. {
    28. QApplication a(argc, argv);
    29. MyButton* w = new MyButton;
    30. w->show();
    31. a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    32. return a.exec();
    33. }
    34.  
    35. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  13. #13
    Join Date
    Jan 2006
    Location
    South Carolina, USA
    Posts
    34
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 4 Times in 4 Posts

    Default Re: [Qt 4.1.3] Child widget not destroyed

    Well, changing that flag resolved the issue. Works as suggested. Hopefully it fixes the threadstarters problem.

    However, according to this http://doc.trolltech.com/4.1/qapplic...dowClosed-prop that should only apply if the window has no parent. So does the Qt app deparent the window because the Qt:Window flag is set?

    Michael

  14. #14
    Join Date
    Jan 2006
    Posts
    32
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    1

    Default Re: [Qt 4.1.3] Child widget not destroyed

    Thank you very much guys: this way it works both with Qt-4.1.2 and with Qt-4.1.3

Similar Threads

  1. minimize child widget
    By sreedhar in forum Qt Programming
    Replies: 5
    Last Post: 15th May 2006, 12:02
  2. Move child widget along with the parent widget
    By sreedhar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2006, 12:00
  3. Referencing Parent Widget from Child
    By taylor34 in forum Qt Programming
    Replies: 8
    Last Post: 11th April 2006, 15:13
  4. [Qt 4.1.0] Split a widget on demand
    By Townk in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 14:16
  5. advanced split widget
    By vitaly in forum Qt Programming
    Replies: 10
    Last Post: 24th January 2006, 20:00

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.