1,QT的编译教程

目录

整体流程:

1,新建project文件

2,编写源代码

3,打开QT的命令行窗口

4,生成工程文件(QT_demo.pro)

[5,生成Make file](#5,生成Make file)

6,编译工程

7,运行编译好的可执行文件


整体流程:

1,新建project文件

新建文本文件,后缀改为.cpp

2,编写源代码

复制代码
#include <QApplication>
#include <QLabel>
#include <QLineEdit>
#include <QHBoxLayout>  // 水平布局
#include <QVBoxLayout>  // 垂直布局
#include <QWidget>
#include <QPushButton>  // 添加缺失的头文件

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QLabel *infoLabel = new QLabel;
    QLabel *openLabel = new QLabel;
    QLineEdit *cmdLineEdit = new QLineEdit;
    QPushButton *commitButton = new QPushButton;
    QPushButton *cancelButton = new QPushButton;  // 修正为 QPushButton
    QPushButton *browseButton = new QPushButton;

    infoLabel->setText("input cmd:");
    openLabel->setText("open");
    commitButton->setText("commit");
    cancelButton->setText("cancel");  // 现在可以正确调用 setText
    browseButton->setText("browse");

    QHBoxLayout *cmdLayout = new QHBoxLayout;  // 水平布局对象
    cmdLayout->addWidget(openLabel);  // 修正拼写错误(addwidget -> addWidget)
    cmdLayout->addWidget(cmdLineEdit);

    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addWidget(commitButton);
    buttonLayout->addWidget(cancelButton);
    buttonLayout->addWidget(browseButton);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(infoLabel);
    mainLayout->addLayout(cmdLayout);
    mainLayout->addLayout(buttonLayout);

    QWidget w;
    w.setLayout(mainLayout);
    w.show();

    return app.exec();
}

3,打开QT的命令行窗口

此时路径为安装QT程序的路径,需要进入到工程文件下

4,生成工程文件(QT_demo.pro)

QT_demo.pro 是 ​​Qt 的项目配置文件​​(类似于 CMake 的 CMakeLists.txt 或 Visual Studio 的 .vcxproj),它告诉 Qt 如何编译你的程序,包括:

  • 需要哪些 ​​Qt 模块​​(如 widgetsguicore)。
  • 哪些 ​​源文件​​ 要编译(如 .cpp 文件)。
  • 哪些 ​​头文件​​ 要包含(如 .h 文件)。
  • 如何生成 ​​可执行文件​​ 或 ​​库​​。

​​为什么需要 .pro 文件?​​

Qt 使用 qmake 工具来生成 Makefile,而 qmake 需要 .pro 文件作为输入。
​​如果没有 .pro 文件,qmake 不知道如何编译你的代码,导致 QApplication 等头文件找不到。​​

输入指令:qmake -project
注:如果失败,请添加环境变量

E:\QT_Creator\5.15.1\mingw81_64\bin

修改:

添加: QT += widgets gui
作用:用于在 .pro 文件中指定 Qt 模块。

QT += widgets gui 的作用​​

  1. ​​widgets​​

    • 提供 GUI 组件(如 QApplicationQPushButtonQLabel 等)。
    • ​​必须添加​​,否则会报错 QApplication: No such file or directory
  2. ​​gui​​

    • 提供图形界面基础功能(如 QPainterQPixmap)。
    • 通常会自动包含在 widgets 中,但显式声明更安全。
      完整的 .pro 文件示例​:
复制代码
# 指定 Qt 模块(widgets 和 gui)
QT += widgets gui

# 项目类型(app 表示生成可执行程序)
TEMPLATE = app

# 生成的可执行文件名
TARGET = QT_demo

# 源文件(你的 .cpp 文件)
SOURCES += main.cpp

# 头文件(如果有)
HEADERS += widget.h

# 资源文件(如图片、qrc 文件)
RESOURCES += resources.qrc

5,生成Make file

指令:qmake

6,编译工程

指令:mingw32-make

7,运行编译好的可执行文件

双击运行QT_demo.exe即可

如果需要修改代码,只需要在修改后重新执行:mingw32-make

例如修改窗口标题名字:

相关推荐
NineData2 小时前
NineData 迁移评估功能正式上线
数据库·dba
NineData7 小时前
数据库迁移总踩坑?用 NineData 迁移评估,提前识别所有兼容性风险
数据库·程序员·云计算
赵渝强老师9 小时前
【赵渝强老师】PostgreSQL中表的碎片
数据库·postgresql
blasit10 小时前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
全栈老石13 小时前
拆解低代码引擎核心:元数据驱动的"万能表"架构
数据库·低代码
倔强的石头_1 天前
kingbase备份与恢复实战(二)—— sys_dump库级逻辑备份与恢复(Windows详细步骤)
数据库
jiayou643 天前
KingbaseES 实战:深度解析数据库对象访问权限管理
数据库
李广坤3 天前
MySQL 大表字段变更实践(改名 + 改类型 + 改长度)
数据库
爱可生开源社区4 天前
2026 年,优秀的 DBA 需要具备哪些素质?
数据库·人工智能·dba
随逸1775 天前
《从零搭建NestJS项目》
数据库·typescript