QSplitter

QSplitter是個版面分割元件,可以將視窗版面進行水平切割或垂直切割,這邊示範如何使用QSplitter。


widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <Qsplitter>
#include <QTextEdit>
#include <QHBoxLayout>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
private:
    QHBoxLayout *mainLayout;
    QSplitter *splitterMain;
    QSplitter *splitterRight;
    QTextEdit *textUp;
    QTextEdit *textMiddle;
    QTextEdit *textBottom;
};

#endif

widget.cpp

#include "widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent)
{
    setWindowTitle(tr("Splitter"));
    splitterMain = new QSplitter(Qt::Horizontal,this);
    QTextEdit *textLeft = new QTextEdit(tr("左邊"),splitterMain);
    textLeft->setAlignment(Qt::AlignCenter);

    splitterRight = new QSplitter(Qt::Vertical,splitterMain);

    textUp = new QTextEdit(tr("右上"),splitterRight);
    textUp->setAlignment(Qt::AlignCenter);
    textMiddle = new QTextEdit(tr("右中"),splitterRight);
    textMiddle->setAlignment(Qt::AlignCenter);
    textBottom = new QTextEdit(tr("右下"),splitterRight);
    textBottom->setAlignment(Qt::AlignCenter);
    splitterMain->setStretchFactor(1,1); 

    mainLayout = new QHBoxLayout(this);
    mainLayout->addWidget(splitterMain);
}

main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

QSplitter