Qt的几种布局样例代码

创建水平布局
#include<QHBoxLayout>
QHBoxLayout* pHLay = new QHBoxLayout(父窗口指针); 一般填this
案例
#include "_2_hLayout.h"
#include<QHBoxLayout>
#include<QLabel>
#include<QLineEdit>
#include<QPushButton>
#include<qdebug.h>
_2_hLayout::_2_hLayout(QWidget* parent)
	: QMainWindow(parent)
{
   
	//QHBoxLayout* pHLay = new QHBoxLayout(this);
	QLabel* pPath = new QLabel(this);

	pPath->setObjectName("pPath");
	//pPath->setFixedSize(40, 32);
	pPath->setText(u8"路径");

	QLineEdit* pEdit = new QLineEdit(this);
	pEdit->setObjectName("pEdit");
	//pEdit->setFixedSize(100, 32);
	pEdit->setMinimumWidth(50);

	QPushButton* pBtn = new QPushButton(this);
	pBtn->setObjectName("pBtn");
	//pBtn->setFixedSize(50, 32);
	pBtn->setText(u8"打开");

	QHBoxLayout* pHLay = new QHBoxLayout(this);
	pHLay->addStretch();

	pHLay->addWidget(pPath);
	pHLay->setSpacing(20);
	qDebug() << pHLay->spacing();
	pHLay->addWidget(pEdit);
	pHLay->addWidget(pBtn);
	pHLay->setContentsMargins(0, 100, 10, 0);
	pHLay->addStretch();
	// 将水平布局设置为窗口的布局管理器
	QWidget* centralWidget = new QWidget(this);
	centralWidget->setLayout(pHLay);
	setCentralWidget(centralWidget);
}

_2_hLayout::~_2_hLayout()
{
   }

竖直布局
#include "_3_vlayout.h"
#include<QVBoxLayout>
#include<QLabel>
#include<QLineEdit>
#include<QPushButton>
#include<qdebug.h>
_3_vlayout::_3_vlayout(QWidget* parent)
	: QWidget(parent)
	, ui(new Ui::_3_vlayoutClass())
{
   
	ui->setupUi(this);

	QLabel* pPath = new QLabel(this);

	pPath->setObjectName("pPath");
	//pPath->setFixedSize(40, 32);
	pPath->setText(u8"路径");

	QLineEdit* pEdit = new QLineEdit(this);
	pEdit->setObjectName("pEdit");
	//pEdit->setFixedSize(100, 32);
	pEdit->setMinimumWidth(50);

	QPushButton* pBtn = new QPushButton(this);
	pBtn->setObjectName("pBtn");
	//pBtn->setFixedSize(50, 32);
	pBtn->setText(u8"打开");

	QVBoxLayout* pVLay = new QVBoxLayout(this);

	pVLay->addWidget(pPath);
	pVLay->setSpacing(10);
	pVLay->addWidget(pEdit);
	pVLay->setSpacing(50);
	pVLay->addWidget(pBtn);
	pVLay->setContentsMargins(80, 70, 60, 50);

}

_3_vlayout::~_3_vlayout()
{
   
	delete ui;
}

栅格布局
#include<QGridLayout>
QGridLayout* pGridLayout = new QGridLayout(this);
#include "_4_GridLayout.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QCheckBox>
#include <QGridLayout>

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

	//头像
	QLabel* pImageLabel = new QLabel(this);
	QPixmap pixmap(":/_4_GridLayout/resources/user_image.png");
	pImageLabel->setFixedSize(150, 150);
	pImageLabel->setPixmap(pixmap);
	pImageLabel->setScaledContents(true);

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

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

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

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

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

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

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

	QGridLayout* pGridLay = new QGridLayout(this);

	pGridLay->addWidget(pImageLabel, 0, 0, 3, 1);
	pGridLay->addWidget(pUserNameLineEdit, 0, 1, 1, 2);
	pGridLay->addWidget(pPasswordLineEdit, 1, 1, 1, 2);
	pGridLay->addWidget(pForgotButton, 2, 1, 1, 1);
	pGridLay->addWidget(pRememberCheckBox, 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);

	pGridLay->setHorizontalSpacing(20);
	pGridLay->setVerticalSpacing(20);

	pGridLay->setContentsMargins(30, 30, 30, 30);
}

_4_GridLayout::~_4_GridLayout()
{
   }
spilit 布局
#include "widget.h"
#include "widget.h"
#include<QHBoxLayout>
#include<QSplitter>
#include<QTextBrowser>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
   
    this->setWindowTitle("Qt分裂器布局_c++代码");

    //整体用水平布局
    QHBoxLayout* pHLay = new QHBoxLayout(this);

    //整体的水平分裂器
    QSplitter* pHSplitter = new QSplitter(Qt::Horizontal, this);
    QWidget* pLeftWidget = new QWidget(this);
    pLeftWidget->setStyleSheet("background-color:rgb(54,54,54)");
    pLeftWidget->setMinimumWidth(200);

    //分裂器添加widget
    pHSplitter->addWidget(pLeftWidget);

    //右侧的竖直分裂器
    //注意参数pHSplitter,表示父指针
    QSplitter* pVSplitter = new QSplitter(Qt::Vertical, pHSplitter);

    //在拖动到位并弹起鼠标后再显示分隔条
    pVSplitter->setOpaqueResize(false);

    QWidget* pRightTopWidget = new QWidget(this);
    pRightTopWidget->setStyleSheet("background-color:rgb(154, 154, 154)");

    QTextBrowser* pRightBottom = new QTextBrowser(this);

    pVSplitter->addWidget(pRightTopWidget);
    pVSplitter->addWidget(pRightBottom);

    pHSplitter->addWidget(pVSplitter);

    //布局添加分裂器
    pHLay->addWidget(pHSplitter);

    //设置整体布局
    setLayout(pHLay);
}

Widget::~Widget()
{
   
}


相关推荐

  1. Qt布局代码

    2024-02-10 14:18:01       30 阅读
  2. QtQtTimer

    2024-02-10 14:18:01       7 阅读
  3. 模式实现方式

    2024-02-10 14:18:01       26 阅读
  4. 模式实现方式

    2024-02-10 14:18:01       19 阅读
  5. 模式实现方式

    2024-02-10 14:18:01       11 阅读
  6. Android单实现方式

    2024-02-10 14:18:01       6 阅读
  7. QT实现modbus库,记录

    2024-02-10 14:18:01       39 阅读
  8. qt中sokect断开情况

    2024-02-10 14:18:01       37 阅读
  9. Qt中鼠标点击状态

    2024-02-10 14:18:01       41 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-10 14:18:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-10 14:18:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-10 14:18:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-10 14:18:01       18 阅读

热门阅读

  1. GCC编译器技巧--在C语言里面使用使用内敛汇编

    2024-02-10 14:18:01       23 阅读
  2. RuoYi模块功能分析:第二章 日志

    2024-02-10 14:18:01       26 阅读
  3. C#系列-C#访问hadoop API(9)

    2024-02-10 14:18:01       33 阅读
  4. 11.1 OpenGL可编程顶点处理:顶点着色器

    2024-02-10 14:18:01       38 阅读
  5. 基础算法-高精度加法 基础算法-高精度加法

    2024-02-10 14:18:01       26 阅读
  6. ubuntu如何离线安装nginx?

    2024-02-10 14:18:01       35 阅读
  7. SIMD学习笔记1

    2024-02-10 14:18:01       26 阅读
  8. Python编程:17个提升工作效率的自动化脚本

    2024-02-10 14:18:01       28 阅读
  9. MATLAB实现随机森林回归算法

    2024-02-10 14:18:01       35 阅读