Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved]Why can't declare an object layout management, must be a pointer?
Forum Updated to NodeBB v4.3 + New Features

[solved]Why can't declare an object layout management, must be a pointer?

Scheduled Pinned Locked Moved General and Desktop
layoutqvboxlayout
3 Posts 2 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • joeQJ Offline
    joeQJ Offline
    joeQ
    wrote on last edited by joeQ
    #1

    when we use the layout to manage some buttons ,such as:

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(btnfirst);
    layout->addWidget(btnsecond);
    layout->addWidget(btnthree);
    layout->addWidget(btnself);
    layout->addWidget(btnexit);
    QWidget *p = new QWidget;
    p->setLayout(layout);'''
    

    that is right,but the following is wrong ,why?

    QVBoxLayout layout;
    layout.addWidget(btnfirst);
    layout.addWidget(btnsecond);
    layout.addWidget(btnthree);
    layout.addWidget(btnself);
    layout.addWidget(btnexit);
    QWidget *p = new QWidget;
    p->setLayout(&layout);
    

    I don't know why , we must be use a pointer ? (thank you!)

    Just do it!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by mcosta
      #2

      Hi and welcome to devnet,

      each QObject instance has an array of pointers of its children; it uses that list to manage memory (destroys the children in the destructor) and visibility (hides the children when is hidden, resizes them according to layouts, ....).

      Your code doesn't work because at the end of the function, the layout variable goes out of scope and is destroyed; so the pointer you passed to setLayout becomes invalid.

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      joeQJ 1 Reply Last reply
      1
      • M mcosta

        Hi and welcome to devnet,

        each QObject instance has an array of pointers of its children; it uses that list to manage memory (destroys the children in the destructor) and visibility (hides the children when is hidden, resizes them according to layouts, ....).

        Your code doesn't work because at the end of the function, the layout variable goes out of scope and is destroyed; so the pointer you passed to setLayout becomes invalid.

        joeQJ Offline
        joeQJ Offline
        joeQ
        wrote on last edited by
        #3

        @mcosta thank you very much! I get it.

        Just do it!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved