# 添加 Qt Test 模块,用于支持单元测试功能QT += testlib
# 添加 Qt 的核心模块和 GUI 模块,这是构建 Qt 应用程序的基础模块QT += core gui
# 如果 Qt 的主版本号大于 4,则添加 widgets 模块。# 这是因为 Qt Widgets 模块是从 Qt 5 开始引入的,用于构建基于 QWidget 的传统桌面应用程序。greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
# 配置项目属性:# - qt:表示这是一个基于 Qt 的项目。# - console:启用控制台支持,允许程序在控制台中输出信息。# - warn_on:启用编译器的所有警告信息,有助于发现潜在问题。# - depend_includepath:启用依赖项的头文件路径。# - testcase:启用测试用例的配置,与 testlib 模块配合使用。CONFIG += qt console warn_on depend_includepath testcase
# 移除 app_bundle 配置选项。# app_bundle 主要用于 macOS,表示将应用程序打包为一个应用程序包(.app)。# 移除该选项可能是因为项目不需要以应用程序包的形式构建,或者是为了兼容其他平台。CONFIG -= app_bundle
# 定义项目模板类型为 "app",表示这是一个应用程序项目。# 其他模板类型包括 "lib"(库)、"subdirs"(子目录项目)等。TEMPLATE = app
# 列出项目中需要编译的源文件(.cpp):# - tst_qbtntest.cpp:可能是与测试相关的源文件。# - mainwindow.cpp:与主窗口相关的源文件。SOURCES += tst_qbtntest.cpp \ mainwindow.cpp
# 列出项目中需要包含的头文件(.h):# - mainwindow.h:与主窗口相关的头文件。HEADERS += \ mainwindow.h
# 列出项目中需要处理的 UI 文件(.ui):# - mainwindow.ui:使用 Qt Designer 设计的主窗口的 UI 文件。# Qt 的 uic 工具会将 .ui 文件转换为 C++ 代码,生成对应的头文件(如 ui_mainwindow.h)。FORMS += \ mainwindow.ui#ifndef MAINWINDOW_H#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACEnamespace Ui { class MainWindow; }QT_END_NAMESPACE
class MainWindow : public QMainWindow{ Q_OBJECT
public: MainWindow(QWidget *parent = nullptr); ~MainWindow();
private slots: void on_PushButton_clicked();
private: Ui::MainWindow *ui;};
#endif // MAINWINDOW_H#include "mainwindow.h"#include "./ui_mainwindow.h"#include <QDebug>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow){ ui->setupUi(this); connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::on_PushButton_clicked);}
MainWindow::~MainWindow(){ delete ui;}
void MainWindow::on_PushButton_clicked(){ qDebug() << "Button clicked!";}#include <QtTest>#include "mainwindow.h"#include <QPushButton>
// 单元测试类 qbtntestclass qbtntest : public QObject{ Q_OBJECT
public: qbtntest(); // 构造函数 ~qbtntest(); // 析构函数
private slots: void test_case1(); // 测试用例
private: MainWindow* mainWindow; // MainWindow 的实例};
// qbtntest 构造函数qbtntest::qbtntest(){ mainWindow = new MainWindow(); // 创建 MainWindow 实例}
// qbtntest 析构函数qbtntest::~qbtntest(){ delete mainWindow; // 释放 MainWindow 实例}
// 测试用例:测试按钮点击事件void qbtntest::test_case1(){ // 从 MainWindow 中查找名为 "pushButton" 的 QPushButton QPushButton* button = mainWindow->findChild<QPushButton*>("pushButton"); QVERIFY(button != nullptr); // 断言:确保按钮存在
// 使用 QSignalSpy 捕获按钮的 clicked 信号 QSignalSpy spy(button, &QPushButton::clicked);
// 模拟鼠标点击按钮 QTest::mouseClick(button, Qt::LeftButton);
// 验证信号是否被触发 QCOMPARE(spy.count(), 1); // 断言:信号被触发一次}
// 定义测试主函数QTEST_MAIN(qbtntest)
// 包含 MOC(元对象系统)生成的代码#include "tst_qbtntest.moc"<?xml version="1.0" encoding="UTF-8"?><ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>600</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <widget class="QPushButton" name="pushButton"> <property name="geometry"> <rect> <x>350</x> <y>170</y> <width>112</width> <height>34</height> </rect> </property> <property name="text"> <string>PushButton</string> </property> </widget> </widget> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>23</height> </rect> </property> </widget> <widget class="QStatusBar" name="statusbar"/> </widget> <resources/> <connections/></ui>全部工程文件放在一起1.5h
来源:CSDN 原文,首次发布于 2025-03-11。