Results 1 to 10 of 10

Thread: Use of parent for automatic unload

  1. #1
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Use of parent for automatic unload

    I've got a new little problem with the use of parent.

    I create a new class based on QGridLayout.
    In the constructor I create some new QWidget like label in this way
    Qt Code:
    1. lbArray[N] = new QLabel(parent);
    To copy to clipboard, switch view to plain text mode 
    where parent is an argument of the constructor of my class:
    Qt Code:
    1. IOPortConfig(QWidget *parent = 0);
    To copy to clipboard, switch view to plain text mode 

    In the MainWindow I create the new QWidget in this way:
    Qt Code:
    1. d_IOInitConfig = new IOPortConfig(this);
    2. hboxIOInitConfig->insertLayout(0, d_IOInitConfig);
    To copy to clipboard, switch view to plain text mode 

    hboxIOInitConfig is an Horizontal Layout partially created by the Designer.

    The "problem" is that, in this way, I've got this warning:

    QLayout: Attempting to add QLayout "" to MainWindow "MainWindow", which already has a layout

    I want to use the parent so I don't need to worry about deleting the QWidget that I created in my class.

    How should I do?

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use of parent for automatic unload

    Use QWidget::setLayout instead of create a new Layout with MainWindow as parent

    Qt Code:
    1. hboxIOInitConfig = new QHBoxLayout;
    2.  
    3. ....
    4.  
    5. this->setLayout(hboxIOInitConfig);
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use of parent for automatic unload

    I don't manually create the hboxIOInitConfig: it is created by the Designer.
    Instead I create my component IOPortConfig and I pass to this Widget "this" as parent.

  4. #4
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Use of parent for automatic unload

    QMainWindow already has a layout. Instead create a widget to be your parent for other controls and add it to the main window layout using setCentralWidget.

  5. #5
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use of parent for automatic unload

    I don't understand.

    New question, in the code below, IOPortConfig parent is hboxIOInitConfig?

    Qt Code:
    1. d_IOInitConfig = new IOPortConfig(); // parent = NULL at this point
    2. hboxIOInitConfig->insertLayout(0, d_IOInitConfig); // parent = hboxIOInitConfig?
    To copy to clipboard, switch view to plain text mode 

    If the answer is yes, Qt automatically can now delete all the Widget internal to IOPortConfig?

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

    Default Re: Use of parent for automatic unload

    To cut this discussion... Any widget that is not a top-level window has a parent. If you don't give it a parent when creating it, it will be reparented when it's inserted into a layout. Either way it will be deleted when the window containing it is deleted.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use of parent for automatic unload

    So, all the Widget that I create inside a class that inherit from QGridLayout they will be unload automatically when I delete the instance of the class without needed to manually remove in the destructor? And I don't need to force the parent when I create every widget. Is it right?

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

    Default Re: Use of parent for automatic unload

    QGridLayout is not a widget and it's not practiced to subclass it to build a hierarchy of widgets (you should subclass QWidget for that) but yes, you don't need to destroy the widgets manually provided the layout is at some point applied to a widget.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use of parent for automatic unload

    Well, why shouldn't be practiced?

    I need to create a composite Widget with inside some array of QComboBox, QLabel, QLine edit. If I create a simple class inherit from QWidget, internally I need to create a QGridLayout and add all my controls to this. Instead I create a new class from QGridLayout and I only need to create the widgets and add these to my class.

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

    Default Re: Use of parent for automatic unload

    Quote Originally Posted by PaceyIV View Post
    Well, why shouldn't be practiced?
    Because a layout can't live on its own, it's always enveloped in a widget.

    I need to create a composite Widget with inside some array of QComboBox, QLabel, QLine edit. If I create a simple class inherit from QWidget, internally I need to create a QGridLayout and add all my controls to this. Instead I create a new class from QGridLayout and I only need to create the widgets and add these to my class.
    Then there is a question whether it should be a class at all. I'd rather create a function that could be applied on any widget. Actually I wouldn't... I would subclass QWidget
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 6
    Last Post: 8th July 2009, 13:24
  2. Replies: 7
    Last Post: 14th May 2009, 01:37
  3. TabWidget's Tab's parent
    By alisami in forum Qt Programming
    Replies: 12
    Last Post: 31st March 2009, 13:30
  4. How to close parent window from child window?
    By montylee in forum Qt Programming
    Replies: 5
    Last Post: 14th October 2008, 11:40
  5. parent() heirarchy misunderstanding?
    By KShots in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2007, 18:18

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.