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

相关推荐
忙碌的菠萝4 分钟前
使用Apache Lucene构建高效的全文搜索服务
apache·mybatis·lucene
_GR1 小时前
Qt开发⑧Qt的窗口_下_浮动窗口+对话框
开发语言·css·c++·qt·microsoft
萧寂1732 小时前
微信小程序自定义轮播图指示点样式
微信小程序·小程序
人类群星闪耀时4 小时前
Apache Flink:实时数据流处理的终极武器
struts·flink·apache
R_yy11 小时前
微信小程序开发——视频播放实现(本地视频或者云端视频均可)
前端·微信小程序·小程序
御风@户外13 小时前
qt5的中文乱码问题,QString、QStringLiteral 为 UTF-16 编码
c++·qt·乱码
东方忘忧13 小时前
QT MD5校验文件和数据的完整性
开发语言·qt
Wyn_13 小时前
【QT】QLinearGradient 线性渐变类简单使用教程
qt
暴躁茹13 小时前
正则表达式用法及其示例:匹配、查找、替换文本中的模式,及QT下如何使用正则表达式。
qt·mysql·正则表达式