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

相关推荐
流***陌40 分钟前
陪诊陪护小程序前端功能解析:就医照料的便捷对接与全流程保障
小程序
说私域1 小时前
开源AI智能名片链动2+1模式S2B2C商城小程序在个性化与小众化消费崛起中的营销宣传策略研究
人工智能·小程序
知识分享小能手1 小时前
微信小程序入门学习教程,从入门到精通,电影之家小程序项目知识点详解 (17)
前端·javascript·学习·微信小程序·小程序·前端框架·vue
应用市场1 小时前
Qt插件机制实现动态组件加载详解
开发语言·qt
星源~2 小时前
Linux-Ubuntu系统安装特别指导
linux·qt·ubuntu·嵌入式开发·物联网设备
泽虞3 小时前
《Qt应用开发》笔记p3
linux·开发语言·数据库·c++·笔记·qt·面试
ajassi20003 小时前
开源 C++ QT QML 开发(十八)多媒体--音频播放
c++·qt·开源
XXYBMOOO3 小时前
如何自定义 Qt 日志处理并记录日志到文件
开发语言·数据库·qt
知南x3 小时前
【QT界面设计学习篇】qt快速开发技巧
开发语言·qt
宏笋3 小时前
Qt 绘制彩色文本,包括字符颜色分割、动画效果和渐变等多种花式效果
qt