Automatically resize the widget
-
wrote on 17 Feb 2022, 11:43 last edited by
Hello , I have a QWidget that I created. I placed a layout in this widget and there are buttons in this layout.
What I want to do is:
The horizontal length of my widget is equal to my entire mainwindow, and
When I enlarge or shrink my mainwindow, my widget is sized accordingly.
How can I do that ?QWidget *widget= new QWidget(this); QPushButton *btn1 = new QPushButton(); QPushButton *btn2 = new QPushButton(); QPushButton *btn3 = new QPushButton(); QHBoxLayout *horizontal_bottom = new QHBoxLayout(); horizontal_bottom->setSpacing(0); horizontal_bottom->addWidget(btn1 ); horizontal_bottom->addWidget(btn2); horizontal_bottom->addWidget(btn3); horizontal_bottom->setContentsMargins(0,0,0,0); widget->setLayout(horizontal_bottom);
-
Hello , I have a QWidget that I created. I placed a layout in this widget and there are buttons in this layout.
What I want to do is:
The horizontal length of my widget is equal to my entire mainwindow, and
When I enlarge or shrink my mainwindow, my widget is sized accordingly.
How can I do that ?QWidget *widget= new QWidget(this); QPushButton *btn1 = new QPushButton(); QPushButton *btn2 = new QPushButton(); QPushButton *btn3 = new QPushButton(); QHBoxLayout *horizontal_bottom = new QHBoxLayout(); horizontal_bottom->setSpacing(0); horizontal_bottom->addWidget(btn1 ); horizontal_bottom->addWidget(btn2); horizontal_bottom->addWidget(btn3); horizontal_bottom->setContentsMargins(0,0,0,0); widget->setLayout(horizontal_bottom);
@Nevez said in Automatically resize the widget:
widget->setLayout(horizontal_bottom);
Did you also add this widget to a layout?
-
wrote on 17 Feb 2022, 12:06 last edited by
I added it but nothing changed.
QGridLayout *finalLayout = new QGridLayout(this); finalLayout->addWidget(widget);
i tried that too.
QHBoxLayout *horizontal_bottom = new QHBoxLayout(widget);
-
wrote on 17 Feb 2022, 12:13 last edited by
Is your widget the central widget of the Window ? How did you add it ? I think your code should works.
-
Is your widget the central widget of the Window ? How did you add it ? I think your code should works.
-
I added it but nothing changed.
QGridLayout *finalLayout = new QGridLayout(this); finalLayout->addWidget(widget);
i tried that too.
QHBoxLayout *horizontal_bottom = new QHBoxLayout(widget);
@Nevez said in Automatically resize the widget:
QHBoxLayout *horizontal_bottom = new QHBoxLayout(widget);
Where is this layout set?
You need to add widget to a layout on your central widget. -
wrote on 17 Feb 2022, 12:28 last edited by
@jsulm
I added a widget to the layout in my central widget. Horizontal length and scaling is exactly what I wanted. But this time, I can't move the widget I added to the position I want (vertically).
The widget just attaches to the center and does not move.
Is there a way to change its location? -
@jsulm
I added a widget to the layout in my central widget. Horizontal length and scaling is exactly what I wanted. But this time, I can't move the widget I added to the position I want (vertically).
The widget just attaches to the center and does not move.
Is there a way to change its location?@Nevez Then first add a vertical layout and then your horizontal layout
-
wrote on 17 Feb 2022, 12:42 last edited by
The position and size of your widget is handled by the layout that contains it.
If you want your widget to be "pushed" to the top, the best is to add a spacer after your widget.You can do it that way:
QVBoxLayout *vbox = new QVBoxLayout(this); vbox->addWidget(widget); vbox->addStretch(1);
See https://doc.qt.io/qt-5/qboxlayout.html#addStretch for more details.
-
The position and size of your widget is handled by the layout that contains it.
If you want your widget to be "pushed" to the top, the best is to add a spacer after your widget.You can do it that way:
QVBoxLayout *vbox = new QVBoxLayout(this); vbox->addWidget(widget); vbox->addStretch(1);
See https://doc.qt.io/qt-5/qboxlayout.html#addStretch for more details.
-
wrote on 17 Feb 2022, 13:44 last edited by
If I understood your problem correctly, you should have 3 layouts:
A QHBoxLayout which contains your buttons (that will span the full width of you window), a QGridLayout that contains the rest of your stuff, and a QVBoxLayout, that contains the other layouts plus optionally a spacer to "push" your buttons at the top (or the bottom).You could have something like that for example :
Try using QtCreator or QtDesigner to understand the layouts, even if in the end you make it all with code.
-
wrote on 17 Feb 2022, 14:36 last edited by
@Fabien-B Thank you very much for your interest and help. I checked the part you mentioned. However, unfortunately, the project I am working on has reached a certain size and I am progressing through the code. However, I still managed to fix the problem.
I was able to push down the widget I wanted to add using "QSpacerItem" as a solution. Then again, there was an irregularity with the other widgets I added. I managed to solve this problem by giving the row and column values different when adding the widgets to the layout. I hope this will be of use to someone. Have a nice day :)
1/12