Resizing QLineEdit in a QGridLayout
-
In a QGridLayout, I am not able to resize a QLineEdit or set its minimum width.
What I'm looking for is that the following code (in a
QMainWindow
subclass)...QWidget *widget = new QWidget; setCentralWidget(widget); QLineEdit *edit = new QLineEdit("Test"); QLineEdit *edit2 = new QLineEdit("Test2"); QGridLayout *layout = new QGridLayout(widget); layout->addWidget(edit, 0, 0, 1, 1, Qt::AlignLeft); layout->addWidget(edit2, 0, 1, 1, 100, Qt::AlignLeft);
...would result in the first
QLineEdit
showing very little text, i.e. one character, and the other QLineEdit stretched out to fit most of the rest of the available space.However, what ultimately shows up is this:
I have tried setting the size policy, column minimum width and line edit minimum width, changing the size hint, and the stylesheet, but neither line edit ever changed its size once. How would I go about achieving this?
-
After some experimentation, I've finally got the "ideal" solution:
If you want to use colspan and rowspan in the same way I'm looking for, then every row and column that is stretched through must have a stretch set to 1. Final code:
QWidget *widget = new QWidget; setCentralWidget(widget); QLineEdit *edit = new QLineEdit("Test"); QLineEdit *edit2 = new QLineEdit("Test2"); QLineEdit *edit3 = new QLineEdit("Test3"); QLineEdit *edit4 = new QLineEdit("Test4"); QGridLayout *layout = new QGridLayout(widget); layout->setColumnStretch(0, 1); layout->setColumnStretch(1, 1); layout->setColumnStretch(2, 1); layout->setRowStretch(0, 1); layout->setRowStretch(1, 1); layout->setRowStretch(2, 1); layout->addWidget(edit, 0, 0, 2, 2); layout->addWidget(edit2, 0, 2, 1, 1); layout->addWidget(edit3, 2, 0, 1, 1); layout->addWidget(edit4, 2, 1, 1, 2); edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); edit2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); edit3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); edit4->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
Results in:
Before I had assumed that the default stretch of every widget in a layout was 1, but nope. This, however, fixes it. Also ensure that every column and row that is spanned at all is stretched (i.e. column 0 with span 2 = 0 and 1 need to be stretched to 1).
-
@swurl Take a look at https://doc.qt.io/qt-6/qgridlayout.html#setColumnStretch
-
@mpergand This did not work either.
Interestingly I tried setting the vertical size policy as well, and setting that to expanding resulted in this:
(this is with both horizontal AND vertical policies of both
QLineEdit
s set toQSizePolicy::Expanding
.) -
@swurl said in Resizing QLineEdit in a QGridLayout:
What values should I input?
The documentation is quite clear:
"The stretch factor is relative to the other columns in this grid. Columns with a higher stretch factor take more of the available space."
So, if one should be double size compared to the other then use 1 and 2. -
@jsulm Understood.
Any idea what's going on with the image in my last post? Seems like the second line edit should be taking up twice the space. Using a column stretch works, but ideally I'd like to avoid the column stretch as my ultimate goal would be to have these line edits take up space properly. For example (from a non-Qt application):
However this is obviously not possible with column stretches, as the two different rows would have different column stretches. Ideally, changing the column spans would work, but as shown in my previous post that did not work.
-
Hi,
If you want your widgets to take different spaces on each lines, you might want to consider using a QVBoxLayout/QHBoxLayout combo as you do not seem to work with a grid in fact.
-
@swurl said in Resizing QLineEdit in a QGridLayout:
how would I have a widget take up two rows?
There are no real rows with the approach from @SGaist
So, the question is not clear. -
@jsulm For example, a widget taking up two rows and two columns in a QGridLayout, but in the VBox-HBox combo
My only idea on doing this with a VBox-HBox combination would be stretching the HBox to be two "rows" tall, but then you couldn't have another one-row-tall widget next to that widget, it'd have to be two "rows" tall.
I feel like I'm thinking of this wrong... There should definitely be a way to have widgets take up different spaces on different lines that I'm missing.
-
-
@jsulm OK, with that I think I've gotten closer.
Here's an example of what I would like the final potential layout to look like (again, non-Qt):
This is the closest I can get: (notice that the Test2 box takes up two "rows", not one)
This is with this code in a
QMainWindow
derivative:QWidget *widget = new QWidget; setCentralWidget(widget); QLineEdit *edit = new QLineEdit("Test"); QLineEdit *edit2 = new QLineEdit("Test2"); QLineEdit *edit3 = new QLineEdit("Test3"); QLineEdit *edit4 = new QLineEdit("Test4"); QVBoxLayout *layout = new QVBoxLayout(widget); QHBoxLayout *h1 = new QHBoxLayout; QHBoxLayout *h2 = new QHBoxLayout; layout->addLayout(h1, 1); layout->addLayout(h2, 1); h1->addWidget(edit, 2); h1->addWidget(edit2, 1); h2->addWidget(edit3, 1); h2->addWidget(edit4, 2); layout->setStretch(layout->indexOf(h1), 2); edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); edit2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); edit3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); edit4->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
I imagine there's a way to do this with the geometry of
edit2
, the one I don't want to take up two "rows", like setting the height to half of its initial height, but that has its drawbacks too. -
@swurl said in Resizing QLineEdit in a QGridLayout:
layout->setStretch(layout->indexOf(h1), 2);
If I understand you well, I think the strech should be applied to h2 not h1.Is it what you want :
If so, you need to add a second vertical layout and insert a dummy widget after test2:
QLineEdit *edit = new QLineEdit("Test"); QLineEdit *edit2 = new QLineEdit("Test2"); QLineEdit *edit3 = new QLineEdit("Test3"); QLineEdit *edit4 = new QLineEdit("Test4"); QVBoxLayout *layout = new QVBoxLayout(widget); QHBoxLayout *h1 = new QHBoxLayout; QHBoxLayout *h2 = new QHBoxLayout; QVBoxLayout *v2 = new QVBoxLayout(widget); layout->addLayout(h1, 1); h1->addWidget(edit, 2); h1->addLayout(v2); // add vertical layout at index 1 v2->addWidget(edit2); auto dummy=new QWidget; // add a dummy widget dummy->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); v2->addWidget(dummy); layout->addLayout(h2, 1); h2->addWidget(edit3, 1); h2->addWidget(edit4, 2); layout->setStretch(layout->indexOf(h1), 2); edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); edit2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); edit3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); edit4->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
-
-
-
After some experimentation, I've finally got the "ideal" solution:
If you want to use colspan and rowspan in the same way I'm looking for, then every row and column that is stretched through must have a stretch set to 1. Final code:
QWidget *widget = new QWidget; setCentralWidget(widget); QLineEdit *edit = new QLineEdit("Test"); QLineEdit *edit2 = new QLineEdit("Test2"); QLineEdit *edit3 = new QLineEdit("Test3"); QLineEdit *edit4 = new QLineEdit("Test4"); QGridLayout *layout = new QGridLayout(widget); layout->setColumnStretch(0, 1); layout->setColumnStretch(1, 1); layout->setColumnStretch(2, 1); layout->setRowStretch(0, 1); layout->setRowStretch(1, 1); layout->setRowStretch(2, 1); layout->addWidget(edit, 0, 0, 2, 2); layout->addWidget(edit2, 0, 2, 1, 1); layout->addWidget(edit3, 2, 0, 1, 1); layout->addWidget(edit4, 2, 1, 1, 2); edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); edit2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); edit3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); edit4->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
Results in:
Before I had assumed that the default stretch of every widget in a layout was 1, but nope. This, however, fixes it. Also ensure that every column and row that is spanned at all is stretched (i.e. column 0 with span 2 = 0 and 1 need to be stretched to 1).
-