QT布局管理详解QVBoxLayout与QHBoxLayout及QGridLayout的使用

 更新时间:2022年06月23日 09:48:02   作者:红客白帽  
在这篇文章中,你将知道水平布局、垂直布局、网格布局如何轻松上手,以纯代码方式展示。对齐方式,大小设置,图片头像匹配标签,布局器里面的组件大小随意切换大小,认真看完这篇文章,QT布局管理器熟练使用

main.cpp

#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    return a.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include<QPushButton>
#include <QMainWindow>
#include <QTextCodec>//解决字符编码乱码问题
#include<QTextEdit>
#include <QSlider>//滑动杆
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
public slots:
private:
    Ui::MainWindow *ui;
    QTextCodec *codec;
    QWidget *window;
    QPushButton *button1;
    QPushButton *button2;
    QPushButton *button3;
    QPushButton *button4;
    QPushButton *button5;
    QWidget *win;//方便全局使用
    QWidget *newweigdet;
};
#endif // MAINWINDOW_H

mainwindow.cpp

我就不一一举例了,具体在代码中渗透

//=============================布局管理器全精通
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QString>
#include<QStringLiteral>
#include<QDebug>//控制台输出
//==========================布局管理器
#include<QVBoxLayout>//水平
#include<QHBoxLayout>//垂直
#include<QHBoxLayout>//网格
#include<QRadioButton>
#include <QLineEdit>
#include <QCheckBox>
#include<QLabel>
#include<QPixmap>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    codec = QTextCodec::codecForName("gbk");//设置字符编码
    codec->setCodecForLocale(codec);
    setWindowTitle(codec->toUnicode("UI学习笔记"));
    win = new QWidget;//创建一个窗口部件
    newweigdet = new QWidget;
    newweigdet->hide();
    QHBoxLayout *hlay = new QHBoxLayout();//创建水平布局
    QPushButton *bt1 = new QPushButton("bt1");
    bt1->setFixedSize(100,40);//设置宽高,位置不变
    QPushButton *bt2 = new QPushButton("bt2");
    QPushButton *bt3 = new QPushButton("bt3");
    QPushButton *bt4 = new QPushButton("bt4");
    bt2->setFixedSize(100,40);//设置宽高,位置不变
    bt3->setFixedSize(100,40);//设置宽高,位置不变
//    qDebug()<<bt1->x()<<" "<<bt1->y()<<" "<<bt1->width()<<" "<<bt1->height();
//    bt1->setGeometry(0,0,800,640);
    hlay->addWidget(bt1);
    hlay->addWidget(bt2);
    hlay->addWidget(bt3);
    hlay->addWidget(bt4);
    hlay->setSpacing(30);//设置组件之间的距离
    //hlay->setContentsMargins(10,10,10,10);//设置内部控件与边框的距离
    //新建垂直布局
    QVBoxLayout *vlay = new QVBoxLayout();
    //创建3个QRadioButton
    QRadioButton *rb1 = new QRadioButton("QRadioButton 1");
    QRadioButton *rb2 = new QRadioButton("QRadioButton 2");
    QRadioButton *rb3 = new QRadioButton("QRadioButton 3");
    //将水平布局放入垂直布局
    vlay->addItem(hlay);
    vlay->addWidget(rb1);
    vlay->addWidget(rb2);
    vlay->addWidget(rb3);
    vlay->setSpacing(30);//设置组件之间的距离
    vlay->setContentsMargins(30,10,10,10);//设置内部控件与边框的距离
    /*控件大小范围限定
        通过上面的代码我们发现一个现象:程序中并没有去设置子控件的大小,
        其默认大小是Qt自动设置的,同时在窗口大小改变时,控件大小也会随之调整。
        然而有时候我们并不想要这样的效果,我们只想让控件大小保持在某一范围内,
        这时就需要用到下面几个API进行设置了。*/
    //设置按钮bt4最小宽度和最大宽度
    bt4->setMinimumWidth(60);
    bt4->setMaximumWidth(70);
    bt4->setFixedSize(50,50);//设置这个部件的宽和高【重要】
    QHBoxLayout *hlay2 = new QHBoxLayout();
    QPushButton *btOK = new QPushButton("OK");
    QPushButton *btCancel= new QPushButton("Cancel");
    hlay2->addWidget(btOK);
    //增加可伸缩空间
    hlay2->addStretch();//拉扯不影响大小【重点】
    hlay2->addWidget(btCancel);
    vlay->addItem(hlay2);
    QHBoxLayout *hlay3 = new QHBoxLayout();
    QPushButton *bt61 = new QPushButton("bt61");
    QPushButton *bt62= new QPushButton("bt62");
    QPushButton *bt63= new QPushButton("bt63");
//    bt61->setFixedSize(100,40);//设置宽高,位置不变
//    bt62->setFixedSize(100,40);//设置宽高,位置不变
//    bt63->setFixedSize(100,40);//设置宽高,位置不变
    hlay3->addWidget(bt61);
    hlay3->addWidget(bt62);
    hlay3->addWidget(bt63);
    //Qt中可以设定控件的拉伸系数,也可以理解为控件的缩放比例。
    hlay3->setStretchFactor(bt61,1);
    hlay3->setStretchFactor(bt62,2);
    hlay3->setStretchFactor(bt63,3);
    vlay->addItem(hlay3);
    win->setLayout(vlay);//水平和垂直组件放在这个QWidget
    win->show();//显示
    bt4->setFixedSize(100,50);
    bt4->setText(codec->toUnicode("下一页内容"));
    connect(bt4,&QPushButton::clicked,[&](){
       on_pushButton_clicked();
    });//设置信号与槽  上一章已经出过文章了
}
MainWindow::~MainWindow()
{
    delete ui;
}
//网格布局
void MainWindow::on_pushButton_clicked()
{
    win->hide();//隐藏上一个窗口
    // 构建控件 头像、用户名、密码输入框等
    QLabel *pImageLabel = new QLabel;
    QLineEdit *pUserLineEdit = new QLineEdit;
    QLineEdit *pPasswordLineEdit = new QLineEdit;
    QCheckBox *pRememberCheckBox = new QCheckBox;
    QCheckBox *pAutoLoginCheckBox = new QCheckBox;
    QPushButton *pLoginButton = new QPushButton;
    QPushButton *pRegisterButton = new QPushButton;
    QPushButton *pForgotButton = new QPushButton;
    QPushButton *page_up = new QPushButton;
    pLoginButton->setFixedHeight(30);//设置宽
    pUserLineEdit->setFixedWidth(200);//设置宽
    // 设置头像
    QPixmap pixmap("C:/Users/SuJieYin/Pictures/Saved Pictures/2.png");
    pImageLabel->setFixedSize(90, 90);
    pImageLabel->setPixmap(pixmap);
    pImageLabel->setScaledContents(true);//图片适应标签
    // 设置文本
    pUserLineEdit->setPlaceholderText(codec->toUnicode("QQ号码/手机/邮箱"));//显示中文
    pPasswordLineEdit->setPlaceholderText(codec->toUnicode("密码"));
    pPasswordLineEdit->setEchoMode(QLineEdit::Password);//隐藏
    pRememberCheckBox->setText(codec->toUnicode("记住密码"));
    pAutoLoginCheckBox->setText(codec->toUnicode("自动登录"));
    pLoginButton->setText(codec->toUnicode("登录"));
    pRegisterButton->setText(codec->toUnicode("注册账号"));
    pForgotButton->setText(codec->toUnicode("找回密码"));
    page_up->setText(codec->toUnicode("上一页"));
    QGridLayout *pLayout = new QGridLayout();
    // 头像 第0行,第0列开始,占3行1列
    pLayout->addWidget(pImageLabel, 0, 0, 3, 1);
    // 用户名输入框 第0行,第1列开始,占1行2列
    pLayout->addWidget(pUserLineEdit, 0, 1, 1, 2);
    pLayout->addWidget(pRegisterButton, 0, 4);
    // 密码输入框 第1行,第1列开始,占1行2列
    pLayout->addWidget(pPasswordLineEdit, 1, 1, 1, 2);
    pLayout->addWidget(pForgotButton, 1, 4);
    // 记住密码 第2行,第1列开始,占1行1列 水平居左 垂直居中
    pLayout->addWidget(pRememberCheckBox, 2, 1, 1, 1, Qt::AlignLeft | Qt::AlignVCenter);
    // 自动登录 第2行,第2列开始,占1行1列 水平居右 垂直居中
    pLayout->addWidget(pAutoLoginCheckBox, 2, 2, 1, 1, Qt::AlignRight | Qt::AlignVCenter);
    // 登录按钮 第3行,第1列开始,占1行2列
    pLayout->addWidget(pLoginButton, 3, 1, 1, 2);
    pLayout->addWidget(page_up,3,4);//设置上一页在第三行第四列
    connect(page_up,&QPushButton::clicked,[&](){
       win->show();
    });
    // 设置水平间距
    pLayout->setHorizontalSpacing(10);
    // 设置垂直间距
    pLayout->setVerticalSpacing(10);
    // 设置外间距
    pLayout->setContentsMargins(10, 10, 10, 10);//边框间距设置
    newweigdet->setLayout(pLayout);//添加进窗口部件
    newweigdet->show();//显示窗口部件
}

ui界面设计

由于是通过纯代码实现,所以ui界面是空的,不信你看:

实际具体细看代码如何实现,注释的已经很清晰了。

登录界面为例

以下如图:第一页

实际运行CTRL+R,点击下一页如图:

总结

学习QT并不是一朝一夕,庞大的函数库,需要巨大的精力投入学习,掌握基础方法后,通过QT手册边学边做,而布局管理器几乎所有项目都要用到,美观的外表非常的重要。

到此这篇关于QT布局管理详解QVBoxLayout与QHBoxLayout及QGridLayout的使用的文章就介绍到这了,更多相关QT布局管理内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Qt实现拖动单个控件移动的示例代码

    Qt实现拖动单个控件移动的示例代码

    做惯了静态图,今天来搞一搞动态图吧!本文将利用Qt实现拖动单个控件移动效果,文中的示例代码讲解详细,感兴趣的可以动手尝试一下
    2022-06-06
  • 使用OpenCV实现检测和追踪车辆

    使用OpenCV实现检测和追踪车辆

    这篇文章主要为大家详细介绍了使用OpenCV实现检测和追踪车辆,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • C++超详细讲解内存空间分配与this指针

    C++超详细讲解内存空间分配与this指针

    this 指针在C++类和对象中是个很方便实用的关键字,可以简化对象成员属性的调用,使代码表达的含义更加准确;在之前的学习中我们都可以判断变量所占内存空间大小,那么我们创建的类对象所占的内存空间怎么计算呢?想知道this的妙用和类对象占用的内存空间就来跟我学习吧
    2022-05-05
  • C语言中do-while语句的2种写法示例

    C语言中do-while语句的2种写法示例

    这篇文章主要给大家介绍了关于C语言中do-while语句的2种写法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • C/C++中使用局部/全局变量初始值或默认值问题

    C/C++中使用局部/全局变量初始值或默认值问题

    这篇文章主要介绍了C/C++中使用局部/全局变量初始值或默认值问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • C++如何删除map容器中指定值的元素详解

    C++如何删除map容器中指定值的元素详解

    map容器是C++ STL中的重要一员,删除map容器中value为指定元素的问题是我们经常与遇到的一个问题,下面这篇文章主要给大家介绍了关于利用C++如何删除map容器中指定值的元素的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-06-06
  • 软件构建工具makefile基础讲解

    软件构建工具makefile基础讲解

    这篇文章介绍了软件构建工具makefile,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-12-12
  • C语言实现大数值金额大写转换的方法详解

    C语言实现大数值金额大写转换的方法详解

    这篇文章主要为大家详细介绍了如何利用C语言实现大数值金额大写转换的功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起了解一下
    2023-03-03
  • 基于C++泛型编程职工管理系统

    基于C++泛型编程职工管理系统

    这篇文章主要介绍了基于C++泛型编程职工管理系统,前面介绍到了C++的泛型编程,并实现了万能容器,不过那使用的是数组,今天呢咱带大家实践一下使用泛型技术,结合单链表实现一个职工管理系统,需要的朋友可以参考一下
    2022-02-02
  • C语言动态数组的使用实现代码

    C语言动态数组的使用实现代码

    这篇文章主要介绍了C语言动态数组的使用实现代码的相关资料,需要的朋友可以参考下
    2017-01-01

最新评论