Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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, like setLayout(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
    

  • Lifetime Qt Champion

    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, like setLayout(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
    


  • @mrjj
    Thanks ... i have just take a look to it.... Understanding the relationship between sizeHint and sizePolicy is a little bit complex, but i has been very useful for me!!! Thank you very much



  • @JonB That is what exactly need!!! I have spent short time for understanding how to use size policy of childrend widget but now i have all at the right place and with the correct dimensions. Thanks a lot!!!



  • @JonB @mrjj
    It's for also for people with your skills and then able to give the best solution in a very short time that i keep promoting Qt and its forum to all... Thanks for your help. I hope to be able to help you in some other issue!!!



  • @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 :)



  • @JonB
    I am passing to QML for this reason. There, placement and dimensioning of graph oblect is very easy to do. I like also the stong division between frontend a nd backend!!!