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?

[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.1k Views
  • 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.
  • J Offline
    J Offline
    joeQ
    wrote on 4 Jun 2015, 14:04 last edited by joeQ 6 May 2015, 08:29
    #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 4 Jun 2015, 14:11 last edited by mcosta 6 Apr 2015, 14:12
      #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/)

      J 1 Reply Last reply 5 Jun 2015, 08:24
      1
      • M mcosta
        4 Jun 2015, 14:11

        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.

        J Offline
        J Offline
        joeQ
        wrote on 5 Jun 2015, 08:24 last edited by
        #3

        @mcosta thank you very much! I get it.

        Just do it!

        1 Reply Last reply
        0

        1/3

        4 Jun 2015, 14:04

        • Login

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