How to create this simple form by code?
-
Hi. Look...
Just a QPlainTextEdit and a QStatusBar.
I don't feel like designing an UI for that. Anyway, I'd like to know how to do it by code, since the results I'm getting don't look like what the Designer creates.
This is the source of mainwindow.ui:
<?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>800</width> <height>600</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <layout class="QVBoxLayout" name="verticalLayout"> <item> <widget class="QPlainTextEdit" name="plainTextEdit"/> </item> </layout> </widget> <widget class="QStatusBar" name="statusbar"/> </widget> <resources/> <connections/> </ui>
Question: does this file defines every single thing required to create the form? If yes, I assume that every undefined property will be set to their defaults. Shouldn't this happen when you create the components by code?
Question: how do I create that form by hand? Is it possible to "follow" the previous dead simple XML instructions and get the same result?
Thanks for your help. :)
-
@Alvein said:
Question: does this file defines every single thing required to create the form? If yes, I assume that every undefined property will be set to their defaults. Shouldn't this happen when you create the components by code?
This file gets translated directly to C++ and as such it follows the normal C++ rules - a class is constructed and members follow the normal construction rules. All Qt ui classes have their members initialized to some defaults so yes, anything you don't define here will get defaulted.
Question: how do I create that form by hand? Is it possible to "follow" the previous dead simple XML instructions and get the same result?
You can pretty much translate it line by line into C++. For example:
<widget class="QMainWindow" name="MainWindow">
This translates to
QMainWindow* mw = new QMainWindow(); mv->setObjectName("MainWindow);
Going further:
<property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>600</height> </rect> </property>
translates to
mw->setGeometry(0, 0, 800, 600);
and so on.
In fact this translation is exactly what the
uic
tool does when you use that xml in your project so you can just take a look at its output and you'll see exactly the code it generated (it's inside thesetupUi()
function called in the MainWindow's contructor). It will be very much a line by line translation like I mentioned. -
@Alvein
Do you understand that the build process runs https://doc.qt.io/qt-5/uic.html ? That reads through themainwindow.ui
file and produces (I think) amainwindow.cpp
&mainwindow.h
. If you want to be sure you get exactly what it produces, look through that code. Yes, most things are explicitly set to some defaults, or left to inherit the Qt widget defaults.Having seen what
uic
generates, you could write & maintain something like that yourself if you want to create a widget without designing it for the.ui
file. The example you give could be written in half a dozen explicit statements. If that's what you were asking. -
Thank you very much, guys.
The file I had to check was "ui_mainwindow.h".
Inside, I found the thing I was missing:
MainWindow->setStatusBar(statusbar);
The rest of instructions are pretty much like the ones I tried.
But without the previous like, I was trying to just set the status bar at the bottom of the layout, with no success. I still think that there should be a way of doing that without using setStatusBar().
Cheers!
-
@Alvein
Yep, that's why looking at the generated code is useful :)See the picture at https://doc.qt.io/qt-5/qmainwindow.html#details. This shows how a
QMainWindow
is laid out, and goes on to explain how you can affect each of the reserved areas.QMainWindow
is not "magic", it's simply aQWidget
window with a predesigned generic layout for you to follow.