定义
调用setStretchFactor函数用于设定可伸缩控件,第一个参数是指定设置的控件序号,控件序号按对象中控件插入的先后次序从0开始依次编号;第二个参数是一个大于0的值,表示此控件为可伸缩控件。
Updates the size policy of the widget at position index to have a stretch factor of stretch.
stretch is not the effective stretch factor; the effective stretch factor is calculated by taking the initial size of the widget and multiplying it with stretch.
setStretchFactor函数是下面这种代码的简单写法:
QWidget *widget = splitter->widget(index);
QSizePolicy policy = widget->sizePolicy();
policy.setHorizontalStretch(stretch);
policy.setVerticalStretch(stretch);
widget->setSizePolicy(policy);
QSplitter分割窗口的实例
#include "mainwindow.h"
#include <QApplication>
#include <QSplitter>
#include <QTextEdit>
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// MainWindow w;
// w.show();
//可分割窗口
QFont font("ZYSong18030",14);
a.setFont(font);
QSplitter *smain = new QSplitter(Qt::Horizontal,0);//水平分割窗口
QTextEdit *tleft = new QTextEdit("left",smain);
tleft->setAlignment(Qt::AlignCenter);//设置文字居中显示
QSplitter *sright = new QSplitter(Qt::Vertical,smain);
sright->setOpaqueResize(false);//设定分割窗口的分隔条在拖拽时是否为实时更新显示,若设为true则实时更新显示
QTextEdit *tright_top = new QTextEdit("right top",sright);
tright_top->setAlignment(Qt::AlignCenter);//设置文字居中显示
QTextEdit *tright_bottom = new QTextEdit("right bottom",sright);
tright_bottom->setAlignment(Qt::AlignCenter);//设置文字居中显示
smain->setStretchFactor(1,1);//选择smain中的第二个控件,即sright
smain->setWindowTitle("SPlitter");
smain->show();
return a.exec();
}
运行效果
可伸缩: