C/C++ -- Gui编程 -- Qt库的使用 -- 使用.ui文件

时间:2022-07-23 10:02:07

1.创建Qt空工程

2.添加Qt设计师界面,无按钮对话框helloqt.ui

3.编辑界面,添加部件,修改对话框对象名为HelloQt

 

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <ui version="4.0">
3 <class>HelloQt</class>
4 <widget class="QDialog" name="HelloQt">
5 <property name="geometry">
6 <rect>
7 <x>0</x>
8 <y>0</y>
9 <width>400</width>
10 <height>300</height>
11 </rect>
12 </property>
13 <property name="windowTitle">
14 <string>Dialog</string>
15 </property>
16 <widget class="QLabel" name="lbl">
17 <property name="geometry">
18 <rect>
19 <x>170</x>
20 <y>140</y>
21 <width>54</width>
22 <height>12</height>
23 </rect>
24 </property>
25 <property name="text">
26 <string>哈喽Qt</string>
27 </property>
28 </widget>
29 </widget>
30 <resources/>
31 <connections/>
32 </ui>

 

4.构建生成Ui头文件''ui_helloqt.h''

5.添加main.cpp,使用构建生成的ui头文件

 

 1 #include "ui_helloqt.h"
2 int main(int argc, char * argv[])
3 {
4 QApplication app(argc, argv);
5 QDialog dlg;
6 Ui::HelloQt ui;
7 ui.setupUi(&dlg);
8 dlg.show();
9 return app.exec();
10 }

 

备注:

 

 ui对象也可以用Ui_HelloQt实例化,因为Ui::HelloQt完全没有更改地继承自Ui_HelloQt

1 namespace Ui {
2 class HelloQt: public Ui_HelloQt {};
3 } //

 命令行下编译.ui文件

uic -o 目标文件.h  源文件.ui

比如 uic -o ui_helloqt.h helloqt.ui