【十四】【QT开发应用】栅格布局,VS代码控制UI之栅格布局,图片资源qrc添加资源后保存,栅格布局添加控件

#include "demo6_GridLayout.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QCheckBox>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QDebug>

demo6_GridLayout::demo6_GridLayout(QWidget *parent)
    : QWidget(parent)
{
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint);

    //头像
    QLabel* pImageLable1 = new QLabel(this);
    QPixmap pixmap(":/demo6_GridLayout/resource/微信图片_20221014111549.jpg");
    pImageLable1->setObjectName("pImageLable1");
    //pImageLable1->setMaximumWidth(800);
    //pImageLable1->setMaximumHeight(600);
    pImageLable1->setFixedSize(150, 150);
    pImageLable1->setPixmap(pixmap);
    pImageLable1->setScaledContents(true);

    //用户名
    QLineEdit* pUserNameLineEdit = new QLineEdit(this);
    pUserNameLineEdit->setFixedSize(300, 50);
    pUserNameLineEdit->setPlaceholderText(QStringLiteral("QQ号码/手机/邮箱"));

    //密码

    QLineEdit* pPassWordLineEdit = new QLineEdit(this);
    pPassWordLineEdit->setFixedSize(300, 50);
    pPassWordLineEdit->setPlaceholderText(QStringLiteral("密码"));
    pPassWordLineEdit->setEchoMode(QLineEdit::Password);

    //找回密码
    QPushButton* pForgotBotton = new QPushButton(this);
    pForgotBotton->setText(QStringLiteral("找回密码"));
    pForgotBotton->setFixedWidth(80);

    //记住密码
    QCheckBox* pRemeberCheckBox = new QCheckBox(this);
    pRemeberCheckBox->setText(QStringLiteral("记住密码"));

    //自动登录
    QCheckBox* pAutoLoginCheckBox = new QCheckBox(this);
    pAutoLoginCheckBox->setText(QStringLiteral("自动登录"));

    //登录
    QPushButton* pLoginButton = new QPushButton(this);
    pLoginButton->setFixedHeight(48);
    pLoginButton->setText(QStringLiteral("登录"));

    //注册账号
    QPushButton* pRegisterButton = new QPushButton(this);
    pRegisterButton->setFixedHeight(48);
    pRegisterButton->setText(QStringLiteral("注册账号"));


    QGridLayout* pGridLay = new QGridLayout(this);
    pGridLay->addWidget(pImageLable1, 0, 0, 3, 1);
    pGridLay->addWidget(pUserNameLineEdit, 0, 1, 1, 2);
    pGridLay->addWidget(pPassWordLineEdit, 1, 1, 1, 2);
    pGridLay->addWidget(pForgotBotton, 2, 1, 1, 1);
    pGridLay->addWidget(pRemeberCheckBox, 2, 2, 1, 1,Qt::AlignLeft|Qt::AlignVCenter);
    pGridLay->addWidget(pAutoLoginCheckBox, 2, 2, 1, 1,Qt::AlignRight|Qt::AlignVCenter);
    pGridLay->addWidget(pLoginButton, 3, 1, 1, 2);
    pGridLay->addWidget(pRegisterButton, 4, 1, 1, 2);


}

demo6_GridLayout::~demo6_GridLayout()
{}

在这里插入图片描述

复盘

代码解释

#include "demo6_GridLayout.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QCheckBox>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QDebug>

// 构造函数,初始化父类 QWidget
demo6_GridLayout::demo6_GridLayout(QWidget *parent)
    : QWidget(parent)
{
    // 设置窗口标志,无边框和最小化、最大化按钮
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint);

    // 创建头像标签
    QLabel* pImageLable1 = new QLabel(this);
    // 加载头像图片
    QPixmap pixmap(":/demo6_GridLayout/resource/微信图片_20221014111549.jpg");
    pImageLable1->setObjectName("pImageLable1"); // 设置对象名称
    //pImageLable1->setMaximumWidth(800); // 设置最大宽度为 800(此行被注释掉)
    //pImageLable1->setMaximumHeight(600); // 设置最大高度为 600(此行被注释掉)
    pImageLable1->setFixedSize(150, 150); // 设置固定大小为 150x150
    pImageLable1->setPixmap(pixmap); // 设置标签的图片
    pImageLable1->setScaledContents(true); // 设置图片缩放以适应标签

    // 创建用户名输入框
    QLineEdit* pUserNameLineEdit = new QLineEdit(this);
    pUserNameLineEdit->setFixedSize(300, 50); // 设置固定大小为 300x50
    pUserNameLineEdit->setPlaceholderText(QStringLiteral("QQ号码/手机/邮箱")); // 设置占位符文本

    // 创建密码输入框
    QLineEdit* pPassWordLineEdit = new QLineEdit(this);
    pPassWordLineEdit->setFixedSize(300, 50); // 设置固定大小为 300x50
    pPassWordLineEdit->setPlaceholderText(QStringLiteral("密码")); // 设置占位符文本
    pPassWordLineEdit->setEchoMode(QLineEdit::Password); // 设置回显模式为密码模式

    // 创建找回密码按钮
    QPushButton* pForgotBotton = new QPushButton(this);
    pForgotBotton->setText(QStringLiteral("找回密码")); // 设置按钮文本
    pForgotBotton->setFixedWidth(80); // 设置固定宽度为 80

    // 创建记住密码复选框
    QCheckBox* pRemeberCheckBox = new QCheckBox(this);
    pRemeberCheckBox->setText(QStringLiteral("记住密码")); // 设置复选框文本

    // 创建自动登录复选框
    QCheckBox* pAutoLoginCheckBox = new QCheckBox(this);
    pAutoLoginCheckBox->setText(QStringLiteral("自动登录")); // 设置复选框文本

    // 创建登录按钮
    QPushButton* pLoginButton = new QPushButton(this);
    pLoginButton->setFixedHeight(48); // 设置固定高度为 48
    pLoginButton->setText(QStringLiteral("登录")); // 设置按钮文本

    // 创建注册账号按钮
    QPushButton* pRegisterButton = new QPushButton(this);
    pRegisterButton->setFixedHeight(48); // 设置固定高度为 48
    pRegisterButton->setText(QStringLiteral("注册账号")); // 设置按钮文本

    // 创建网格布局管理器
    QGridLayout* pGridLay = new QGridLayout(this);
    // 将头像标签添加到网格布局的第0行,第0列,占3行1列
    pGridLay->addWidget(pImageLable1, 0, 0, 3, 1);
    // 将用户名输入框添加到网格布局的第0行,第1列,占1行2列
    pGridLay->addWidget(pUserNameLineEdit, 0, 1, 1, 2);
    // 将密码输入框添加到网格布局的第1行,第1列,占1行2列
    pGridLay->addWidget(pPassWordLineEdit, 1, 1, 1, 2);
    // 将找回密码按钮添加到网格布局的第2行,第1列,占1行1列
    pGridLay->addWidget(pForgotBotton, 2, 1, 1, 1);
    // 将记住密码复选框添加到网格布局的第2行,第2列,占1行1列,左对齐,垂直居中
    pGridLay->addWidget(pRemeberCheckBox, 2, 2, 1, 1, Qt::AlignLeft | Qt::AlignVCenter);
    // 将自动登录复选框添加到网格布局的第2行,第2列,占1行1列,右对齐,垂直居中
    pGridLay->addWidget(pAutoLoginCheckBox, 2, 2, 1, 1, Qt::AlignRight | Qt::AlignVCenter);
    // 将登录按钮添加到网格布局的第3行,第1列,占1行2列
    pGridLay->addWidget(pLoginButton, 3, 1, 1, 2);
    // 将注册账号按钮添加到网格布局的第4行,第1列,占1行2列
    pGridLay->addWidget(pRegisterButton, 4, 1, 1, 2);
}

// 析构函数
demo6_GridLayout::~demo6_GridLayout()
{}

#include "demo6_GridLayout.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QCheckBox>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QDebug>

这些头文件包含了创建 UI 组件和布局管理器所需的类。

    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint);

this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint):设置窗口标志,使窗口无边框并包含最小化和最大化按钮。

QLabel* pImageLable1 = new QLabel(this);
QPixmap pixmap(":/demo6_GridLayout/resource/微信图片_20221014111549.jpg");
pImageLable1->setObjectName("pImageLable1");
pImageLable1->setFixedSize(150, 150);
pImageLable1->setPixmap(pixmap);
pImageLable1->setScaledContents(true);

QLabel* pImageLable1 = new QLabel(this):创建一个标签,父组件为 this。

QPixmap pixmap(“:/demo6_GridLayout/resource/微信图片_20221014111549.jpg”):加载图片资源。

pImageLable1->setObjectName(“pImageLable1”):设置标签的对象名称。

pImageLable1->setFixedSize(150, 150):设置标签的固定大小。

pImageLable1->setPixmap(pixmap):将图片设置为标签的内容。

pImageLable1->setScaledContents(true):使图片缩放以适应标签。

QLineEdit* pUserNameLineEdit = new QLineEdit(this);
pUserNameLineEdit->setFixedSize(300, 50);
pUserNameLineEdit->setPlaceholderText(QStringLiteral("QQ号码/手机/邮箱"));

QLineEdit* pUserNameLineEdit = new QLineEdit(this):创建一个文本输入框,父组件为 this。

pUserNameLineEdit->setFixedSize(300, 50):设置输入框的固定大小。

pUserNameLineEdit->setPlaceholderText(QStringLiteral(“QQ号码/手机/邮箱”)):设置占位符文本。

QLineEdit* pPassWordLineEdit = new QLineEdit(this);
pPassWordLineEdit->setFixedSize(300, 50);
pPassWordLineEdit->setPlaceholderText(QStringLiteral("密码"));
pPassWordLineEdit->setEchoMode(QLineEdit::Password);

pPassWordLineEdit->setEchoMode(QLineEdit::Password):设置回显模式为密码模式,使输入的文本显示为掩码字符。

QPushButton* pForgotBotton = new QPushButton(this);
pForgotBotton->setText(QStringLiteral("找回密码"));
pForgotBotton->setFixedWidth(80);

pForgotBotton->setFixedWidth(80):设置按钮的固定宽度。

QCheckBox* pRemeberCheckBox = new QCheckBox(this);
pRemeberCheckBox->setText(QStringLiteral("记住密码"));

QCheckBox* pAutoLoginCheckBox = new QCheckBox(this);
pAutoLoginCheckBox->setText(QStringLiteral("自动登录"));
QPushButton* pLoginButton = new QPushButton(this);
pLoginButton->setFixedHeight(48);
pLoginButton->setText(QStringLiteral("登录"));

QPushButton* pRegisterButton = new QPushButton(this);
pRegisterButton->setFixedHeight(48);
pRegisterButton->setText(QStringLiteral("注册账号"));

pLoginButton->setFixedHeight(48) 和 pRegisterButton->setFixedHeight(48):设置按钮的固定高度。

栅格布局添加控件

QGridLayout* pGridLay = new QGridLayout(this);
pGridLay->addWidget(pImageLable1, 0, 0, 3, 1);
pGridLay->addWidget(pUserNameLineEdit, 0, 1, 1, 2);
pGridLay->addWidget(pPassWordLineEdit, 1, 1, 1, 2);
pGridLay->addWidget(pForgotBotton, 2, 1, 1, 1);
pGridLay->addWidget(pRemeberCheckBox, 2, 2, 1, 1, Qt::AlignLeft | Qt::AlignVCenter);
pGridLay->addWidget(pAutoLoginCheckBox, 2, 2, 1, 1, Qt::AlignRight | Qt::AlignVCenter);
pGridLay->addWidget(pLoginButton, 3, 1, 1, 2);
pGridLay->addWidget(pRegisterButton, 4, 1, 1, 2);

QGridLayout* pGridLay = new QGridLayout(this):创建一个网格布局管理器,父组件为 this。

addWidget 方法用于将控件添加到布局中,参数分别为控件、行、列、占用的行数和列数。
在这里插入图片描述

对齐方式

在 Qt 框架中,控件的对齐方式可以通过 Qt::Alignment 标志进行控制。这些标志允许开发者精确地控制控件在布局中的位置和对齐方式。下面详细介绍 Qt::AlignLeft | Qt::AlignVCenter 和 Qt::AlignRight | Qt::AlignVCenter 的含义和用法。
Qt::AlignLeft | Qt::AlignVCenter

Qt::AlignLeft:这个标志用于将控件水平对齐到布局单元格的左侧。
Qt::AlignVCenter:这个标志用于将控件垂直居中对齐在布局单元格中。

组合使用 Qt::AlignLeft | Qt::AlignVCenter,可以使控件在布局单元格中左对齐并垂直居中。这种对齐方式非常适用于需要在容器左侧显示内容的场景,同时保持控件在垂直方向上居中。

添加资源

在这里插入图片描述

在这里插入图片描述

结尾

最后,感谢您阅读我的文章,希望这些内容能够对您有所启发和帮助。如果您有任何问题或想要分享您的观点,请随时在评论区留言。
同时,不要忘记订阅我的博客以获取更多有趣的内容。在未来的文章中,我将继续探讨这个话题的不同方面,为您呈现更多深度和见解。
谢谢您的支持,期待与您在下一篇文章中再次相遇!

  • 32
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

妖精七七_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值