QT 之QSplitter 切割窗口

QT之切分窗口

     QSplitter就是一个可以包含一些其他窗口部件的部件。在切分窗口QSplitter中的这些窗口部件会通过切分条Splitter handle而分割开来。用户可以通过拖动这些分割条改变切分条中子窗口的大小。QSplitter中的子窗口部件将会自动按照创建时的顺序一个挨着一个的放在一起,并且以切分窗口分割条来分割相邻的窗口。下面是代码实例

#include
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QTextEdit *editor1 = new QTextEdit;    //定于在splitter中的三个编辑框
    QTextEdit *editor2 = new QTextEdit;
    QTextEdit *editor3 = new QTextEdit;
    QSplitter splitter(Qt::Horizontal);        //定义一个切分窗口
   splitter.addWidget(editor1);               //将文件控件加入到切分框
    splitter.addWidget(editor2);
    splitter.addWidget(editor3);        
    editor1->setPlainText("Mon enfant, ma soeur,\n"
                          "Songe  la douceur\n"
                          "D'aller l-bas vivre ensemble,\n"
                          "Aimer  loisir,\n"
                          "Aimer et mourir\n"
                          "Au pays qui te ressemble.");
    editor2->setPlainText("My child, my sister,\n"
                          "think of the sweetness\n"
                          "of going there to live together!\n"
                          "To love at leisure,\n"
                          "to love and to die\n"
                          "in a country that is the image of you!");
    editor3->setPlainText("Mein Kind, meine Schwester,\n"
                          "denke an den Traum\n"
                          "dort hin(unter) zu gehen um zusammen\n"
                          "zu leben und in aller Ruhe zu lieben,\n"
                          "Zu lieben und zu sterben\n"
                          "in dem Land, das dir gleicht.");
    splitter.setWindowTitle(QObject::tr("Splitter"));          //设置标题
    splitter.show();
    return app.exec();

}

效果如下:

QT 之QSplitter 切割窗口_第1张图片

可以随意改变各个窗口大小:

QT 之QSplitter 切割窗口_第2张图片

另外下面演示下使用多个splitter进行水平或者垂直方向上的嵌套。本代码适用于《C++ GUI QT4》

mailclient.cpp. 其界面布局部分代码如下

//窗口布局
    rightSplitter = new QSplitter(Qt::Vertical);
    rightSplitter->addWidget(messagesTreeWidget);  //在右窗口中添加一个邮件列表
    rightSplitter->addWidget(textEdit);                         //显示文本邮件
    rightSplitter->setStretchFactor(1, 1);                      //保证textEdit伸展到多余的空间
    mainSplitter = new QSplitter(Qt::Horizontal);
    mainSplitter->addWidget(foldersTreeWidget);
    mainSplitter->addWidget(rightSplitter);
    mainSplitter->setStretchFactor(1, 1);                     //使用两个setStretchFactor保证了把多余的额外空间都留给textEdit
    setCentralWidget(mainSplitter);
    setWindowTitle(tr("Mail Client"));

效果如下图:

QT 之QSplitter 切割窗口_第3张图片

你可能感兴趣的:(软件)