Widgets not expanding in grid layout applied to wizard page
-
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 thelogosPage
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. -
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 thelogosPage
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.@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?! -
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 thelogosPage
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.@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
-
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:
with highlighted what I would like:
In another dialog (not a wizard) of my application I have no issues to get this:
but the UI of this dialog is built from a.ui
file. -
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:
with highlighted what I would like:
In another dialog (not a wizard) of my application I have no issues to get this:
but the UI of this dialog is built from a.ui
file.@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.
-
@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.
@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).
-
@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).
@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.
-
@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.
@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 thesetText(tr("..."))
andsetTooltip(tr("..."))
applied to all the widgets containing translatable text.No other function is present in the code generated from the
.ui
. -
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 toExpandingFieldsGrow
and thesizePolicy
of each widget toQSizePolicy::Policy::Ignored
for both the directions.