Results 1 to 2 of 2

Thread: Layout parents

  1. #1

    Default Layout parents

    While trying to understand the difference between creating widgets on the stack or the heap, I came across this behaviour that puzzles me regarding layouts:

    int main(int argc, char *argv[]){
    QApplication app(argc, argv);
    QWidget win;
    QVBoxLayout layout(&win);


    This will work fine. Then, while trying to invert the order of creation to see what would happen when the objects go out of scope, this happened:

    int main(int argc, char *argv[]){

    QApplication app(argc, argv);
    QVBoxLayout layout;
    QWidget win;
    layout.setParent(&win);


    I was expecting some crash due to win trying to delete the stack created layout, but instead this is what I got:

    QLayout:arentWidget: A layout can only have another layout as a parent.

    But in the version, isn't the layout being created with the QWidget as parent? I expected a crash when the destructor got called, but not this... can someone explain this to me?

    Thank you!
    Newbie.

  2. #2
    Join Date
    Jan 2012
    Posts
    66
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Platforms
    Windows

    Default Re: Layout parents

    setParent is a method of QObject, so I don't really think it provides the functionality you were aiming for. I believe setParent is used more to keep track of the memory used by the object. My guess is that the error message is just clunky.

    AFAIK you are never supposed to create GUI objects on the stack, but I could be wrong.

Similar Threads

  1. How to access parents variables
    By revellix in forum Qt Programming
    Replies: 3
    Last Post: 10th October 2011, 16:03
  2. Replies: 2
    Last Post: 7th June 2011, 15:57
  3. QTreeWidget item with multiple parents
    By di_zou in forum Newbie
    Replies: 0
    Last Post: 16th November 2009, 16:32
  4. QStandardItemModel, parents and childs
    By alexandernst in forum Newbie
    Replies: 7
    Last Post: 23rd July 2009, 01:00
  5. how to distroy child window which parents is zero
    By rajesh in forum Qt Programming
    Replies: 4
    Last Post: 22nd June 2006, 09:52

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.