窗口程序写在ui文件中

窗口程序写在ui文件中

1. main.cpp

1
2
3
4
5
6
7
8
9
10
11
#include "widget.h"

#include <QApplication>

int main(int argc, char *argv[]) {
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}

2. widget.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE

class Widget : public QWidget {
Q_OBJECT

public:
Widget(QWidget *parent = nullptr);
~Widget();

private:
Ui::Widget *ui;
};
#endif // WIDGET_H

3. widget.cpp

1
2
3
4
5
6
7
8
9
#include "widget.h"
#include "./ui_widget.h"

Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) {
ui->setupUi(this);
}

Widget::~Widget() { delete ui; }

4. widget.ui

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
</widget>
<resources/>
<connections/>
</ui>

5. CMakelists.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
cmake_minimum_required(VERSION 3.16)
project(MyQtApp LANGUAGES C CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_PREFIX_PATH "C:\\D\\Program\\Qt\\6.2.4\\MSVC2022_x64_static_624_Release")

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Force x64 build
set(CMAKE_GENERATOR_PLATFORM x64)
set(CMAKE_GENERATOR_TOOLSET host=x64)

# Force Release build
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CONFIGURATION_TYPES Release)
# 设置 C++ 标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Widgets)
find_package(Qt6 REQUIRED COMPONENTS Core Widgets)

aux_source_directory(./src srcs)

add_executable(${PROJECT_NAME}
WIN32 # If you need a terminal for debug, please comment this statement
${srcs}
)
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin")

target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::Core Qt6::Widgets)
# Add static build settings

target_compile_definitions(${PROJECT_NAME} PRIVATE
QT_STATICPLUGIN
QT_NO_DEBUG
NDEBUG
)

6. 编译脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash

# 1. 检查并清理构建目录
if [ -d "build" ]; then
echo "清理旧的构建目录..."
rm -rf build
fi
mkdir build
cd build

# 2. 配置项目
# -A x64 指定生成 64 位程序
# 如果你安装了多个 VS 版本,建议显式指定 -G "Visual Studio 17 2022"
echo "正在使用 MSVC 配置项目..."
cmake .. -G "Visual Studio 17 2022" -A x64

# 3. 构建项目
# MSVC 是多配置生成器,所以必须指定 --config Release
echo "正在编译 (Release 模式)..."
cmake --build . --config Release -j8

# 4. (可选) 自动部署 Qt 依赖
# 注意:即使你用 MSVC 编译,也要用 MSVC 版本的 windeployqt
# C:/path/to/msvc_64/bin/windeployqt.exe ../bin/MyQtApp.exe

echo "构建完成!"