Through the use of Qt Creator, we can quickly design the interface with designer (interface designer).
This example UI content is as follows (simply adding a button):
The code directory structure of the project is as follows:
Eventually a UI file is generated in the directory where the project is located:
This UI file is actually an XML file:
when we compile QT program code, QT Creator uses the UIC tool to convert the contents of the UI file into C + + code , and automatically generates UI_ class name in the build-directory of the same directory as the project directory. h file, as in this example Ui_mywidge T.h, is made by Mywidget.ui using UIC tools, as long as through the Designer to modify the contents of the graphical interface, Ui_mywidget.h will also follow the automatic synchronization update content, Ui_mywidget.h is automatically generated , the user can write code without care about its implementation process :
Next, let's analyze how this ui_mywidget.h and QT program code relate (i.e. UI files and QT code relationships).
A namespace declaration is automatically added to the mywidget.h , and there are more UI pointer object members in the class:
The mywidget.h corresponding mywidget.cpp implementation process is as follows:
Next, let's look at the ui_mywidget.h implementation process:
With this step-by-step analysis, we find that the designer UI interface is ultimately converted to C + + code (UI_ class name), which is similar to the Qt code we write, which means that the code is the king. Designer (Interface Designer) just helps us to design the interface quickly, without it, we can also write Qt program . If we are not familiar with the operation of some parts , we do not know how to use their corresponding functions, we drag this part through the designer (interface Designer), modify the required properties, and then compile, see how its automatic conversion of C + + code implementation, This will not only learn the use of their corresponding functions, so thatdesigner (interface Designer) itself is a good teacher to learn Qt .
So how do we modify the properties of the UI file part through code?
In design mode, each part has a "objectName" (object name) attribute in "qobject":
The value of the property of "ObjectName" (The object name) (the value in this example, "pushbutton"), which is the name of the object pointer automatically created in "Ui_ class name. h":
So, in a function of the. cpp file, we can refer to this member through "ui->" , in Qt Creator, ". (dot) "Key is smart, or" if you are working with a normal object. (point), if the object is pointing to, automatically changesto, and prompts the member that can be referenced:
Here, the contents of the pushbutton are modified by code:
After compiling the run program, the contents of the button are actually modified:
Copyright NOTICE: This blog post, mostly I compiled, or in the network collection, reproduced please indicate the source!!
QT Introductory learning--qt UI files and QT code relationships in Creator