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入门案例。

相关推荐
m0_5261194024 分钟前
iconfont我修改好颜色,但是在小程序项目是黑色的
小程序
桥田智能3 小时前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构
森G4 小时前
75、服务器源码解析---------云视频服务项目
linux·服务器·网络·c++·qt
2601_956743685 小时前
2026 上海小程序开发甄选:源码、云函数、跨端兼容技术评判
小程序·开发经验·上海
IT_张三5 小时前
CSDN-项目分享-暑期备考小程序
小程序
森G5 小时前
77、线程池原理和实现------服务器源码解析----云视频服务项目
服务器·c++·qt
小小龙学IT6 小时前
Apache Airflow 2.x 深度指南:用 Python 编排一切的现代化工作流引擎
开发语言·python·apache
森G6 小时前
71、打包发布---------打包发布
c++·qt
初圣魔门首席弟子6 小时前
Node.js 详细介绍(知识库版)
windows·qt·node.js·知识库
Shepherd06197 小时前
【IT 运维】Apache 使用 mod_remoteip 恢复 Cloudflare 后的真实访客 IP
运维·tcp/ip·apache