How to fit a QWidget children in a QWidget parent?
-
Hi all
i am Cristiano and i am struggling with the problem i am going to describe in this topic. I am working of a projects that is made of a few of widget ( i will name them "children") that are placed in several pages ( bigger widgets, i will name them "parent") in different position and size. Let me try to give you a snippet of code:Parent::Parent(QWidget *parent) :QWidget(parent),ui(new Ui::Parent) { this->child1 = new Child(this->ui->widget1); this->child2 = new Child(this->ui->widget2); }
The snippet of code shows the constructor of the Parent class. In it i have created two instances of Child and passed them the right parent. That makes the placement working good ( child1 and child2 are placed where i expect they must be), but their size don't match with the size of the parents.
How can i do that?
Your help is very appreciated as usual
Thanks
Cristiano
-
@cristiano-narcisi
To get layout (position, size, etc.) right, you are supposed to add widgets onto the layout of their parent. Your code adds them directly onto the parent widget without layout, and by and large that's not good.this->child1 = new Child(this->ui->widget1); this->ui->widget1->layout()->addWidget(child1); this->child2 = new Child(this->ui->widget2); this->ui->widget2->layout()->addWidget(child1);
Make sure your
widget1/2
have a layout, likesetLayout(new QHBoxLayout)
, before this is called to add things onto them (or do it here, not so good).Note that when you add a widget onto another widget's layout, it automatically sets the child widget's parent for you, So you could write above with just:
this->child1 = new Child(); // no parent at this point this->ui->widget1->layout()->addWidget(child1); // also does a `child1->setParent(this->ui->widget1)` for you, automatically
-
Hi
The trick is to use layouts. Layouts will adjust the size of its widgets to use all space from the
the parent.https://doc.qt.io/qt-5/layout.html
You apply the layout to the parent and then insert the widgets into the layout.
-
@cristiano-narcisi
To get layout (position, size, etc.) right, you are supposed to add widgets onto the layout of their parent. Your code adds them directly onto the parent widget without layout, and by and large that's not good.this->child1 = new Child(this->ui->widget1); this->ui->widget1->layout()->addWidget(child1); this->child2 = new Child(this->ui->widget2); this->ui->widget2->layout()->addWidget(child1);
Make sure your
widget1/2
have a layout, likesetLayout(new QHBoxLayout)
, before this is called to add things onto them (or do it here, not so good).Note that when you add a widget onto another widget's layout, it automatically sets the child widget's parent for you, So you could write above with just:
this->child1 = new Child(); // no parent at this point this->ui->widget1->layout()->addWidget(child1); // also does a `child1->setParent(this->ui->widget1)` for you, automatically
-
-
@cristiano-narcisi
Can't speak for @mrjj, but I accept cash or cheque donations... ;-) [ <- Joke! ]Yes, it takes a while to get the hang of the layout stuff, and I find the Qt Creator interface to widget layout odd. I am still learning/struggling :)