Results 1 to 4 of 4

Thread: QObjects as data members stored by value

  1. #1
    Join Date
    Jun 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QObjects as data members stored by value

    In all Qt examples QObjects are stored in their parent by pointer and created on heap in the constructor:
    Qt Code:
    1. class MainWidget: public QWidget
    2. {
    3. Q_OBJECT
    4. QLabel* nameLbl;
    5. QPushButton* fireBtn;
    6. //etc
    7. };
    8.  
    9. //in cpp:
    10.  
    11. MainWidget::MainWidget(QWidget* parent)
    12. :QWidget(parent)
    13. {
    14. nameLbl = new QLabel(this)
    15. fireBtn = new QPushButton("Fire", this);
    16. //etc
    17. }
    To copy to clipboard, switch view to plain text mode 
    Futhermore, Trolls recommend to create QObjects usually on heap, except in main() function and dealing with modal dialogs like QColorDialog.
    But there can be another approach. If we know exactly that the lifetime of a child is the same, as its parent, and the parent and thread of a child will never change, we can write this:
    Qt Code:
    1. class MainWidget: public QWidget
    2. {
    3. Q_OBJECT
    4. QLabel nameLbl;
    5. QPushButton fireBtn;
    6. //etc
    7. };
    8.  
    9. //in cpp:
    10.  
    11. MainWidget::MainWidget(QWidget* parent)
    12. :QWidget(parent),
    13. nameLbl(this),
    14. fireBtn("Fire", this)
    15. {
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 
    pros:
    1. Less typing (using dots . instead of arrows -> , laconic constructing)
    2. Less calls to 'new' and 'delete', that are always expensive.
    any cons? are there any drawbacks of this approach? do you use such approach?
    The only one that I know is that you need to be accurate when connecting to signals that are emitted when a child object destroys (QObject::destroyed etc). Let's assume that we're not interested in such signals.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QObjects as data members stored by value

    passing this as the owner of your stack based nameLbl etc will cause it to be a child (it the Qt object parent sense) of "this". When this is destroyed it will destroy its children (with delete).
    delete must not be called on stack objects but only on stuff allocated with new. So your code will crash.

    If you do that, you must make sure that all stack objects are not owned by any other objects.

    HTH

  3. #3
    Join Date
    Jun 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObjects as data members stored by value

    It won't crash, because the desctructors of data members are called BEFORE the destructor of the base class.

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QObjects as data members stored by value

    yes, you are right
    in that case it should work

Similar Threads

  1. data rate transfer is decreasing in TCP connection
    By navi1084 in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2009, 16:15
  2. Best way to display lots of data fast
    By New2QT in forum Newbie
    Replies: 4
    Last Post: 16th October 2008, 22:46
  3. Replies: 4
    Last Post: 19th October 2007, 19:47
  4. Data model
    By steg90 in forum Qt Programming
    Replies: 3
    Last Post: 17th September 2007, 12:14
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.