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. Widgets not expanding in grid layout applied to wizard page
Forum Update on Monday, May 27th 2025

Widgets not expanding in grid layout applied to wizard page

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 267 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.
  • FlincoF Offline
    FlincoF Offline
    Flinco
    wrote on last edited by Flinco
    #1

    As the name of the topic suggests, I can't get widgets placed in a QGridLayout installed in a QWizardPage to expand until they take up all the space available.

    I have a QWizard to which I add some QWizardPages.

    addPage(&mainDataPage);
    ...
    addPage(&logosPage);
    

    Unlike the others, the last page (logosPage) does not have a QFormLayout but rather a QGridLayout.
    This is what I do in the constructor of the logosPage page:

    QGridLayout *layout = new QGridLayout(this);
    this->setLayout(layout);
    

    Then I add some widgets; for example:

    layout->addWidget(new QLabel(tr("Left Logo")), 0, 0, Qt::AlignRight | Qt::AlignVCenter);
    layout->addWidget(new QLabel(tr("Logo 1 not loaded")), 0, 1, 4, 1, Qt::AlignCenter);
    ...
    layout->addWidget(new QPushButton(tr("Load")), 1, 0, Qt::AlignCenter);
    

    I would like the widgets (or at least some of them) to expand until they take up all the available space. Specifically, I would like QPushButtons to expand horizontally and some QLabels to expand in both directions. But I can't figure out how to do it.

    With the QLabels I have tried this way:

    QSizePolicy sizePolicy(QSizePolicy::Policy::Ignored, QSizePolicy::Policy::Ignored);
    sizePolicy.setHorizontalStretch(0);
    sizePolicy.setVerticalStretch(0);
    sizePolicy.setHeightForWidth(false);
    
    QLabel *label = new QLabel(tr("My cool label"));
    label->setStyleSheet("QLabel { background-color : white; }"); // I want it white
    label->setToolTip(tr("This label is cool"));
    label->setSizePolicy(sizePolicy);
    label->setFrameShape(QFrame::Shape::Box); // I want it boxed
    label->setScaledContents(true);
    layout->addWidget(label, 0, 1, 4, 1, Qt::AlignCenter);
    

    Everything works except that the QLabel remains minimum sized and does not go to take up all the space in the grid cells where it is installed.

    What am I doing wrong? What am I missing?
    I have the impression that the QGridLayout behaves differently when installed in a QWizardPage than if it were installed elsewhere.

    Pl45m4P 2 Replies Last reply
    0
    • FlincoF Flinco

      As the name of the topic suggests, I can't get widgets placed in a QGridLayout installed in a QWizardPage to expand until they take up all the space available.

      I have a QWizard to which I add some QWizardPages.

      addPage(&mainDataPage);
      ...
      addPage(&logosPage);
      

      Unlike the others, the last page (logosPage) does not have a QFormLayout but rather a QGridLayout.
      This is what I do in the constructor of the logosPage page:

      QGridLayout *layout = new QGridLayout(this);
      this->setLayout(layout);
      

      Then I add some widgets; for example:

      layout->addWidget(new QLabel(tr("Left Logo")), 0, 0, Qt::AlignRight | Qt::AlignVCenter);
      layout->addWidget(new QLabel(tr("Logo 1 not loaded")), 0, 1, 4, 1, Qt::AlignCenter);
      ...
      layout->addWidget(new QPushButton(tr("Load")), 1, 0, Qt::AlignCenter);
      

      I would like the widgets (or at least some of them) to expand until they take up all the available space. Specifically, I would like QPushButtons to expand horizontally and some QLabels to expand in both directions. But I can't figure out how to do it.

      With the QLabels I have tried this way:

      QSizePolicy sizePolicy(QSizePolicy::Policy::Ignored, QSizePolicy::Policy::Ignored);
      sizePolicy.setHorizontalStretch(0);
      sizePolicy.setVerticalStretch(0);
      sizePolicy.setHeightForWidth(false);
      
      QLabel *label = new QLabel(tr("My cool label"));
      label->setStyleSheet("QLabel { background-color : white; }"); // I want it white
      label->setToolTip(tr("This label is cool"));
      label->setSizePolicy(sizePolicy);
      label->setFrameShape(QFrame::Shape::Box); // I want it boxed
      label->setScaledContents(true);
      layout->addWidget(label, 0, 1, 4, 1, Qt::AlignCenter);
      

      Everything works except that the QLabel remains minimum sized and does not go to take up all the space in the grid cells where it is installed.

      What am I doing wrong? What am I missing?
      I have the impression that the QGridLayout behaves differently when installed in a QWizardPage than if it were installed elsewhere.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @Flinco said in Widgets not expanding in grid layout applied to wizard page:

      QLabels to expand in both directions

      QSizePolicy sizePolicy(QSizePolicy::Policy::Ignored, QSizePolicy::Policy::Ignored);
      

      What about QSizePolicy::Expanding, QSizePolicy::Expanding here?!


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0
      • FlincoF Offline
        FlincoF Offline
        Flinco
        wrote on last edited by
        #3

        I tried all the possible QSizePolicy::Policy values, including QSizePolicy::Expanding and QSizePolicy::MinimumExpanding, with no change at all in the behaviour.

        1 Reply Last reply
        0
        • FlincoF Flinco

          As the name of the topic suggests, I can't get widgets placed in a QGridLayout installed in a QWizardPage to expand until they take up all the space available.

          I have a QWizard to which I add some QWizardPages.

          addPage(&mainDataPage);
          ...
          addPage(&logosPage);
          

          Unlike the others, the last page (logosPage) does not have a QFormLayout but rather a QGridLayout.
          This is what I do in the constructor of the logosPage page:

          QGridLayout *layout = new QGridLayout(this);
          this->setLayout(layout);
          

          Then I add some widgets; for example:

          layout->addWidget(new QLabel(tr("Left Logo")), 0, 0, Qt::AlignRight | Qt::AlignVCenter);
          layout->addWidget(new QLabel(tr("Logo 1 not loaded")), 0, 1, 4, 1, Qt::AlignCenter);
          ...
          layout->addWidget(new QPushButton(tr("Load")), 1, 0, Qt::AlignCenter);
          

          I would like the widgets (or at least some of them) to expand until they take up all the available space. Specifically, I would like QPushButtons to expand horizontally and some QLabels to expand in both directions. But I can't figure out how to do it.

          With the QLabels I have tried this way:

          QSizePolicy sizePolicy(QSizePolicy::Policy::Ignored, QSizePolicy::Policy::Ignored);
          sizePolicy.setHorizontalStretch(0);
          sizePolicy.setVerticalStretch(0);
          sizePolicy.setHeightForWidth(false);
          
          QLabel *label = new QLabel(tr("My cool label"));
          label->setStyleSheet("QLabel { background-color : white; }"); // I want it white
          label->setToolTip(tr("This label is cool"));
          label->setSizePolicy(sizePolicy);
          label->setFrameShape(QFrame::Shape::Box); // I want it boxed
          label->setScaledContents(true);
          layout->addWidget(label, 0, 1, 4, 1, Qt::AlignCenter);
          

          Everything works except that the QLabel remains minimum sized and does not go to take up all the space in the grid cells where it is installed.

          What am I doing wrong? What am I missing?
          I have the impression that the QGridLayout behaves differently when installed in a QWizardPage than if it were installed elsewhere.

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @Flinco said in Widgets not expanding in grid layout applied to wizard page:

          layout->addWidget(label, 0, 1, 4, 1, Qt::AlignCenter);

          Also, are you aware what these values mean?
          Row 0
          Column 1
          RowSpan 4 (label will take 4 "cells" horizontally, if possible)
          ColumnSpan 1 (and 1 cell vertically, if possible)

          So for a 5x2 grid it should look like

          X L L L L
          X X X X X
          

          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          0
          • FlincoF Offline
            FlincoF Offline
            Flinco
            wrote on last edited by Flinco
            #5

            I believe the span values are correct, since the grid is structured like this:

                     0           1            2            3       
              +------------+------------+------------+------------+
            0 |       label|   label    |       label|   label    |
              +------------+  spanning  +------------+  spanning  +
            1 |<--button-->|   4 rows   |<--button-->|   4 rows   |
              +------------+     to     +------------+     to     +
            2 |<--button-->|accommodate +<--button-->|accommodate |
              +------------+     a      +------------+     a      +
            3 |            |   pixmap   |            |   pixmap   |
              +------------+------------+------------+------------+
            4 |       label|   label    |       label|   label    |
              +------------+  spanning  +------------+  spanning  +
            5 |<--button-->|   4 rows   +<--button-->|   4 rows   |
              +------------+     to     +------------+     to     +
            6 |<--button-->|accommodate +<--button-->|accommodate |
              +------------+     a      +------------+     a      +
            7 |            |   pixmap   |            |   pixmap   |
              +------------+------------+------------+------------+
            

            Cells (3,0), (3,2), (7,0), and (7,2) are empty.

            Just to be more clear, this is what i get:
            Screenshot_0.png
            with highlighted what I would like:
            Screenshot_1.png

            In another dialog (not a wizard) of my application I have no issues to get this:
            Screenshot_2.png
            but the UI of this dialog is built from a .ui file.

            jsulmJ 1 Reply Last reply
            0
            • FlincoF Flinco

              I believe the span values are correct, since the grid is structured like this:

                       0           1            2            3       
                +------------+------------+------------+------------+
              0 |       label|   label    |       label|   label    |
                +------------+  spanning  +------------+  spanning  +
              1 |<--button-->|   4 rows   |<--button-->|   4 rows   |
                +------------+     to     +------------+     to     +
              2 |<--button-->|accommodate +<--button-->|accommodate |
                +------------+     a      +------------+     a      +
              3 |            |   pixmap   |            |   pixmap   |
                +------------+------------+------------+------------+
              4 |       label|   label    |       label|   label    |
                +------------+  spanning  +------------+  spanning  +
              5 |<--button-->|   4 rows   +<--button-->|   4 rows   |
                +------------+     to     +------------+     to     +
              6 |<--button-->|accommodate +<--button-->|accommodate |
                +------------+     a      +------------+     a      +
              7 |            |   pixmap   |            |   pixmap   |
                +------------+------------+------------+------------+
              

              Cells (3,0), (3,2), (7,0), and (7,2) are empty.

              Just to be more clear, this is what i get:
              Screenshot_0.png
              with highlighted what I would like:
              Screenshot_1.png

              In another dialog (not a wizard) of my application I have no issues to get this:
              Screenshot_2.png
              but the UI of this dialog is built from a .ui file.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Flinco said in Widgets not expanding in grid layout applied to wizard page:

              but the UI of this dialog is built from a .ui file

              Then look into the generated code to see how it is done in code.

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

              FlincoF 1 Reply Last reply
              2
              • jsulmJ jsulm

                @Flinco said in Widgets not expanding in grid layout applied to wizard page:

                but the UI of this dialog is built from a .ui file

                Then look into the generated code to see how it is done in code.

                FlincoF Offline
                FlincoF Offline
                Flinco
                wrote on last edited by Flinco
                #7

                @jsulm I copied the part involving the QSizePOlicy object right from the code generated by the .ui file.

                My feeling is that the QGridLayout is inheriting different constraints when it is installed in a QDialog than when it is installed in a QWizardPage. If I find confirmation to this guess, I would move to a different approach (use a QFormLayout where I insert nested layouts in the field column).

                jsulmJ 1 Reply Last reply
                0
                • FlincoF Flinco

                  @jsulm I copied the part involving the QSizePOlicy object right from the code generated by the .ui file.

                  My feeling is that the QGridLayout is inheriting different constraints when it is installed in a QDialog than when it is installed in a QWizardPage. If I find confirmation to this guess, I would move to a different approach (use a QFormLayout where I insert nested layouts in the field column).

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Flinco said in Widgets not expanding in grid layout applied to wizard page:

                  I copied the part involving the QSizePOlicy object right from the code generated by the .ui file

                  What about the rest? Why don't you try more of the generated code in your wizard page? I really doubt it has anything to do with QDialog or QWizardPage.

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

                  FlincoF 1 Reply Last reply
                  1
                  • jsulmJ jsulm

                    @Flinco said in Widgets not expanding in grid layout applied to wizard page:

                    I copied the part involving the QSizePOlicy object right from the code generated by the .ui file

                    What about the rest? Why don't you try more of the generated code in your wizard page? I really doubt it has anything to do with QDialog or QWizardPage.

                    FlincoF Offline
                    FlincoF Offline
                    Flinco
                    wrote on last edited by Flinco
                    #9

                    @jsulm said in Widgets not expanding in grid layout applied to wizard page:

                    What about the rest?

                    The rest (of the setupUI() function) is a bunch of blocks like the following:

                    loadLogo1Button = new QPushButton(parentObj);
                    loadLogo1Button->setObjectName("loadLogo1Button");
                    loadLogo1Button->setIcon(icon2);
                    
                    gridLayout->addWidget(loadLogo1Button, 10, 0, 1, 1);
                    

                    followed by:

                    gridLayout->setColumnStretch(0, 1);
                    gridLayout->setColumnStretch(1, 1);
                    ...
                    gridLayout->setColumnStretch(7, 1);
                    

                    that I ported too (of course adapting the number of calls to the number of columns needed in the wizard page).

                    After that, there are the settings for the Tab Order (that I don't care in the wizard) and the signal-slot connections (that obviously in the wizard will be different).

                    The function ends with a call to the retranslateUi() function, where there are exsclusively calls to the setText(tr("...")) and setTooltip(tr("...")) applied to all the widgets containing translatable text.

                    No other function is present in the code generated from the .ui.

                    1 Reply Last reply
                    0
                    • FlincoF Offline
                      FlincoF Offline
                      Flinco
                      wrote on last edited by Flinco
                      #10

                      I have opened a ticket on the Qt bugtracking site. I would like to understand definitively if I am missing something or if I have run into a bug.

                      In the mean time I worked around my problem changing a little the layout and using a combination of QFormLayout, QHBoxLayout, and QVBoxLayout, which seem to respond correctly to the expansion directives.
                      Each row of the QFormLayout has the following structure:

                      +------------+---------------------------+
                      |label       |+-HBoxLayout-+------------+|
                      |            ||            |+VBoxLayout+||
                      |            ||            ||<-button->|||
                      |            ||   label    |+----------+||
                      |            ||  accommo-  ||<-button->|||
                      |            ||   dating   |+----------+||
                      |            ||     a      ||    ^     |||
                      |            ||   pixmap   ||SpacerItem|||
                      |            ||            ||    v     |||
                      |            ||            |+----------+||
                      |            |+------------+------------+|
                      +------------+---------------------------+
                      

                      I just set the fieldGrowthPolicy of the QformLayout to ExpandingFieldsGrow and the sizePolicy of each widget to QSizePolicy::Policy::Ignored for both the directions.

                      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