Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
How to set initial height for a widget?
This might be a newbie question, but I'm not finding the answer in the documentation.
I have QMainWindow holding a QVBoxLayout with several widgets. How do I set the height of the individual widgets? I see how to set the maximum and minimum values, but that limits resizing. I just want a default start size, and let the operator free to adjust it if they want.
So far, I've looked at setGeometry, setBaseSize and a couple others. Resize() works for the main window, but doesn't seem to work for interior widgets.
Is the only answer to create a UI view of this and tweak things graphically? I'm trying to figure things out one at a time, and I'm worried that might be a new can of worms.
MuldeR last edited by
Well, the idea about layouts is that you don't need to worry about the exact size or position of the widget. Instead, the layout will apply the size and the position that it deems "optimal". This size of a widget will be determined based on the type of layout, but also based on the Widget's "recommended" size - see sizeHint() method - and of course based on the available space. For example, if the window is resized, the widget sizes and positions get updated automatically, which is a great thing! You can still position widgets without a layout, if you want to. But then you have to take care of everything manually. Once you are used to layouts, you probably don't want that ;-)
Thanks for the reply.
Yes, for the most part it is fine, but I'd still like to tweak the proportions. QTableView and QTextEdit both seem to make assumptions that aren't true for my situation. I don't see any way to set a default size for that, and you would think that's kind of a basic need for a GUI. I want one a little taller and the other a little shorter.
Even if I add a widget without a layout, resize() or setGeometry() for that widget is apparently overwritten by the main window (which has its own layout, but I can resize the whole thing).
I'd be interested in the philosophy of the approaches (when to use layouts, when to use the UI file) all at a general level. Does such a document exist?
MuldeR last edited by
When you add a Widget without a layout, then you definitely can (and have to) set the absolute position and size. Also you make it sound like using layouts and using UI files is mutual exclusive. But it isn't at all! Usually you will design your GUI in the QtDesigner, with the help of layouts, and then save the result to an .ui file. But without an example it is hard to tell, what your "problem" is. Maybe, if you experiment with layouts a bit in QtDesigner, you'll find a pleasing solution. Usually working with layouts is very straight forward...
I didn't see that with my widget. I should add I'm using Qt 5.2
I did solve the problem though - I created a new a new layout that inherits from QVBoxLayout and only overrides sizeHint(). Does exactly what I want. I could get fancy and add setRecommendedHeight() and setRecommendedWidth() that sizeHint() would use, but like you're saying, using QtDesigner is probably the best way to go.
Thanks for the help.