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

相关推荐
前端极客探险家23 分钟前
微信小程序全解析:从入门到实战
微信小程序·小程序
h_654321034 分钟前
微信小程序van-dialog确认验证失败时阻止对话框的关闭
微信小程序·小程序
-曾牛1 小时前
基于微信小程序的在线聊天功能实现:WebSocket通信实战
前端·后端·websocket·网络协议·微信小程序·小程序·notepad++
说私域2 小时前
基于开源AI智能名片链动2+1模式S2B2C商城小程序的“互相拆台”式宣传策略研究
人工智能·小程序·开源·零售
charlie1145141913 小时前
基于Qt6 + MuPDF在 Arm IMX6ULL运行的PDF浏览器——MuPDF Adapter文档
arm开发·qt·学习·pdf·教程·设计·qt6
电信2301杨臣4 小时前
QT---信号与槽
开发语言·qt
少恭写代码4 小时前
在Taro中开发一个跨端Svg组件,同时支持小程序、H5、React Native
react native·小程序·taro
范纹杉想快点毕业6 小时前
以项目的方式学QT开发(一)——超详细讲解(120000多字详细讲解,涵盖qt大量知识)逐步更新!
c语言·数据结构·c++·git·qt·链表·github
钢铁男儿7 小时前
PyQt 探索QMainWindow:打造专业的PyQt5主窗
python·qt·pyqt