第一个 Qt 程序 #
创建项目 #
使用 Qt Creator 创建项目 #
text
步骤 1:新建项目
┌─────────────────────────────────────────────────────────────┐
│ │
│ 1. 打开 Qt Creator │
│ 2. 文件 -> 新建文件或项目 │
│ 3. 选择 Application -> Qt Widgets Application │
│ 4. 点击 "选择..." │
│ │
└─────────────────────────────────────────────────────────────┘
步骤 2:项目设置
┌─────────────────────────────────────────────────────────────┐
│ │
│ 名称:HelloQt │
│ 创建路径:选择合适的目录 │
│ 构建系统:qmake (或 CMake) │
│ │
└─────────────────────────────────────────────────────────────┘
步骤 3:类信息
┌─────────────────────────────────────────────────────────────┐
│ │
│ 基类:QMainWindow / QWidget / QDialog │
│ 类名:MainWindow │
│ 头文件:mainwindow.h │
│ 源文件:mainwindow.cpp │
│ 生成界面文件:勾选 │
│ │
└─────────────────────────────────────────────────────────────┘
步骤 4:选择构建套件
┌─────────────────────────────────────────────────────────────┐
│ │
│ 选择已配置的 Kit │
│ 例如:Desktop Qt 6.x.x MinGW 64-bit │
│ │
└─────────────────────────────────────────────────────────────┘
最简单的 Qt 程序 #
纯代码版本 #
cpp
// main.cpp
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel label("Hello, Qt!");
label.setWindowTitle("My First Qt App");
label.resize(300, 100);
label.setAlignment(Qt::AlignCenter);
label.show();
return app.exec();
}
代码解析 #
text
┌─────────────────────────────────────────────────────────────┐
│ 代码结构解析 │
├─────────────────────────────────────────────────────────────┤
│ │
│ #include <QApplication> │
│ │ │
│ └── 引入应用程序类,管理整个应用程序 │
│ │
│ #include <QLabel> │
│ │ │
│ └── 引入标签控件,用于显示文本 │
│ │
│ int main(int argc, char *argv[]) │
│ │ │
│ └── 标准 C++ main 函数入口 │
│ │
│ QApplication app(argc, argv); │
│ │ │
│ └── 创建 Qt 应用程序对象 │
│ - 每个 Qt GUI 程序必须有且只有一个 │
│ - 处理命令行参数 │
│ - 管理事件循环 │
│ │
│ QLabel label("Hello, Qt!"); │
│ │ │
│ └── 创建标签控件并设置文本 │
│ │
│ label.show(); │
│ │ │
│ └── 显示控件 │
│ │
│ return app.exec(); │
│ │ │
│ └── 启动事件循环 │
│ - 进入等待用户交互状态 │
│ - 返回应用程序退出码 │
│ │
└─────────────────────────────────────────────────────────────┘
完整的 Qt Widgets 项目 #
项目文件结构 #
text
HelloQt/
├── HelloQt.pro # 项目配置文件
├── main.cpp # 主程序入口
├── mainwindow.h # 主窗口头文件
├── mainwindow.cpp # 主窗口实现
└── mainwindow.ui # 界面设计文件
项目配置文件 (.pro) #
qmake
# HelloQt.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
主程序入口 (main.cpp) #
cpp
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
主窗口头文件 (mainwindow.h) #
cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
主窗口实现 (mainwindow.cpp) #
cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 设置窗口标题
this->setWindowTitle("Hello Qt");
// 设置窗口大小
this->resize(400, 300);
}
MainWindow::~MainWindow()
{
delete ui;
}
Qt 程序运行流程 #
text
┌─────────────────────────────────────────────────────────────┐
│ Qt 程序运行流程 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 1. 程序启动 │
│ │ │
│ ▼ │
│ 2. 创建 QApplication 对象 │
│ │ │
│ │ - 初始化 Qt 应用程序 │
│ │ - 处理命令行参数 │
│ │ - 设置应用程序属性 │
│ │ │
│ ▼ │
│ 3. 创建主窗口 │
│ │ │
│ │ - 初始化界面控件 │
│ │ - 设置信号槽连接 │
│ │ - 加载界面资源 │
│ │ │
│ ▼ │
│ 4. 显示窗口 (show) │
│ │ │
│ ▼ │
│ 5. 进入事件循环 (exec) │
│ │ │
│ │ ┌─────────────────────────────────┐ │
│ │ │ 事件循环 │ │
│ │ │ ┌─────────────────────────┐ │ │
│ │ │ │ 等待事件 │ │ │
│ │ │ │ ├── 鼠标事件 │ │ │
│ │ │ │ ├── 键盘事件 │ │ │
│ │ │ │ ├── 定时器事件 │ │ │
│ │ │ │ └── 自定义事件 │ │ │
│ │ │ │ │ │ │ │
│ │ │ │ ▼ │ │ │
│ │ │ │ 分发事件到对应处理器 │ │ │
│ │ │ └─────────────────────────┘ │ │
│ │ └─────────────────────────────────┘ │
│ │ │
│ ▼ │
│ 6. 用户关闭窗口 │
│ │ │
│ ▼ │
│ 7. 退出事件循环 │
│ │ │
│ ▼ │
│ 8. 程序结束 │
│ │
└─────────────────────────────────────────────────────────────┘
添加交互功能 #
带按钮的示例 #
cpp
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QMessageBox>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
window.setWindowTitle("Interactive Qt App");
window.resize(300, 200);
QVBoxLayout *layout = new QVBoxLayout(&window);
QPushButton *button = new QPushButton("Click Me!", &window);
layout->addWidget(button);
// 连接信号与槽
QObject::connect(button, &QPushButton::clicked, [&]() {
QMessageBox::information(&window, "Message", "Button Clicked!");
});
window.show();
return app.exec();
}
使用 Qt Designer 设计界面 #
text
┌─────────────────────────────────────────────────────────────┐
│ Qt Designer 使用 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 1. 双击打开 mainwindow.ui │
│ │
│ 2. 从控件箱拖拽控件到设计区域 │
│ - Push Button │
│ - Label │
│ - Line Edit │
│ │
│ 3. 设置控件属性 │
│ - objectName:控件名称 │
│ - text:显示文本 │
│ - geometry:位置和大小 │
│ │
│ 4. 使用布局管理器 │
│ - 水平布局 │
│ - 垂直布局 │
│ - 网格布局 │
│ │
│ 5. 信号槽编辑器 │
│ - 连接信号与槽 │
│ - 编辑连接 │
│ │
└─────────────────────────────────────────────────────────────┘
编译和运行 #
使用 Qt Creator #
text
编译运行步骤:
┌─────────────────────────────────────────────────────────────┐
│ │
│ 1. 选择构建配置 │
│ - Debug:调试版本 │
│ - Release:发布版本 │
│ │
│ 2. 编译项目 │
│ - 快捷键:Ctrl+B │
│ - 或点击左下角锤子图标 │
│ │
│ 3. 运行项目 │
│ - 快捷键:Ctrl+R │
│ - 或点击绿色运行按钮 │
│ │
│ 4. 调试项目 │
│ - 快捷键:F5 │
│ - 或点击调试图标 │
│ │
└─────────────────────────────────────────────────────────────┘
命令行编译 #
bash
# 进入项目目录
cd HelloQt
# 生成 Makefile
qmake
# 编译
make # Linux/macOS
mingw32-make # Windows MinGW
nmake # Windows MSVC
# 运行
./HelloQt # Linux/macOS
HelloQt.exe # Windows
程序输出 #
text
运行结果:
┌─────────────────────────────────────────┐
│ Hello Qt [_][□][X]│
├─────────────────────────────────────────┤
│ │
│ │
│ Hello, Qt! │
│ │
│ │
└─────────────────────────────────────────┘
常见问题 #
问题 1:找不到 Qt 头文件 #
text
错误信息:
fatal error: QApplication: No such file or directory
解决方案:
1. 检查 .pro 文件是否正确配置
QT += core gui widgets
2. 运行 qmake 重新生成 Makefile
Qt Creator -> 构建 -> 运行 qmake
问题 2:链接错误 #
text
错误信息:
undefined reference to `QApplication::QApplication(int&, char**)'
解决方案:
1. 确保正确安装 Qt
2. 检查构建套件配置
3. 清理项目后重新构建
构建 -> 清理项目
构建 -> 重新构建项目
问题 3:中文乱码 #
cpp
// 解决方案 1:使用 QString::fromUtf8
label.setText(QString::fromUtf8("你好,Qt!"));
// 解决方案 2:使用 QStringLiteral
label.setText(QStringLiteral("你好,Qt!"));
// 解决方案 3:在 .pro 文件添加
// MSVC 编译器
msvc {
QMAKE_CXXFLAGS += /execution-charset:utf-8
}
下一步 #
现在你已经创建了第一个 Qt 程序,接下来学习 Qt 项目结构,深入了解 Qt 项目的组织方式!
最后更新:2026-03-29