QT 利用QSplitter 分割区域, 并添加QScrollArea 滚动区域,滚动条
1. QSplitter 分割区域, 可以分割区域中可以随意添加自己的布局
2.
#include "dialog.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); //Dialog w; //w.show(); QTextEdit *editor1 = new QTextEdit("test1"); QTextEdit *editor2 = new QTextEdit("test2"); QTextEdit *editor3 = new QTextEdit("test3"); QSplitter *splitter = new QSplitter(Qt::Vertical); splitter->addWidget(editor1); splitter->addWidget(editor2); QSplitter mainsp(Qt::Horizontal); mainsp.addWidget(editor3); mainsp.addWidget(splitter); mainsp.show(); return a.exec(); }
3. 添加滚动条
#include "dialog.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); //Dialog w; //w.show(); QTextEdit *editor1 = new QTextEdit("test1"); QTextEdit *editor2 = new QTextEdit("test2"); QTextEdit *editor3 = new QTextEdit("test3"); QSplitter *splitter = new QSplitter(Qt::Vertical); splitter->addWidget(editor1); splitter->addWidget(editor2); QSplitter *mainsp = new QSplitter(Qt::Horizontal); mainsp->addWidget(editor3); mainsp->addWidget(splitter); QScrollArea scrollArea; scrollArea.setWidget(mainsp); scrollArea.viewport()->setBackgroundRole(QPalette::Dark); scrollArea.viewport()->setAutoFillBackground(true); scrollArea.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); //控件大小 小于 视窗大小时,默认不会显示滚动条 scrollArea.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); //强制显示滚动条。 scrollArea.show(); return a.exec(); }