创建水平布局
#include
QHBoxLayout* pHLay = new QHBoxLayout(父窗口指针); 一般填this
案例
#include "_2_hLayout.h"
#include
#include
#include
#include
#include
_2_hLayout::_2_hLayout(QWidget* parent)
: QMainWindow(parent)
{
QLabel* pPath = new QLabel(this);
pPath->setObjectName("pPath");
pPath->setText(u8"路径");
QLineEdit* pEdit = new QLineEdit(this);
pEdit->setObjectName("pEdit");
pEdit->setMinimumWidth(50);
QPushButton* pBtn = new QPushButton(this);
pBtn->setObjectName("pBtn");
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->setText(u8"路径");
QLineEdit* pEdit = new QLineEdit(this);
pEdit->setObjectName("pEdit");
pEdit->setMinimumWidth(50);
QPushButton* pBtn = new QPushButton(this);
pBtn->setObjectName("pBtn");
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);
pHSplitter->addWidget(pLeftWidget);
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()
{
}