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. It cannot be that hard.... Add layout to central widget retrospectively without being forced to build new gui from scratch
Forum Updated to NodeBB v4.3 + New Features

It cannot be that hard.... Add layout to central widget retrospectively without being forced to build new gui from scratch

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 1.7k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    DeSa
    wrote on last edited by
    #1

    Hi,

    during the christmas/new years vacation, I did a coding marathon attempting to write SW which can manipuate, sort, tag, classify (person/face,...) all my images and videos taken since 2015.

    While focussing solely on functionality, I did GUI rather "on demand" and forgot to add a layout to centralWidget as I started the GUI. As a result - as expected - resizing main window does not do any resizing for the 500+ widgets I created with Qt Creator. How can I add a simple vertical layout to centralWidget? I tried opening the ui-file with notepad++ and adding it by habd as via QT Designer it would not let me. But this failed.

    For any suggestions/hints to fix this rather small issue, I thank you up front.

    271e2836-a204-4099-858f-90a02751a0c9-image.png

    Pl45m4P D C 3 Replies Last reply
    0
    • D DeSa

      @Pl45m4 I changed the language settings to english so the screenshot below is more shareable. Unfortunatelly, a right click on central widget does not seem to give me any option to add a layout. Maybe I am just not seeing it....

      I might add it was easier for old folks like me when ui-files were made of c++ code and editable by users, which is not the case today any more if I got this correctly from several other posts here.

      5b00c4f3-35f1-465c-b254-bbc01d6b8ba9-image.png

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #6

      @DeSa said in It cannot be that hard.... Add layout to central widget retrospectively without being forced to build new gui from scratch:

      I changed the language settings to english so the screenshot below is more shareable. Unfortunatelly, a right click on central widget does not seem to give me any option to add a layout. Maybe I am just not seeing it....

      Unfortunately QtDesigner is far from being intuitive. You can remove a layout from centralWidget by removing it from the top-child (which removes the centralWidget layout and not the child one... at least from my experience)
      Try a right click on the empty centralWidget area on your widget and then "Layout" -> "vertical".

      I might add it was easier for old folks like me when ui-files were made of c++ code and editable by users, which is not the case today any more if I got this correctly from several other posts here.

      The Designer should make it easier, which isn't the case, if you know what you are doing and know how to add layouts and widgets by code.

      when ui-files were made of c++ code

      If you write it yourself, you dont need UI files at all.
      The *.ui files are some kind of XML style definitions of your widgets and layouts and become C++ code later (via uic), which you include as header (e.g. ui_mainwindow.h) when using the UI definition file.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0
      • D DeSa

        Hi,

        during the christmas/new years vacation, I did a coding marathon attempting to write SW which can manipuate, sort, tag, classify (person/face,...) all my images and videos taken since 2015.

        While focussing solely on functionality, I did GUI rather "on demand" and forgot to add a layout to centralWidget as I started the GUI. As a result - as expected - resizing main window does not do any resizing for the 500+ widgets I created with Qt Creator. How can I add a simple vertical layout to centralWidget? I tried opening the ui-file with notepad++ and adding it by habd as via QT Designer it would not let me. But this failed.

        For any suggestions/hints to fix this rather small issue, I thank you up front.

        271e2836-a204-4099-858f-90a02751a0c9-image.png

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #2

        @DeSa said in It cannot be that hard.... Add layout to central widget retrospectively without being forced to build new gui from scratch:

        As a result - as expected - resizing main window does not do any resizing for the 500+ widgets I created with Qt Creator.

        You have 500+ widgets in your MainWindow?

        How can I add a simple vertical layout to centralWidget?

        In mainWindow c'tor:

        QVBoxLayout * vbox = new QVBoxLayout(this);
        this->centralWidget()->setLayout(vbox);
        

        The problem is, you not only have to assign the layout, you also have to put all widgets in again.

        Edit:
        You could also right-click centralWidget in this object tree above and try to add a layout via Layout and then choose vertical.
        [Doesn't work for centralWidget. Only for child widgets with content]
        Because there are already child widgets (the tabWidget) added to the centralWidget, assigning a layout shouldn't be a problem.


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        D 1 Reply Last reply
        1
        • Pl45m4P Pl45m4

          @DeSa said in It cannot be that hard.... Add layout to central widget retrospectively without being forced to build new gui from scratch:

          As a result - as expected - resizing main window does not do any resizing for the 500+ widgets I created with Qt Creator.

          You have 500+ widgets in your MainWindow?

          How can I add a simple vertical layout to centralWidget?

          In mainWindow c'tor:

          QVBoxLayout * vbox = new QVBoxLayout(this);
          this->centralWidget()->setLayout(vbox);
          

          The problem is, you not only have to assign the layout, you also have to put all widgets in again.

          Edit:
          You could also right-click centralWidget in this object tree above and try to add a layout via Layout and then choose vertical.
          [Doesn't work for centralWidget. Only for child widgets with content]
          Because there are already child widgets (the tabWidget) added to the centralWidget, assigning a layout shouldn't be a problem.

          D Offline
          D Offline
          DeSa
          wrote on last edited by
          #3

          @Pl45m4 thanks! By 500+ widgets I meant all different classes of widgets like graphicsView, buttons, spacers, checkBoxes, ....

          I am attempting something like what you wrote right now. I thought there was a more elegant way to fix the mess I created but it seems there isn't.

          Do you have an example how to put all widgets in? For instance first level widget which should resize when I resize the main window is the tab widget. Do you have few lines of code how to put the tab widget inside the central widget's layout?

          Pl45m4P 1 Reply Last reply
          0
          • D DeSa

            @Pl45m4 thanks! By 500+ widgets I meant all different classes of widgets like graphicsView, buttons, spacers, checkBoxes, ....

            I am attempting something like what you wrote right now. I thought there was a more elegant way to fix the mess I created but it seems there isn't.

            Do you have an example how to put all widgets in? For instance first level widget which should resize when I resize the main window is the tab widget. Do you have few lines of code how to put the tab widget inside the central widget's layout?

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by
            #4

            @DeSa

            I've edited my answer :)
            I personally don't like QtDesigner. I use it only until a certain point (its possibilities are limited anyway)... if you know that your GUI will be more complex than few widgets and three, four layouts, better start to code your UI yourself.

            Of course only the top-level widgets should be added to centralWidgets layout directly. For sure your widgets have inner widgets/content, right? These widgets also need layouts.
            If you didn't use any layout at all before, you will need to do this as well...
            (We don't know your design. So you have to see what needs to be added where and what layout you need)


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            1 Reply Last reply
            0
            • D DeSa

              Hi,

              during the christmas/new years vacation, I did a coding marathon attempting to write SW which can manipuate, sort, tag, classify (person/face,...) all my images and videos taken since 2015.

              While focussing solely on functionality, I did GUI rather "on demand" and forgot to add a layout to centralWidget as I started the GUI. As a result - as expected - resizing main window does not do any resizing for the 500+ widgets I created with Qt Creator. How can I add a simple vertical layout to centralWidget? I tried opening the ui-file with notepad++ and adding it by habd as via QT Designer it would not let me. But this failed.

              For any suggestions/hints to fix this rather small issue, I thank you up front.

              271e2836-a204-4099-858f-90a02751a0c9-image.png

              D Offline
              D Offline
              DeSa
              wrote on last edited by
              #5

              @Pl45m4 I changed the language settings to english so the screenshot below is more shareable. Unfortunatelly, a right click on central widget does not seem to give me any option to add a layout. Maybe I am just not seeing it....

              I might add it was easier for old folks like me when ui-files were made of c++ code and editable by users, which is not the case today any more if I got this correctly from several other posts here.

              5b00c4f3-35f1-465c-b254-bbc01d6b8ba9-image.png

              Pl45m4P 1 Reply Last reply
              0
              • D DeSa

                @Pl45m4 I changed the language settings to english so the screenshot below is more shareable. Unfortunatelly, a right click on central widget does not seem to give me any option to add a layout. Maybe I am just not seeing it....

                I might add it was easier for old folks like me when ui-files were made of c++ code and editable by users, which is not the case today any more if I got this correctly from several other posts here.

                5b00c4f3-35f1-465c-b254-bbc01d6b8ba9-image.png

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by Pl45m4
                #6

                @DeSa said in It cannot be that hard.... Add layout to central widget retrospectively without being forced to build new gui from scratch:

                I changed the language settings to english so the screenshot below is more shareable. Unfortunatelly, a right click on central widget does not seem to give me any option to add a layout. Maybe I am just not seeing it....

                Unfortunately QtDesigner is far from being intuitive. You can remove a layout from centralWidget by removing it from the top-child (which removes the centralWidget layout and not the child one... at least from my experience)
                Try a right click on the empty centralWidget area on your widget and then "Layout" -> "vertical".

                I might add it was easier for old folks like me when ui-files were made of c++ code and editable by users, which is not the case today any more if I got this correctly from several other posts here.

                The Designer should make it easier, which isn't the case, if you know what you are doing and know how to add layouts and widgets by code.

                when ui-files were made of c++ code

                If you write it yourself, you dont need UI files at all.
                The *.ui files are some kind of XML style definitions of your widgets and layouts and become C++ code later (via uic), which you include as header (e.g. ui_mainwindow.h) when using the UI definition file.


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                1 Reply Last reply
                0
                • D DeSa

                  Hi,

                  during the christmas/new years vacation, I did a coding marathon attempting to write SW which can manipuate, sort, tag, classify (person/face,...) all my images and videos taken since 2015.

                  While focussing solely on functionality, I did GUI rather "on demand" and forgot to add a layout to centralWidget as I started the GUI. As a result - as expected - resizing main window does not do any resizing for the 500+ widgets I created with Qt Creator. How can I add a simple vertical layout to centralWidget? I tried opening the ui-file with notepad++ and adding it by habd as via QT Designer it would not let me. But this failed.

                  For any suggestions/hints to fix this rather small issue, I thank you up front.

                  271e2836-a204-4099-858f-90a02751a0c9-image.png

                  C Offline
                  C Offline
                  ChrisW67
                  wrote on last edited by
                  #7

                  @DeSa said in It cannot be that hard.... Add layout to central widget retrospectively without being forced to build new gui from scratch:

                  How can I add a simple vertical layout to centralWidget?

                  Perhaps I have misunderstood the question, but this appears to be a two-click job:
                  9572d0eb-be2d-43f8-a3d4-4eeed40a7888-image.png

                  Done. You can fix any other layout-free widget in the same way.
                  5ff73ea7-e3e1-4e61-8f1c-30c916201d40-image.png

                  Pl45m4P 1 Reply Last reply
                  1
                  • C ChrisW67

                    @DeSa said in It cannot be that hard.... Add layout to central widget retrospectively without being forced to build new gui from scratch:

                    How can I add a simple vertical layout to centralWidget?

                    Perhaps I have misunderstood the question, but this appears to be a two-click job:
                    9572d0eb-be2d-43f8-a3d4-4eeed40a7888-image.png

                    Done. You can fix any other layout-free widget in the same way.
                    5ff73ea7-e3e1-4e61-8f1c-30c916201d40-image.png

                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by Pl45m4
                    #8

                    @ChrisW67 said in It cannot be that hard.... Add layout to central widget retrospectively without being forced to build new gui from scratch:

                    Perhaps I have misunderstood the question, but this appears to be a two-click job:

                    At first I also thought so, that's why I wrote the first post... Rightclick, assign layout, done.
                    But I tried it as @DeSa also did and QtDesigner really don't let you do this on centralWidget

                    @ChrisW67 What happens if you rightclick and try the layout menu action? If this works, why not Layout via context menu on rightclick?! Which should do the same, usually...
                    QtDesigner is really pretty bad and unintuitive :-/


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    JonBJ 1 Reply Last reply
                    0
                    • Pl45m4P Pl45m4

                      @ChrisW67 said in It cannot be that hard.... Add layout to central widget retrospectively without being forced to build new gui from scratch:

                      Perhaps I have misunderstood the question, but this appears to be a two-click job:

                      At first I also thought so, that's why I wrote the first post... Rightclick, assign layout, done.
                      But I tried it as @DeSa also did and QtDesigner really don't let you do this on centralWidget

                      @ChrisW67 What happens if you rightclick and try the layout menu action? If this works, why not Layout via context menu on rightclick?! Which should do the same, usually...
                      QtDesigner is really pretty bad and unintuitive :-/

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

                      @Pl45m4
                      If you/the OP follow exactly what @ChrisW67 shows in his screenshot it works. For whatever reason, you cannot do it by right-click on the central widget but you can do it by clicking a layout in the menubar so do that!?

                      Otherwise the OP always has two other options:

                      • Use copy and paste to remove what is there, place a layout or whatever, and paste back the original content.
                      • Create a new MainWindow project, put the layout on, look at the XML in the .ui file and copy/paste in the couple of lines which insert the missing layout. Ugly, but done.
                      D 1 Reply Last reply
                      1
                      • JonBJ JonB

                        @Pl45m4
                        If you/the OP follow exactly what @ChrisW67 shows in his screenshot it works. For whatever reason, you cannot do it by right-click on the central widget but you can do it by clicking a layout in the menubar so do that!?

                        Otherwise the OP always has two other options:

                        • Use copy and paste to remove what is there, place a layout or whatever, and paste back the original content.
                        • Create a new MainWindow project, put the layout on, look at the XML in the .ui file and copy/paste in the couple of lines which insert the missing layout. Ugly, but done.
                        D Offline
                        D Offline
                        DeSa
                        wrote on last edited by
                        #10

                        @JonB , @ChrisW67 , @PI45m4,

                        Many thanks for your help. It worked. I used the screenshot below from @ChrisW67 and applied it to each container widget starting with central widget and now it resizes perfectly. There is a bug in Qt Creator - I had to apply this numerous times until it actually set the layout to the container. But it worked.

                        Many thanks once again!

                        52f087bc-7825-401f-8ea3-4096acb3fd08-image.png

                        1 Reply Last reply
                        0
                        • D DeSa has marked this topic as solved on

                        • Login

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