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. Parent of new QLabel(...)?

Parent of new QLabel(...)?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 778 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.
  • N Offline
    N Offline
    need4openid
    wrote on last edited by need4openid
    #1

    Hello,

    If I create a VBOXLayout and groupboxes, something like this:

        QGroupBox *box1 = new QGroupBox("Options", this);
        QVBoxLayout *box1layout = new QVBoxLayout(box1);
        box1layout->addWidget(new QLabel("", box1));
        box1->setLayout(box1layout);
        layout->addWidget(box1);
    

    In my mind I imagine it the way that if I create a groupbox, then it would become the parent of everything that I place into it. If I add a new widget then the widget would become the parent of everything inside it. Therefore my parent-child relationship looks something like this:

    mainwindow <-- box1 <-- QLabel
    

    Is this logic correct? Or should I just use always this as parent? Or should I just use null for parent? What is the preferred way of doing this? Thanks in advance.

    JonBJ 1 Reply Last reply
    0
    • N need4openid

      Hello,

      If I create a VBOXLayout and groupboxes, something like this:

          QGroupBox *box1 = new QGroupBox("Options", this);
          QVBoxLayout *box1layout = new QVBoxLayout(box1);
          box1layout->addWidget(new QLabel("", box1));
          box1->setLayout(box1layout);
          layout->addWidget(box1);
      

      In my mind I imagine it the way that if I create a groupbox, then it would become the parent of everything that I place into it. If I add a new widget then the widget would become the parent of everything inside it. Therefore my parent-child relationship looks something like this:

      mainwindow <-- box1 <-- QLabel
      

      Is this logic correct? Or should I just use always this as parent? Or should I just use null for parent? What is the preferred way of doing this? Thanks in advance.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @need4openid
      To give a simple answer, in fact you don't need to pass anything (nullptr is the default) if you will be adding the new widget onto an existing widget/layout (e.g. via addWidget() or setLayout()), which is the normal case. For example, if you add that QGroupBox onto a widget after creating it, which I imagine you will, you could have omitted the this parent there. Up to you, I usually don't bother.

      As an example, see https://doc.qt.io/qt-5/layout.html#tips-for-using-layouts and the code they give there. Note e.g.

      When you use a layout, you do not need to pass a parent when constructing the child widgets. The layout will automatically reparent the widgets (using QWidget::setParent()) so that they are children of the widget on which the layout is installed.

      1 Reply Last reply
      2

      • Login

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