欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

QT 的QLayout布局属性

程序员文章站 2024-02-21 12:37:40
...

QT布局控件


在QT的布局类中主要有如下的关系:
QT 的QLayout布局属性
QLayout 类主要作为布局类的基类,是抽象类,提供多种接口函数给继承类。
QLayout 提供了子页面,子Layout的添加的接口,同时提供了边界设置,菜单项设置等一系列接口

virtual void  addItem(QLayoutItem *item) = 0 
void  addWidget(QWidget*w)
void  setContentsMargins(intleft, int top, int right, int bottom)
void  setMenuBar(QWidget*widget)

QBoxLayout 主要分为水平布局类(QHBoxLayout)和垂直布局类(QVBoxLayout)
QBoxLayout作为QLayout的子类,提供了一些额外的信息:提供元素的拉伸比例,添加空元素等

void  addLayout(QLayout*layout, int stretch = 0)
void  addSpacerItem(QSpacerItem*spacerItem)
void  addSpacing(intsize)
bool setStretchFactor(QWidget*widget, int stretch)
bool setStretchFactor(QLayout*layout, int stretch)

QFormLayout 主要用于双列多行的布局
QFormLayout 作为Qlayout 的子类,提供了多种部件的添加,插入,获取部件位置的功能函数。

void addRow(const QString &labelText, QWidget *field)
void getItemPosition(int index, int *rowPtr, ItemRole *rolePtr) const
void getLayoutPosition(QLayout *layout, int *rowPtr, ItemRole *rolePtr) const
void insertRow(int row, QWidget *label, QWidget *field)
QLayoutItem *itemAt(int row, ItemRole role) const
Qt::Alignment labelAlignment() const
QWidget *labelForField(QWidget *field) const
void removeRow(int row)
int rowCount() const

QGridLayout 主要用于多列多行的布局
QGridLayout同样作为QLayout的子类,在父类的基础上,提供了多种子部件添加,获取位置,数量,设置最大最小高度宽度的功能函数。

void addItem(QLayoutItem *item, int row, int column, int rowSpan = 1, int columnSpan = 1, Qt::Alignment alignment = Qt::Alignment())
void addLayout(QLayout *layout, int row, int column, Qt::Alignment alignment = Qt::Alignment())
void addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment = Qt::Alignment())
int columnCount() const
int columnMinimumWidth(int column) const
int columnStretch(int column) const
void getItemPosition(int index, int *row, int *column, int *rowSpan, int *columnSpan) const
int rowCount() const
int rowMinimumHeight(int row) const
void setColumnMinimumWidth(int column, int minSize)
void setColumnStretch(int column, int stretch)
void setHorizontalSpacing(int spacing)
void setOriginCorner(Qt::Corner corner)
void setRowMinimumHeight(int row, int minSize)
void setRowStretch(int row, int stretch)

QStackedLayout 主要提供了一种页面切换的布局
QStackedLayout在继承父类的部分接头的同时,实现添加,插入部件,获取当前序号和设置部件的重叠显示的属性。

int addWidget(QWidget *widget)
int currentIndex() const
QWidget *currentWidget() const
int insertWidget(int index, QWidget *widget)
void setStackingMode(StackingMode stackingMode)//设置部件的重叠时的效果,是仅显示当前最上层部件还是都显示
StackingMode stackingMode() const

QFormLayout的使用示例:

#include "formlayouttest.h"
#include <QFormLayout>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QHBoxLayout>
FormlayoutTest::FormlayoutTest(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	QFormLayout *pLay = new QFormLayout(this);
	QLineEdit *pName = new QLineEdit();
	pLay->addRow("&Name", pName);


	QLineEdit *pEmail = new QLineEdit();
	pLay->addRow("&Email", pEmail);

	QLineEdit *pAge = new QLineEdit();
	pLay->insertRow(1, "&Age", pAge);

	QLabel *pInfo = new QLabel();
	pInfo->setText("Output Information");
	pInfo->setStyleSheet("color:Green;");
	pLay->insertRow(0, "", pInfo);
	
	//添加Save和Clear按钮
	QPushButton *pSave = new QPushButton("Save");
	QPushButton *pClear = new QPushButton("Clear");
	QObject::connect(pSave, SIGNAL(clicked()), this, SLOT(Save()));
	QObject::connect(pClear, SIGNAL(clicked()), this, SLOT(Clear()));
	//添加一个平行布局
	QHBoxLayout  *pHLay = new QHBoxLayout();
	pHLay->addWidget(pSave);
	pHLay->addWidget(pClear);
	pLay->addRow(pHLay);
}
void FormlayoutTest::Save()
{
	//找到Layout
	QFormLayout *formlay = (QFormLayout*)this->layout();
	//显示错误信息label
	QLabel *label = (QLabel*)formlay->itemAt(0, QFormLayout::FieldRole)->widget();
	//存放错误信息
	QString error = "";

	//遍历所有QLineEdit
	for (int i = 0; i < formlay->rowCount(); i++) {
		QLayoutItem *item = formlay->itemAt(i, QFormLayout::FieldRole);
		QLineEdit *e = (QLineEdit*)item->widget();
		if (!e)
			continue;

		//获取标题内容
		QLayoutItem *layItem = formlay->itemAt(i, QFormLayout::LabelRole);
		if(!layItem) continue;
		QLabel *titelLabel = (QLabel *)layItem->widget();
		if(!titelLabel) continue;
		QString title = titelLabel->text();

		QString strname = e->metaObject()->className();
		if (strname == "QLineEdit") {
			if (e->text().trimmed() == "")
			{
				e->setFocus();
				error += title;
				error += "  is empty!\n";
			}
		}
	}
	label->setText(error);
}
void FormlayoutTest::Clear()
{
	//在一个weidget 中只能有一个大QformLayout
	QFormLayout *formlay = (QFormLayout*)this->layout();
	for (int i = 0; i < formlay->rowCount(); i++) {
		QLayoutItem *item = formlay->itemAt(i, QFormLayout::FieldRole);
		QLineEdit *e = (QLineEdit*)item->widget();
		if(!e)
			continue;
		QString strname = e->metaObject()->className();
		if (strname == "QLineEdit") {
			e->setText("");
		}
	}
}

QT 的QLayout布局属性

QGridLayout的使用示例:

#include "uilayout.h"
#include <QtWidgets/QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QPushButton>
#include <QDebug>
#include <QGridLayout>
int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	QWidget *pWidget = new QWidget;
	QGridLayout *pGL = new QGridLayout(pWidget);
	QPushButton *pbtn11 = new QPushButton("btn11");
	pGL->addWidget(pbtn11, 0, 0);
	QPushButton *pbtn12 = new QPushButton("btn12");
	pGL->addWidget(pbtn12, 0, 1);
	QPushButton *pbtn13 = new QPushButton("btn13");
	pGL->addWidget(pbtn13, 0, 2);

	QPushButton *pbtn21 = new QPushButton("btn21");
	pGL->addWidget(pbtn21,1, 0);
	QPushButton *pbtn23 = new QPushButton("btn23");
	pGL->addWidget(pbtn23, 1, 2);

	//水平间距
	pGL->setHorizontalSpacing(10);
	//垂直间距
	pGL->setVerticalSpacing(20);
	pWidget->show();
	return a.exec();
}

QT 的QLayout布局属性

具体的其他功能函数,可以参见QT助手文档。

相关标签: QT相关 qt