QT5布局管理

相关基本功能类
分割窗口QSplitter类
停靠窗口QDockWidget类
堆栈窗口QStackedWidget类
范例实现一个分割窗口的功能(在main函数里写)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QFont font("ZYsong18030", 12);
        a.setFont(font);
    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);//设置是否实时更新显示;
    QTextEdit *textUp = new QTextEdit(QObject::tr("Top Widget"), splitterRight);
    textUp->setAlignment(Qt::AlignCenter);
    QTextEdit *textButtom = new QTextEdit(QObject::tr("Bottom Widget"), splitterRight);
    textButtom->setAlignment(Qt::AlignCenter);
    splitterMain->setStretchFactor(1, 1);//设定伸缩控件
    splitterMain->setWindowTitle(QObject::tr("Splitter"));
    splitterMain->show();
    return a.exec();
}

停靠窗口用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "DockWindows.h"
#include <QTextEdit>
#include <QDockWidget>
using namespace Qt;

DockWindows::DockWindows(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle("DockWindows");
    QTextEdit* te = new QTextEdit(this);
    te->setText("Main Window");
    te->setAlignment(Qt::AlignCenter);
    setCentralWidget(te);
    //停靠窗口1;
    QDockWidget *dock = new QDockWidget("DockWindow1", this);
    dock->setFeatures(QDockWidget::DockWidgetMovable);       //movable;
    dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    QTextEdit *tel = new QTextEdit();
    tel->setText("window1,the dock widget can be moved between docks by the user" "");
    dock->setWidget(tel);
    addDockWidget(Qt::RightDockWidgetArea, dock);
    //2
    dock = new QDockWidget("DockWindow2", this);
    dock->setFeatures(QDockWidget::DockWidgetClosable|QDockWidget::DockWidgetFloatable);       //可关闭可浮动;
    dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    QTextEdit *te2 = new QTextEdit();
    te2->setText(tr("window1,the dock widget 可以浮动关闭停靠" ""));
    dock->setWidget(te2);
    addDockWidget(Qt::RightDockWidgetArea, dock);
    //3
    dock = new QDockWidget("dockWindow3", this);
    dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
    QTextEdit *te3 = new QTextEdit();
    te3->setText(tr("全部特效"));
    dock->setWidget(te3);
    addDockWidget(Qt::RightDockWidgetArea,dock);
    ui.setupUi(this);
}

堆栈窗体QstackedWidget类的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "StackDlg.h"
#include <QHBoxLayout>

StackDlg::StackDlg(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle("stackwidget");
    list = new QListWidget(this);
    list->insertItem(0, "window1");
    list->insertItem(1, "window2");
    list->insertItem(2, "window3");
    label1 = new QLabel("win1");
    label2 = new QLabel("win2");
    label3 = new QLabel("win3");
    stack = new QStackedWidget(this);
    stack->addWidget(label1);
    stack->addWidget(label2);
    stack->addWidget(label3);
    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->setMargin(5);//设定对话框边距
    mainLayout->setSpacing(5);//设定个控件之间的边距
    mainLayout->addWidget(list);//设定可伸缩控件
    mainLayout->addWidget(stack, 0, Qt::AlignHCenter);
    mainLayout->setStretchFactor(list, 1);)//设定可伸缩控件
    mainLayout->setStretchFactor(stack, 3);
    connect(list, SIGNAL(currentRowChanged(int)), stack, SLOT(setCurrentIndex(int)));//使用两个槽函数
    ui.setupUi(this);
}

基本布局

提供QGBoxLayout/QVBoxLayout/QGridLayout类,分别是水平布局,垂直布局,网格排列布局,前两者继承于QBoxLayout,QBoxLayout和后者统一继承于QLayout。

发表评论

电子邮件地址不会被公开。 必填项已用*标注