Qt QSplitter任意拆分窗口

 

Qt拆分窗口可以使用QSplitter,也可以使用比较容易使用布局控件来完成,本文章给出使用QSplitter分割窗口的例子。

QSplitter 继承自QFrame,而QFrame又继承自QWidget,这点很重要。

对于使用QSplitter拆分窗口:

  1)新建一个QSplitter,就意味着新建了一个QWidget

  2)不同的QSplitter对象通过指定父子关系来完成嵌套(划分窗口)

  3)QSplitter可以放置到你指定的任何窗口内

 

例子1:将一个窗口拆分为左右两列,如下图所示:

代码如下:

int main(int argc, char *argv[])
{
//例子1,
qDebug("Mainframe start...\n");
QApplication app(argc, argv);

QTextCodec::setCodecForTr(QTextCodec::codecForName(QTextCodec::codecForLocale()->name()));
QTextCodec::setCodecForLocale(QTextCodec::codecForName(QTextCodec::codecForLocale()->name()));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName(QTextCodec::codecForLocale()->name()));

QSplitter *pSpliter = new QSplitter(Qt::Horizontal);
//QTextEdit* pLeftEdt = new QTextEdit(pSpliter); //此注释指定父窗口,打开后,后面的pSpliter->addWidget(pLeftEdt); 可以注释掉

QTextEdit* pLeftEdt = new QTextEdit;
pLeftEdt->setText(QObject::tr("左窗口"));
//QTextEdit* pRightEdt = new QTextEdit(pSpliter); //此注释指定父窗口,打开后,后面的pSpliter->addWidget(pRightEdt); 可以注释掉
QTextEdit* pRightEdt = new QTextEdit;
pRightEdt->setText(QObject::tr("右窗口"));
pSpliter->addWidget(pLeftEdt);
pSpliter->addWidget(pRightEdt);

pSpliter->setStretchFactor(0, 1);//下面两句设置左右两列的宽度比为1:4

pSpliter->setStretchFactor(1, 4);
pSpliter->setWindowTitle(QObject::tr("Split-Wnd Demo1"));
pSpliter->show();
return app.exec();

}

 

例子2:将一个窗口拆分为左右两列,右面再拆分为上下两列,如下图所示:

代码如下:

qDebug("Mainframe start...\n");
QApplication app(argc, argv);

QTextCodec::setCodecForTr(QTextCodec::codecForName(QTextCodec::codecForLocale()->name()));
QTextCodec::setCodecForLocale(QTextCodec::codecForName(QTextCodec::codecForLocale()->name()));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName(QTextCodec::codecForLocale()->name()));

QSplitter *pSpliter = new QSplitter(Qt::Horizontal);
QTextEdit* pLeftEdt = new QTextEdit(pSpliter);
pLeftEdt->setText(QObject::tr("左窗口"));

QSplitter *pRightSpliter = new QSplitter(Qt::Vertical, pSpliter);
QTextEdit* pRightTopEdt = new QTextEdit(pRightSpliter);
pRightTopEdt->setText(QObject::tr("右上窗口"));
QTextEdit* pRightBtmEdt = new QTextEdit(pRightSpliter);
pRightBtmEdt->setText(QObject::tr("右下窗口"));
pSpliter->setStretchFactor(0, 1);
pSpliter->setStretchFactor(1, 4);

pRightSpliter->setStretchFactor(0, 1);//右面窗口上下窗口高度比为1:4
pRightSpliter->setStretchFactor(1, 4);
pSpliter->setWindowTitle(QObject::tr("Split-Wnd Demo2"));

pSpliter->show();
return app.exec();

 

例子3:将一个窗口拆分为左右两列,左面再拆分为上下两列,与例子2恰好相反,如下图所示:

代码如下:

qDebug("Mainframe start...\n");
QApplication app(argc, argv);

QTextCodec::setCodecForTr(QTextCodec::codecForName(QTextCodec::codecForLocale()->name()));
QTextCodec::setCodecForLocale(QTextCodec::codecForName(QTextCodec::codecForLocale()->name()));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName(QTextCodec::codecForLocale()->name()));

QSplitter *pLeftSpliter = new QSplitter(Qt::Vertical);
QTextEdit* pLeftTopEdt = new QTextEdit(pLeftSpliter);
pLeftTopEdt->setText(QObject::tr("左上窗口"));
QTextEdit* pLeftBtmEdt = new QTextEdit(pLeftSpliter);
pLeftBtmEdt->setText(QObject::tr("左下窗口"));

QSplitter *pSpliter = new QSplitter(Qt::Horizontal);
QTextEdit* pRightEdt = new QTextEdit;
pRightEdt->setText(QObject::tr("右窗口"));

pSpliter->addWidget(pLeftSpliter);
pSpliter->addWidget(pRightEdt);

pSpliter->setStretchFactor(0, 4);
pSpliter->setStretchFactor(1, 1);//左右宽度比为4:1

pLeftSpliter->setStretchFactor(0, 4);//左面窗口上下窗口高度比为4:1
pLeftSpliter->setStretchFactor(1, 1);
pSpliter->setWindowTitle(QObject::tr("Split-Wnd Demo3"));

pSpliter->show();
return app.exec();

 

总结:QSplitter一次只能将一个窗口1分为2,横向竖向拆分由参数指定。要理清部件间的父子关系。有了这个思路,可以任意拆分窗口数量

posted @ 2014-03-23 11:36  peterliang  阅读(20224)  评论(0编辑  收藏  举报