Qt学习之QSplitter分割窗口

在Qt开发中,QSplitter是比较常用到的,也就是分割窗口。

并且窗口分割处也就是QSplitter的handle是可以随意拖动的。

这样,每个窗口的大小就可以按照自己的需要进行调整。

QSplitter可以容纳其他Widget,具有布局一样的特性,但是又可以动态调整,

可以说是动态布局特性,其内部的Widget可以自由伸缩。

QSplitter有两种:垂直和水平。

 enum Orientation {
        Horizontal = 0x1,
        Vertical = 0x2
    };
先实现水平窗口分割,内部QTextEdit可以随分隔条移动而自由伸缩:

#include <QtWidgets/QApplication>
#include <QHBoxLayout>
#include <QSplitter>
#include <QTextEdit>

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	QWidget widget;
	QHBoxLayout *pHBox = new QHBoxLayout(&widget);
	QSplitter *pSplitter = new QSplitter(Qt::Orientation::Horizontal, &widget);//水平
	pSplitter->setStyleSheet("QSplitter:handle{background-color:grey}");

	QTextEdit *pEdit1 = new QTextEdit(&widget);
	QTextEdit *pEdit2 = new QTextEdit(&widget);

	pEdit1->setText("Left");
	pEdit2->setText("Right");

	pSplitter->addWidget(pEdit1);
	pSplitter->addWidget(pEdit2);

	pHBox->addWidget(pSplitter);
	widget.show();
	return a.exec();
}
效果如下:

Qt学习之QSplitter分割窗口_第1张图片Qt学习之QSplitter分割窗口_第2张图片

然后实现垂直窗口分割:

#include <QtWidgets/QApplication>
#include <QHBoxLayout>
#include <QSplitter>
#include <QTextEdit>

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	QWidget widget;
	QHBoxLayout *pHBox = new QHBoxLayout(&widget);
	QSplitter *pSplitter = new QSplitter(Qt::Orientation::Vertical, &widget);//水平
	pSplitter->setStyleSheet("QSplitter:handle{background-color:grey}");

	QTextEdit *pEdit1 = new QTextEdit(&widget);
	QTextEdit *pEdit2 = new QTextEdit(&widget);

	pEdit1->setText("Top");
	pEdit2->setText("Bottom");

	pSplitter->addWidget(pEdit1);
	pSplitter->addWidget(pEdit2);

	pHBox->addWidget(pSplitter);
	widget.show();
	return a.exec();
}

Qt学习之QSplitter分割窗口_第3张图片

先水平分割,再垂直分割:

#include <QHBoxLayout>
#include <QSplitter>
#include <QTextEdit>

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	QWidget widget;
	/***3.先水平,后垂直****/
	QHBoxLayout *pHBox = new QHBoxLayout(&widget);
	QSplitter *pSplitter = new QSplitter(Qt::Orientation::Horizontal, &widget);//水平
	pSplitter->setStyleSheet("QSplitter:handle{background-color:grey}");
	QTextEdit *pEdit1 = new QTextEdit(&widget);
	
	QSplitter *pSplitterV = new QSplitter(Qt::Orientation::Vertical, &widget);//垂直
	QTextEdit *pEdit2 = new QTextEdit(&widget);
	QTextEdit *pEdit3 = new QTextEdit(&widget);

	pEdit1->setText("Left");
	pEdit2->setText("RightTop");
	pEdit3->setText("RightBottom");

	pSplitterV->addWidget(pEdit2);
	pSplitterV->addWidget(pEdit3);

	pSplitter->addWidget(pEdit1);
	pSplitter->addWidget(pSplitterV);

	pHBox->addWidget(pSplitter);
	widget.show();
	return a.exec();
}
Qt学习之QSplitter分割窗口_第4张图片
先垂直分割,再水平分割:

#include <QtWidgets/QApplication>
#include <QHBoxLayout>
#include <QSplitter>
#include <QTextEdit>

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	QWidget widget;
	/***4.先垂直,后水平****/
	QHBoxLayout *pHBox = new QHBoxLayout(&widget);
	QSplitter *pSplitter = new QSplitter(Qt::Orientation::Vertical, &widget);//水平
	pSplitter->setStyleSheet("QSplitter:handle{background-color:grey}");
	QTextEdit *pEdit1 = new QTextEdit(&widget);

	QSplitter *pSplitterV = new QSplitter(Qt::Orientation::Horizontal, &widget);//垂直
	QTextEdit *pEdit2 = new QTextEdit(&widget);
	QTextEdit *pEdit3 = new QTextEdit(&widget);

	pEdit1->setText("Top");
	pEdit2->setText("BottomLeft");
	pEdit3->setText("BottomRight");

	pSplitterV->addWidget(pEdit2);
	pSplitterV->addWidget(pEdit3);

	pSplitter->addWidget(pEdit1);
	pSplitter->addWidget(pSplitterV);

	pHBox->addWidget(pSplitter);
	widget.show();
	widget.show();
	return a.exec();
}

Qt学习之QSplitter分割窗口_第5张图片


void setHandleWidth(int)可以用来设置把手的宽度。


本文地址:http://blog.csdn.net/u011417605/article/details/50914738

源码下载:http://download.csdn.net/detail/u011417605/9464654

交流QQ:1245178753

你可能感兴趣的:(qt,QTextEdit,分割窗口,QSplitter)