Qt入门教程:创建我的第一个小程序

本章教程,主要介绍如何编写一个简单的QT小程序。主要是介绍创建项目的过程。

一、打开QT软件编辑器

这里使用的是QT5.14.2版本的,安装教程参考以往教程:https://blog.csdn.net/qq_19309473/article/details/142907096


二、创建项目









到这里,一个简单的QT项目就已经创建完成了。

三、编写代码

这里写了一个简单的登录界面。找到main.cpp源文件。

cpp 复制代码
#include <QApplication>
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>

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

    QWidget window;
    window.setWindowTitle("登录");

    // 创建标签
    QLabel *userLabel = new QLabel("账号:");
    QLabel *passLabel = new QLabel("密码:");

    // 创建文本框
    QLineEdit *userLineEdit = new QLineEdit;
    QLineEdit *passLineEdit = new QLineEdit;
    passLineEdit->setEchoMode(QLineEdit::Password);

    // 创建按钮
    QPushButton *loginButton = new QPushButton("登录");

    // 布局
    QVBoxLayout *mainLayout = new QVBoxLayout;
    QHBoxLayout *userLayout = new QHBoxLayout;
    QHBoxLayout *passLayout = new QHBoxLayout;

    userLayout->addWidget(userLabel);
    userLayout->addWidget(userLineEdit);

    passLayout->addWidget(passLabel);
    passLayout->addWidget(passLineEdit);

    mainLayout->addLayout(userLayout);
    mainLayout->addLayout(passLayout);
    mainLayout->addWidget(loginButton);

    window.setLayout(mainLayout);
    window.show();

    return app.exec();
}

四、运行项目

编写好代码之后,点击左下角的绿色三角形,即可运行项目。

这样就完成了,一个简单QT入门案例。

相关推荐
看到我,请让我去学习29 分钟前
Qt— 布局综合项目(Splitter,Stacked,Dock)
开发语言·qt
创想未来CTF30 分钟前
Qt同步处理业务并禁用按钮
qt
喂完待续3 小时前
【Tech Arch】Hive技术解析:大数据仓库的SQL桥梁
大数据·数据仓库·hive·hadoop·sql·apache
SelectDB3 小时前
5000+ 中大型企业首选的 Doris,在稳定性的提升上究竟花了多大的功夫?
大数据·数据库·apache
weixin_lynhgworld4 小时前
剧本杀小程序系统开发:构建剧本杀社交新生态
小程序
说私域4 小时前
基于定制开发开源 AI 智能名片 S2B2C 商城小程序的热点与人工下发策略研究
人工智能·小程序
weixin_lynhgworld4 小时前
陪诊小程序系统开发:让就医不再是一件难事
小程序
谱写秋天6 小时前
Qt 5.5 的安装与配置(使用 VSCode编辑)
开发语言·vscode·qt
喂完待续16 小时前
Apache Hudi:数据湖的实时革命
大数据·数据仓库·分布式·架构·apache·数据库架构
前端市界21 小时前
前端视角: PyQt6+Vue3 跨界开发实战
前端·qt·pyqt