在Qt 5.12.8 (MinGW 7.3.0 64-bit)控制台中执行:
xxxxxxxxxx
11C:\Qt\Qt5.12.8\5.12.8\mingw73_64>qtcreator
点击“New”按钮,或选择“文件”菜单中的“新建文件或项目...”菜单项,弹出“项目模板”对话框。
在“项目模板”对话框的左侧窗格中选择“Application”,右侧窗格中选择“Qt Widgets Application”,点击“Choose...”按钮,弹出“项目配置”对话框。
在“项目配置”对话框的“项目位置”页面中指定项目的名称为“Calculator2”,创建路径为“C:\Users\Minwei\Projects\Qt”,点击“下一步”按钮,进入“构建器”页面。
在“构建器”页面中选择使用“qmake”,点击“下一步”按钮,进入“详情”页面。
在“详情”页面中设置主窗口的详细信息:
类名:CalculatorDialog
基类:QDialog
头文件:calculatordialog.h
源文件:calculatordialog.cpp
创建界面:是
界面文件:calculatordialog.ui
点击“下一步”按钮,进入“翻译文件”页面。
在“翻译文件”页面中设置翻译文件的语言和文件名,翻译文件不是必须的,这步可直接跳过,点击“下一步”按钮,进入“SDK”页面。
在“SDK”页面中选择“Desktop Qt 5.12.8 MinGW 64-bit”,点击“下一步”按钮,进入“项目管理”页面。
在“项目管理”页面中指定父级项目和版本控制,浏览项目中的文件摘要,点击“完成”按钮,生成项目框架代码。
双击项目浏览器窗格中的calculatordialog.ui文件,启动Qt设计师,设计计算器界面。
C:\Users\Minwei\Projects\Qt\Calculator2\calculatordialog.ui:
xxxxxxxxxx
651
2<ui version="4.0">
3 <class>CalculatorDialog</class>
4 <widget class="QDialog" name="CalculatorDialog">
5 <property name="geometry">
6 <rect>
7 <x>0</x>
8 <y>0</y>
9 <width>499</width>
10 <height>43</height>
11 </rect>
12 </property>
13 <property name="windowTitle">
14 <string>计算器</string>
15 </property>
16 <layout class="QHBoxLayout" name="m_layout">
17 <item>
18 <widget class="QLineEdit" name="m_editX">
19 <property name="alignment">
20 <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
21 </property>
22 </widget>
23 </item>
24 <item>
25 <widget class="QLabel" name="m_label">
26 <property name="text">
27 <string>+</string>
28 </property>
29 <property name="alignment">
30 <set>Qt::AlignCenter</set>
31 </property>
32 </widget>
33 </item>
34 <item>
35 <widget class="QLineEdit" name="m_editY">
36 <property name="alignment">
37 <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
38 </property>
39 </widget>
40 </item>
41 <item>
42 <widget class="QPushButton" name="m_button">
43 <property name="enabled">
44 <bool>false</bool>
45 </property>
46 <property name="text">
47 <string>=</string>
48 </property>
49 </widget>
50 </item>
51 <item>
52 <widget class="QLineEdit" name="m_editZ">
53 <property name="alignment">
54 <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
55 </property>
56 <property name="readOnly">
57 <bool>true</bool>
58 </property>
59 </widget>
60 </item>
61 </layout>
62 </widget>
63 <resources/>
64 <connections/>
65</ui>
注意,这里使用了布局,使各组件在父窗口中的排布更加自动化。当父窗口的水平和垂直尺寸发生变化时,其内部各组件的位置和大小也会随之发生变化。
将项目配置中的“Shadow build”选项置为“否”。点击位于Qt创造器主界面左下角的“运行”按钮,或者选择“构建”菜单中的“运行”菜单项,当然也可以使用快捷键“Ctrl+R”。项目将被构建并运行,其效果如图所示:
所得到的可执行程序位于C:\Users\Minwei\Projects\Qt\Calculator2\debug目录下,名为Calculator2.exe。
在Qt创造器中构建并运行项目的Release版本,注意将“Shadow build”选项置为“否”。所得到的可执行程序位于C:\Users\Minwei\Projects\Qt\Calculator2\release目录下,名为Calculator2.exe。创建一个独立的目录,将release目录下的可执行程序复制到该目录下,比如:
xxxxxxxxxx
21C:\Users\Minwei\Projects\Qt\Calculator2>md windeploy
2C:\Users\Minwei\Projects\Qt\Calculator2>copy release\Calculator2.exe windeploy
在Qt 5.12.8 (MinGW 7.3.0 64-bit)控制台中执行:
xxxxxxxxxx
11C:\Users\Minwei\Projects\Qt\Calculator2\windeploy>windeployqt Calculator2.exe
将该目录下的所有文件连同子目录打包发布即可。即使目标计算机上并未安装Qt,可执行程序依然可以正常运行。