最简单的分割窗体
#include <QApplication>
#include <QLabel>
#include <QSplitter>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSplitter * mainSplitter = new QSplitter(Qt::Horizontal);
mainSplitter->setHandleWidth(10);
mainSplitter->setStyleSheet("QSplitter::handle { background-color: black }"); //设置分界线的样式
QLabel *label = new QLabel(QObject::tr("TestA"));
QLabel *label2 = new QLabel(QObject::tr("TestB"));
mainSplitter->addWidget(label2);
mainSplitter->addWidget(label);
mainSplitter->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
mainSplitter->show();
return app.exec();
}
结果截图如下
竖向排列子窗口
#include <QApplication>
#include <QLabel>
#include <QSplitter>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSplitter * mainSplitter = new QSplitter(Qt::Horizontal);
mainSplitter->setHandleWidth(10);
mainSplitter->setStyleSheet("QSplitter::handle { background-color: black }"); //设置分界线的样式
QLabel *label = new QLabel(QObject::tr("TestA"));
QLabel *label2 = new QLabel(QObject::tr("TestB"));
mainSplitter->addWidget(label2);
mainSplitter->addWidget(label);
mainSplitter->setOrientation(Qt::Vertical);
mainSplitter->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
mainSplitter->show();
return app.exec();
}
结果截图如下
http://blog.csdn.net/a649518776/article/details/6633857
转
#include <QApplication>
#include <QSplitter>
#include <QTextCodec>
#include <QTextEdit>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//设置字体
QFont font("ZYSong18030",12);
a.setFont(font);
//The QTextCodec class provides conversions between text encodings
//Returns a pointer to the codec most suitable for this locale.On Windows, the codec will be based on a system locale.
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
//主splitter,以下填充text和子splitter
//QSplitter::QSplitter ( Qt::Orientation orientation, QWidget * parent = 0 )
QSplitter *splitterMain=new QSplitter(Qt::Horizontal,0);
QTextEdit *textLeft=new QTextEdit(QObject::tr("Left Widget"),splitterMain);
textLeft->setAlignment(Qt::AlignCenter);
QSplitter *splitterRight=new QSplitter(Qt::Vertical,splitterMain);
splitterRight->setOpaqueResize(false);
//开始填充右边的splitter,放上两个text
QTextEdit *textTop=new QTextEdit(QObject::tr("Right Top Widget"),splitterRight);
textTop->setAlignment(Qt::AlignCenter);
QTextEdit *textBottom=new QTextEdit(QObject::tr("Right Bottom Widget"),splitterRight);
textBottom->setAlignment(Qt::AlignCenter);
//设置主splitter
splitterMain->setStretchFactor(1,1);
splitterMain->setWindowTitle(QObject::tr("Splitter"));
splitterMain->show();
return a.exec();
}
解析:
1.QSplitter的构造方法:
QSplitter *splitterMain=new QSplitter(Qt::Horizontal,0);
第一个参数通过Qt::Horizontal 和 Qt::Vertical来设定为水平分割或垂直分割。第二个设定0代表是主窗口,无父窗口。
不过子splitter设置的
new QSplitter(Qt::Vertical,splitterMain);
代表主窗口是splitterMain。则子splitter就被添加到splitterMain中。
添加时为从左至右(或从上至下)添加;
2.添加QTextEdit控件。
QTextEdit *textLeft=new QTextEdit(QObject::tr("Left Widget"),splitterMain);
第一个参数设置文本内容,第二个参数设置主窗体。
3.设置拖动时是否实时更新
splitterRight->setOpaqueResize(false);
false为不实时更新——在拖动时候只显示一条灰色的线条,在拖动到位并释放鼠标后再显示分割条。默认为true——实时更新。
![](https://img-my.csdn.net/uploads/201210/14/1350216979_3936.jpg)
4.设定可伸缩控件:
splitterMain->setStretchFactor(1,1);
第一个参数代表控件序号,第一个序号为0.后面参数0代表不可伸缩,非0代表可伸缩。
这里设置右边的可伸缩。当拉伸整个窗口时候,左边保持原来宽度。
![](https://img-my.csdn.net/uploads/201210/14/1350217009_5025.jpg)
拉伸后,我们发现左边的宽度不变。