Qt的几种布局样例代码

创建水平布局
#include
QHBoxLayout* pHLay = new QHBoxLayout(父窗口指针); 一般填this
案例
#include "_2_hLayout.h"
#include
#include
#include
#include
#include
_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
#include
#include
#include
#include
_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* pGridLayout = new QGridLayout(this);
#include "_4_GridLayout.h"
#include 
#include 
#include 
#include 
#include 

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


你可能感兴趣的:(Qt常用知识,qt,命令模式,开发语言)