It cannot be that hard.... Add layout to central widget retrospectively without being forced to build new gui from scratch
-
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.
-
@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 emptycentralWidget
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 (viauic
), which you include as header (e.g.ui_mainwindow.h
) when using the UI definition file. -
@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-clickcentralWidget
in this object tree above and try to add a layout viaLayout
and then choosevertical
.
[Doesn't work for centralWidget. Only for child widgets with content]
Because there are already child widgets (the tabWidget) added to thecentralWidget
, assigning a layout shouldn't be a problem. -
@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?
-
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) -
@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.
-
@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 emptycentralWidget
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 (viauic
), which you include as header (e.g.ui_mainwindow.h
) when using the UI definition file. -
@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:
Done. You can fix any other layout-free widget in the same way.
-
@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 oncentralWidget
@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 :-/ -
@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.
-
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!
-