Results 1 to 3 of 3

Thread: Creating the QListView instance without a parent

  1. #1
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Creating the QListView instance without a parent

    Hi, I found in many Qt-examples, either from the Official Documentation, or from the books, that the QListView (or any other View) can be created dinamically without specifying the parent, like here:
    Qt Code:
    1. QListView *listView = new QListView;
    2. listView->setModel(&model); // model was created previously on the stack
    To copy to clipboard, switch view to plain text mode 

    So the question is: why have it sense?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Creating the QListView instance without a parent

    The parent defines the context in which a widget is created.
    If it has a parent, it will be drwan in its parent context.
    If it has no parent. it will become a top level window.
    For non visual QObjects, parents will delete their children when they them selves get destroyed, which makes your code safer and cleaner.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. The following 2 users say thank you to high_flyer for this useful post:

    Astronomy (3rd February 2010), yashaka (4th February 2010)

  4. #3
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating the QListView instance without a parent

    Thank you! You made me think about this ones more and I found that didn't draw attention to the fact that in all that examples the following code happens a bit further:
    Qt Code:
    1. hboxLayout->addWidget(listView);
    2. mainWgt.setLayout(hboxLayout);
    To copy to clipboard, switch view to plain text mode 
    So listView will be safely deleted when mainWgt will close.

Similar Threads

  1. single instance app
    By bnilsson in forum Qt Programming
    Replies: 2
    Last Post: 1st August 2009, 08:42
  2. Replies: 1
    Last Post: 15th June 2009, 17:22
  3. QPluginLoader instance
    By Jeff100 in forum Qt Programming
    Replies: 1
    Last Post: 22nd November 2007, 06:47
  4. QApplication instance
    By nile.one in forum Qt Programming
    Replies: 9
    Last Post: 5th October 2007, 12:06
  5. creating a global instance
    By skatakam in forum General Programming
    Replies: 6
    Last Post: 24th February 2006, 16:26

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.