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. setLayout to a widget Vs setting parent to the layout
Forum Updated to NodeBB v4.3 + New Features

setLayout to a widget Vs setting parent to the layout

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

    Hi All,

    I want to know the difference between setting a layout to a widget and setting a parent to a layout. Please refer to the following codes to get a clarity to my points:

    CASE-1 : Setting layout to a widget

     QWidget *parentWidget= new QWidget();
     FlowLayout *flowL = new FlowLayout();
    ...
    ......
    ..............
    parentWidget->setLayout(flowL);
    

    CASE-2 setting parent to a layout

    
    QWidget *parentWidget= new QWidget();
     FlowLayout *flowL = new FlowLayout(parentWidget);
    .....
    .........
    .............
    parentWidget->setLayout(flowL);
    
    

    “ In order to be irreplaceable, one must always be different” – Coco Chanel

    jsulmJ 1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #4

      @Swati777999 CASE-1 and CASE-2 are equivalent. Creating a QLayout with a specified QWidget parent automatically sets it as the QWidget's layout. In CASE-1 you have to set the layout on the parent widget manually because the layout is parentless. In CASE-2 the call to setLayout() is unnecessary because the layout constructor did it for you (it does no harm to call it again).

      @AnneRanch jsulm's response was simply asking if the OP had read the manual and pointing at the relevant bit. The information is in the manual and reasonably easy to find, but it is possible the OP read it and did not understand. As for a "code example"... the example is the original post.

      1 Reply Last reply
      4
      • Swati777999S Swati777999

        Hi All,

        I want to know the difference between setting a layout to a widget and setting a parent to a layout. Please refer to the following codes to get a clarity to my points:

        CASE-1 : Setting layout to a widget

         QWidget *parentWidget= new QWidget();
         FlowLayout *flowL = new FlowLayout();
        ...
        ......
        ..............
        parentWidget->setLayout(flowL);
        

        CASE-2 setting parent to a layout

        
        QWidget *parentWidget= new QWidget();
         FlowLayout *flowL = new FlowLayout(parentWidget);
        .....
        .........
        .............
        parentWidget->setLayout(flowL);
        
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @Swati777999 Did you read documentation which explains this?
        https://doc.qt.io/qt-5/qwidget.html#QWidget
        https://doc.qt.io/qt-5/qlayout.html#QLayout

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • A Offline
          A Offline
          Anonymous_Banned275
          wrote on last edited by
          #3

          Ah... the well known "RTFM" answer style (is back ).

          Can you come up with a simple code example ? Or reference to example?
          I have been struggling with similar in "area" usage .

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #4

            @Swati777999 CASE-1 and CASE-2 are equivalent. Creating a QLayout with a specified QWidget parent automatically sets it as the QWidget's layout. In CASE-1 you have to set the layout on the parent widget manually because the layout is parentless. In CASE-2 the call to setLayout() is unnecessary because the layout constructor did it for you (it does no harm to call it again).

            @AnneRanch jsulm's response was simply asking if the OP had read the manual and pointing at the relevant bit. The information is in the manual and reasonably easy to find, but it is possible the OP read it and did not understand. As for a "code example"... the example is the original post.

            1 Reply Last reply
            4

            • Login

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