QtDesigner problem
-
Hi,
I'm trying to learn something about Qt and, currently, QtDesigner
in particular.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
resizing problem:verticalLayoutWidget = new QWidget(centralWidget);
Can someone please tell me what I'm doing wrong in QtDesigner?
Thanks.
Bill
-
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?
Thanks.
Bill
@<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>875</width>
<height>676</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<widget class="QTextEdit" name="textEdit">
<property name="geometry">
<rect>
<x>340</x>
<y>220</y>
<width>104</width>
<height>75</height>
</rect>
</property>
</widget>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>@ -
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">
<item>
<widget class="QTextEdit" name="textEdit"/>
</item>
</layout>
</widget>@ -
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?Thanks again!
Bill
-
Look for "Qt Designer" and "Qt Designer Examples" in the documentation. You have everything there to get started :)