I'm trying to learn something about Qt and, currently, QtDesigner
I create a new Qt Widgets Application in QtDesigner. For simplicity, I remove
the menu bar, toolbar, and status bar. Then I add a vertical layout and a text
edit widget inside of that. I expected that the text edit widget would be
resized to fit the main window but it is not; it's size remains fixed as the
main window is resized.
I can get the correct behavior if I write the Qt cpp code by hand. But the cpp code
generated by QtDesigner appears wrong to me. In particular, for some reason it is
creating an extra QWidget inside the centralWidget and that seems to be part of the
verticalLayoutWidget = new QWidget(centralWidget);
Can someone please tell me what I'm doing wrong in QtDesigner?
Hi and welcome to devnet,
If you don't need any feature from QMainWindow, then you should rather start with e.g. a QDialog or just a QWidget. You can't set a layout on a QMainWindow because it already has one for e.g. dock widgets. The central widget is where you would put your QTextEdit.
Hope it helps
Thank you for your reply.
Ultimately I want to create an application based on QMainWindow and was hoping to start with QtDesigner. I started with something very simple, trying to understand the interactions, and unfortunately got stuck there.
I followed your suggestion and removed the layout. Unfortunately, the situation is still the same; the QTextEdit widget does not resize when the main window is resized.
The ui file is so simple that I've attached it below. Is there some trick required to make QtDesigner generate correct cpp code for this case?
@<?xml version="1.0" encoding="UTF-8"?>
<widget class="QMainWindow" name="MainWindow">
<widget class="QWidget" name="centralWidget">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<widget class="QTextEdit" name="textEdit">
<layoutdefault spacing="6" margin="11"/>
From this you file, you just have QTextEdit in your MainWindow area. Go to designer, right click on your mainWindow container, set layout. This will set the Layout for CenteralWidget place only. After this it resizes. I feel it is very straight forward and works.
Just how it showed for me.
@ <widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="verticalLayout">
<widget class="QTextEdit" name="textEdit"/>
Thanks so much for the quick reply!
As expected, your solution works.
My original attempt did include the layout inside the centralWidget but
was clearly different than your solution that modifies the layout of the centralWidget.
Since my main objective at this point is learning about Qt, I'm wondering
if you could point me to the right place in the documentation where I could read more about this?
Look for "Qt Designer" and "Qt Designer Examples" in the documentation. You have everything there to get started :)