最簡(jiǎn)單的分割窗體
[cpp]
view plaincopy#include <QApplication>
#include <QLabel>
#include <QSplitter>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSplitter * mainSplitter = new QSplitter(Qt::Horizontal);
mainSplitter->setHandleWidth(10);
mainSplitter->setStyleSheet("QSplitter::handle { background-color: black }"); //設(shè)置分界線的樣式
QLabel *label = new QLabel(QObject::tr("TestA"));
QLabel *label2 = new QLabel(QObject::tr("TestB"));
mainSplitter->addWidget(label2);
mainSplitter->addWidget(label);
mainSplitter->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
mainSplitter->show();
return app.exec();
}
結(jié)果截圖如下
豎向排列子窗口
[cpp]
view plaincopy#include <QApplication>
#include <QLabel>
#include <QSplitter>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSplitter * mainSplitter = new QSplitter(Qt::Horizontal);
mainSplitter->setHandleWidth(10);
mainSplitter->setStyleSheet("QSplitter::handle { background-color: black }"); //設(shè)置分界線的樣式
QLabel *label = new QLabel(QObject::tr("TestA"));
QLabel *label2 = new QLabel(QObject::tr("TestB"));
mainSplitter->addWidget(label2);
mainSplitter->addWidget(label);
mainSplitter->setOrientation(Qt::Vertical);
mainSplitter->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
mainSplitter->show();
return app.exec();
}
結(jié)果截圖如下
http://blog.csdn.net/a649518776/article/details/6633857轉(zhuǎn)
[cpp]
view plaincopy#include <QApplication>
#include <QSplitter>
#include <QTextCodec>
#include <QTextEdit>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//設(shè)置字體
QFont font("ZYSong18030",12);
a.setFont(font);
//The QTextCodec class provides conversions between text encodings
//Returns a pointer to the codec most suitable for this locale.On Windows, the codec will be based on a system locale.
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
//主splitter,以下填充text和子splitter
//QSplitter::QSplitter ( Qt::Orientation orientation, QWidget * parent = 0 )
QSplitter *splitterMain=new QSplitter(Qt::Horizontal,0);
QTextEdit *textLeft=new QTextEdit(QObject::tr("Left Widget"),splitterMain);
textLeft->setAlignment(Qt::AlignCenter);
QSplitter *splitterRight=new QSplitter(Qt::Vertical,splitterMain);
splitterRight->setOpaqueResize(false);
//開始填充右邊的splitter,放上兩個(gè)text
QTextEdit *textTop=new QTextEdit(QObject::tr("Right Top Widget"),splitterRight);
textTop->setAlignment(Qt::AlignCenter);
QTextEdit *textBottom=new QTextEdit(QObject::tr("Right Bottom Widget"),splitterRight);
textBottom->setAlignment(Qt::AlignCenter);
//設(shè)置主splitter
splitterMain->setStretchFactor(1,1);
splitterMain->setWindowTitle(QObject::tr("Splitter"));
splitterMain->show();
return a.exec();
}
解析:
1.QSplitter的構(gòu)造方法:
QSplitter *splitterMain=new QSplitter(Qt::Horizontal,0);
第一個(gè)參數(shù)通過(guò)
Qt::Horizontal 和
Qt::Vertical來(lái)設(shè)定為水平分割或垂直分割。第二個(gè)設(shè)定0代表是主窗口,無(wú)父窗口。
不過(guò)子splitter設(shè)置的
new QSplitter(Qt::Vertical,splitterMain);
代表主窗口是splitterMain。則子splitter就被添加到splitterMain中。
添加時(shí)為從左至右(或從上至下)添加;
2.添加QTextEdit控件。
QTextEdit *textLeft=new QTextEdit(QObject::tr("Left Widget"),splitterMain);
第一個(gè)參數(shù)設(shè)置文本內(nèi)容,第二個(gè)參數(shù)設(shè)置主窗體。
3.設(shè)置拖動(dòng)時(shí)是否實(shí)時(shí)更新
splitterRight->setOpaqueResize(false);
false為不實(shí)時(shí)更新——在拖動(dòng)時(shí)候只顯示一條灰色的線條,在拖動(dòng)到位并釋放鼠標(biāo)后再顯示分割條。默認(rèn)為true——實(shí)時(shí)更新。
4.設(shè)定可伸縮控件:
splitterMain->setStretchFactor(1,1);
第一個(gè)參數(shù)代表控件序號(hào),第一個(gè)序號(hào)為0.后面參數(shù)0代表不可伸縮,非0代表可伸縮。
這里設(shè)置右邊的可伸縮。當(dāng)拉伸整個(gè)窗口時(shí)候,左邊保持原來(lái)寬度。
拉伸后,我們發(fā)現(xiàn)左邊的寬度不變。