How force a widget to fill the empty space of another hidden widget?
-
Hi,
I work on a application where it's GUI looks this:
| 1 | 2 |
| 3 | 4 |
All the 4 widgets are shown (not hidden) at the beginning. It has a menu with view options where the user can hide widget number 2, 3 and 4.
I want the widget number one stretch and use the empty place of the other hidden widgets. The size policy is expanding for both vertical and horizontal. I have changed the stretch horizontal and vertical to 100 but it did not work. Also, there is no limitation in maximum size of the widgets.
I should mention that the whole layout is in Grid format.
I would be thankful if someone help me :)
-
Hi,
What exact layout are you using ?
Grids can be achieved using QGridLayout but also a combination of QVBoxLayout and QHBoxLayout.
-
Hi,
What exact layout are you using ?
Grids can be achieved using QGridLayout but also a combination of QVBoxLayout and QHBoxLayout.
-
In that case you have to change the span of widget 1 when hiding widget 2. This likely means that you have to pull widget one of the grid and put it back in with the appropriate span value.
-
In that case you have to change the span of widget 1 when hiding widget 2. This likely means that you have to pull widget one of the grid and put it back in with the appropriate span value.
@SGaist I can use resize() only when there is no Layout. When I set the grid layout the resize does not work. When there are only two vertical widgets (in my example 1 and 3), it works perfectly.
But, with all the four widgets:
- If I only hide number 3, number 1 does NOT extend.
- If I hide both number 3 and number 4 then number 1 and 2 extend and fill the empty place bellow them.
It looks the grid system wants 1 and 2 be have same height.
-
@SGaist I can use resize() only when there is no Layout. When I set the grid layout the resize does not work. When there are only two vertical widgets (in my example 1 and 3), it works perfectly.
But, with all the four widgets:
- If I only hide number 3, number 1 does NOT extend.
- If I hide both number 3 and number 4 then number 1 and 2 extend and fill the empty place bellow them.
It looks the grid system wants 1 and 2 be have same height.
@Bamshad there are a couple of ways to do this, Like @SGaist said, is one.
If you don't force stretch factors and set the size policy to expanding, then you don't have to do anything. Like in this small example:
int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget w; QVBoxLayout *vLay = new QVBoxLayout(&w); QPushButton *btn = new QPushButton("Hide/Show"); QGridLayout *gLay = new QGridLayout; QLabel *l = new QLabel("Widget 1"); l->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); l->setStyleSheet("background-color:red;"); gLay->addWidget(l,0,0,1,1); l = new QLabel("Widget 2"); l->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); l->setStyleSheet("background-color:blue;"); QObject::connect(btn, &QPushButton::clicked, l, [l]()->void{l->setVisible(!l->isVisible());}); gLay->addWidget(l,0,1,1,1); l = new QLabel("Widget 3"); l->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); l->setStyleSheet("background-color:yellow;"); QObject::connect(btn, &QPushButton::clicked, l, [l]()->void{l->setVisible(!l->isVisible());}); gLay->addWidget(l,1,0,1,1); l = new QLabel("Widget 4"); l->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); l->setStyleSheet("background-color:green;"); QObject::connect(btn, &QPushButton::clicked, l, [l]()->void{l->setVisible(!l->isVisible());}); gLay->addWidget(l,1,1,1,1); vLay->addLayout(gLay,1); vLay->addWidget(btn); w.show(); return a.exec(); }
-
I didn't mention resize at any point. I wrote about the span values that you can use to make widgets use more than one column/row.
-
I didn't mention resize at any point. I wrote about the span values that you can use to make widgets use more than one column/row.
-
I already suggested a possible way to implement that, didn't I ?
-
You'll see them once you add the widgets where you want them.