Can't get my widget to auto-resize...
-
...my app has a QPlainTextBox in a QHBoxLayout (in a QWidget). I'd like it to grow vertically with the Widget.
I've read the Layout Management page, and I don't see what I'm missing -- vertical policy is "Expanding," and maximum size is arbitrarily large. Any suggestions what to look for? Thanks.
-
@mzimmers Hi! Here's a minimal example:
#include <QApplication> #include <QWidget> #include <QHBoxLayout> #include <QPlainTextEdit> int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget widget; // main widget; our window auto layout = new QHBoxLayout(&widget); // "layout" it not a QWidget itself, it only manages QWidgets widget.setLayout(layout); // set "layout" as the layout manager for "widget" auto textEdit = new QPlainTextEdit(&widget); // "textEdit" has "widget" as its parent, but it's not managed yet layout->addWidget(textEdit); // tell "layout" to manage "textEdit" widget.show(); return a.exec(); }
-
@Wieland I have tested your code snippet, and it resizes as expected. What version of Qt and OS are you on?
-
@Wieland thanks for the example. I used Designer to build mine. The attributes all look the same as yours...any ideas?
-
Hi,
Are you sure the internal widgets are put in a layout that was applied to the edited widget ?
-
@Wieland: no need to call setLayout if you passed the widget as parent at layout creation time it will be assigned automatically..
-
It looks like groupBoxSecurity and groupBoxUserPwd are not in a layout.
-
If you want that everything be resized automatically, then yes. Otherwise you would need to re-implemt the resize event and then do all the sizing by hand.
-
I bet you already know this:
-
That should be achievable, Try playing with the stretch factor.
-
OK, I just got another data point, though I'm not sure what it means. So, this is my UI without any layouts applied:
This does not do any resizing, nor would I expect it to.This is my UI with a grid layout applied to all child objects:
This does not do any resizing; I thought that it would.
This (the new data point) is with a grid applied to the main widget itself (oddly doesn't show up in the tree):
This does resize.
So, in short...I have no clue at all what I'm doing here. And I haven't even touched on the topic that the layouts are screwing up my UI. (This I presume can be fixed by nested layouts?)
Thanks...
-
The last one looks good (almost no problems). You have to set layouts to your group boxes too and everything will be resizing. Also bear in mind you don't really need a grid layout for the top widget,
vertical one (with 2 items) should do just fine.Edit.
Actually I took a closer look. I'd put the two groups in a nested horizontal layout, the dropdown with buttons in a vertical one. The vertical one and the text area in a horizontal one and set the root widget's layout to vertical.You can judge if there's a layout attached to the widget by looking at the object tree - the red crossed circle means there's no layout. Otherwise the assigned layout is shown as an icon. The child objects you see in the tree will be ordered according to the set layout. In practice using a nested layout isn't very often needed, only occasionally.