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. layout set and widget as parent...
Forum Updated to NodeBB v4.3 + New Features

layout set and widget as parent...

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

    Hello!

    is this....

    QWidget* wdg = new QWidget(this);
    QHBoxLayout* lay = new QHBoxLayout(wdg); //does internally applies layout to wdg parent?
    QPushButton* pbt = new QPushButton(wdg); //does internally layout applied to wdg add the button?
    

    the same to this?

    QWidget* wdg = new QWidget(this);
    QHBoxLayout* lay = new QHBoxLayout;
    wdg->setLayout(lay);
    QPushButton* pbt = new QPushButton;
    lay->addWidget(pbt);
    
    Christian EhrlicherC 1 Reply Last reply
    0
    • 1 1XU7

      @Christian-Ehrlicher

      i tought by setting wdg as parent to button, and wdg has lay as layout, the layout would add button automatically... but you mean that is not made.. i need to manually add button to layout:

      QWidget* wdg = new QWidget(this);
      QHBoxLayout* lay = new QHBoxLayout(wdg);
      QPushButton* pbt = new QPushButton(wdg);  //it is necesary to set wdg as parent here?
      lay->addWidget(pbt);
      
      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @1XU7 said in layout set and widget as parent...:

      i tought by setting wdg as parent to button, and wdg has lay as layout, the layout would add button automatically...

      No and I don't see any evidence that a QObject-based parent-child relationship should have something to do with a layout.
      The other way round is correct - when you add a widget to another widgets layout then the other widget is becoming the parent of the child because otherwise parent-child stuff like e.g. event propagation would not work. So no, no need to set a parent in your code but it will also not hurt and makes it more obvious what you're trying to do.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      4
      • 1 1XU7

        Hello!

        is this....

        QWidget* wdg = new QWidget(this);
        QHBoxLayout* lay = new QHBoxLayout(wdg); //does internally applies layout to wdg parent?
        QPushButton* pbt = new QPushButton(wdg); //does internally layout applied to wdg add the button?
        

        the same to this?

        QWidget* wdg = new QWidget(this);
        QHBoxLayout* lay = new QHBoxLayout;
        wdg->setLayout(lay);
        QPushButton* pbt = new QPushButton;
        lay->addWidget(pbt);
        
        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by Christian Ehrlicher
        #2

        According to the documentation, yes - at least for the Q

        I misread your post - since you don't add pbt to a layout it will look different. Layouts and parent are two different concepts.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 1 Reply Last reply
        2
        • Christian EhrlicherC Christian Ehrlicher

          According to the documentation, yes - at least for the Q

          I misread your post - since you don't add pbt to a layout it will look different. Layouts and parent are two different concepts.

          1 Offline
          1 Offline
          1XU7
          wrote on last edited by
          #3

          @Christian-Ehrlicher

          i tought by setting wdg as parent to button, and wdg has lay as layout, the layout would add button automatically... but you mean that is not made.. i need to manually add button to layout:

          QWidget* wdg = new QWidget(this);
          QHBoxLayout* lay = new QHBoxLayout(wdg);
          QPushButton* pbt = new QPushButton(wdg);  //it is necesary to set wdg as parent here?
          lay->addWidget(pbt);
          
          Christian EhrlicherC 1 Reply Last reply
          0
          • 1 1XU7

            @Christian-Ehrlicher

            i tought by setting wdg as parent to button, and wdg has lay as layout, the layout would add button automatically... but you mean that is not made.. i need to manually add button to layout:

            QWidget* wdg = new QWidget(this);
            QHBoxLayout* lay = new QHBoxLayout(wdg);
            QPushButton* pbt = new QPushButton(wdg);  //it is necesary to set wdg as parent here?
            lay->addWidget(pbt);
            
            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @1XU7 said in layout set and widget as parent...:

            i tought by setting wdg as parent to button, and wdg has lay as layout, the layout would add button automatically...

            No and I don't see any evidence that a QObject-based parent-child relationship should have something to do with a layout.
            The other way round is correct - when you add a widget to another widgets layout then the other widget is becoming the parent of the child because otherwise parent-child stuff like e.g. event propagation would not work. So no, no need to set a parent in your code but it will also not hurt and makes it more obvious what you're trying to do.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            4
            • 1 1XU7 has marked this topic as solved on

            • Login

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