Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to fit a QWidget children in a QWidget parent?

How to fit a QWidget children in a QWidget parent?

Scheduled Pinned Locked Moved Solved General and Desktop
qwidgetgeometryparent & childsizesizepolicy
8 Posts 3 Posters 10.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cristiano.narcisi
    wrote on last edited by
    #1

    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

    JonBJ 1 Reply Last reply
    0
    • C cristiano.narcisi

      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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #3

      @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
      
      C 2 Replies Last reply
      3
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #2

        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.

        C 1 Reply Last reply
        3
        • C cristiano.narcisi

          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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #3

          @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
          
          C 2 Replies Last reply
          3
          • mrjjM mrjj

            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.

            C Offline
            C Offline
            cristiano.narcisi
            wrote on last edited by
            #4

            @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

            1 Reply Last reply
            0
            • JonBJ JonB

              @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
              
              C Offline
              C Offline
              cristiano.narcisi
              wrote on last edited by
              #5

              @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!!!

              1 Reply Last reply
              0
              • JonBJ JonB

                @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
                
                C Offline
                C Offline
                cristiano.narcisi
                wrote on last edited by
                #6

                @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!!!

                JonBJ 1 Reply Last reply
                0
                • C cristiano.narcisi

                  @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!!!

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #7

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

                  C 1 Reply Last reply
                  0
                  • JonBJ JonB

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

                    C Offline
                    C Offline
                    cristiano.narcisi
                    wrote on last edited by
                    #8

                    @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!!!

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved