Results 1 to 3 of 3

Thread: Keeping QWidgets as private QWIdget subclass members

  1. #1
    Join Date
    May 2007
    Location
    Warsaw, Poland
    Posts
    52
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Keeping QWidgets as private QWIdget subclass members

    Is it ok to declare for example QLabel as a private member of some CustomWidget class and "place" it in CustomWidget using setParent or Layouts...
    Qt Code:
    1. class CustomWidget : public QWidget {
    2. Q_OBJECT
    3. public:
    4. CustomWidget(QWidget *parent = 0);
    5. private:
    6. QLabel oneL;
    7. };
    To copy to clipboard, switch view to plain text mode 
    What about Qt's "garbage collector" in this case?
    Shall I rather keep pointers to QWidgets I want to use in my CustomWidget instance?
    What differs these two methods?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Keeping QWidgets as private QWIdget subclass members

    Is it ok to declare for example QLabel as a private member of some CustomWidget class and "place" it in CustomWidget using setParent or Layouts...
    It is ok for them to be private. No problem in that matter.

    What about Qt's "garbage collector" in this case?
    Shall I rather keep pointers to QWidgets I want to use in my CustomWidget instance?
    Yes, you should rather use pointer members, in order to have the guarantee that the parent QObject will successfully delete its children. If you do this then you also have to make sure that all QObject(or subclasses) members will be created with a QObject parent such that at some point in your application the object will be deleted, either manually deleted, with deleteLater or by setting the delete on close flag.

    What differs these two methods?
    deleteLater doesn't work properly if you allocate on the stack.
    Last edited by marcel; 31st December 2007 at 17:11.

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

    Default Re: Keeping QWidgets as private QWIdget subclass members

    Here's one thread worth reading. It will explain why shouldn't you allocate widgets on the stack.
    J-P Nurmi

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.