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()
{
}


  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值