先上结果图:
官方文档说明:
The QSplitter class implements a splitter widget; A splitter lets the user control the size of child widgets by dragging the boundary between them. Any number of widgets may be controlled by a single splitter. The typical use of a QSplitter is to create several widgets and add them using insertWidget() or addWidget(). 也就是说QSplitter 是一个分离控件的一个UI控件。被分离的控件之间的边界可以被调整,常见的创建方式是:
QSplitter *splitter = new QSplitter(parent); QListView *listview = new QListView; QTreeView *treeview = new QTreeView; QTextEdit *textedit = new QTextEdit; splitter->addWidget(listview); splitter->addWidget(treeview); splitter->addWidget(textedit);
在文档中值得注意的是:添加 QLayout进入QSplitter(或者是设置layout 把splitter作为layout的parent)是不被支持的。可以使用addWidget代替之。
上代码:
1 void 2 MainWindow::setUIComponent() 3 { 4 QSplitter * pSplitter = new QSplitter(Qt::Horizontal, ui->centralWidget); 5 6 ///-------------------------------------------------------------- 7 8 QSplitter * LeftSplitter = new QSplitter(Qt::Vertical, pSplitter);//可以使用设置parent来代替addWidget,初始的方式是横向的 9 10 QTextEdit * RightEdText = new QTextEdit(pSplitter); 11 RightEdText->setText("right - all"); 12 13 ///-------------------------------------------------------------- 14 15 QTextEdit * LeftTextEditUp = new QTextEdit(LeftSplitter); 16 LeftTextEditUp->setText("left - up"); 17 QTextEdit * LeftTextEditDown = new QTextEdit(LeftSplitter); 18 LeftTextEditDown->setText("left - down"); 19 20 pSplitter->setStretchFactor(0,4);//设置比例 21 pSplitter->setStretchFactor(1,1); 22 23 LeftSplitter->setStretchFactor(0,4); 24 LeftSplitter->setStretchFactor(1,1); 25 }