Qt之自定义布局管理器(QBorderLayout)

Posted 一去丶二三里

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt之自定义布局管理器(QBorderLayout)相关的知识,希望对你有一定的参考价值。

简述

QBorderLayout,顾名思义-边框布局,实现了排列子控件包围中央区域的布局。

具体实现要求不再赘述,请参考前几节内容。

| 版权声明:一去、二三里,未经博主允许不得转载。

实现

QBorderLayout主要采用QLayout和QWidgetItem实现,而窗口使用了QWidget,中央窗体使用QTextBrowser,四周以QLabel排列。

效果

这里写图片描述

源码

QBorderLayout.h

#ifndef QBORDERLAYOUT_H
#define QBORDERLAYOUT_H

#include <QLayout>
#include <QRect>

class QBorderLayout : public QLayout
{
public:
    enum Position {West, North, South, East, Center};

    explicit QBorderLayout(QWidget *parent, int margin = 0, int spacing = -1);
    QBorderLayout(int spacing = -1);
    ~QBorderLayout();

    void addItem(QLayoutItem *item) Q_DECL_OVERRIDE;
    void addWidget(QWidget *widget, Position position);
    Qt::Orientations expandingDirections() const Q_DECL_OVERRIDE;
    bool hasHeightForWidth() const Q_DECL_OVERRIDE;
    int count() const Q_DECL_OVERRIDE;
    QLayoutItem *itemAt(int index) const Q_DECL_OVERRIDE;
    QSize minimumSize() const Q_DECL_OVERRIDE;
    void setGeometry(const QRect &rect) Q_DECL_OVERRIDE;
    QSize sizeHint() const Q_DECL_OVERRIDE;
    QLayoutItem *takeAt(int index) Q_DECL_OVERRIDE;

    void add(QLayoutItem *item, Position position);

private:
    struct ItemWrapper
    {
        ItemWrapper(QLayoutItem *i, Position p) {
            item = i;
            position = p;
        }

        QLayoutItem *item;
        Position position;
    };

    enum SizeType { MinimumSize, SizeHint };
    QSize calculateSize(SizeType sizeType) const;

    QList<ItemWrapper *> list;
};

#endif // QBORDERLAYOUT_H

QBorderLayout.cpp

#include "QBorderLayout.h"

QBorderLayout::QBorderLayout(QWidget *parent, int margin, int spacing)
    : QLayout(parent)
{
    setMargin(margin);
    setSpacing(spacing);
}

QBorderLayout::QBorderLayout(int spacing)
{
    setSpacing(spacing);
}

QBorderLayout::~QBorderLayout()
{
    QLayoutItem *l;
    while ((l = takeAt(0)))
        delete l;
}

void QBorderLayout::addItem(QLayoutItem *item)
{
    add(item, West);
}

void QBorderLayout::addWidget(QWidget *widget, Position position)
{
    add(new QWidgetItem(widget), position);
}

Qt::Orientations QBorderLayout::expandingDirections() const
{
    return Qt::Horizontal | Qt::Vertical;
}

bool QBorderLayout::hasHeightForWidth() const
{
    return false;
}

int QBorderLayout::count() const
{
    return list.size();
}

QLayoutItem *QBorderLayout::itemAt(int index) const
{
    ItemWrapper *wrapper = list.value(index);
    if (wrapper)
        return wrapper->item;
    else
        return 0;
}

QSize QBorderLayout::minimumSize() const
{
    return calculateSize(MinimumSize);
}

void QBorderLayout::setGeometry(const QRect &rect)
{
    ItemWrapper *center = 0;
    int eastWidth = 0;
    int westWidth = 0;
    int northHeight = 0;
    int southHeight = 0;
    int centerHeight = 0;
    int i;

    QLayout::setGeometry(rect);

    for (i = 0; i < list.size(); ++i) {
        ItemWrapper *wrapper = list.at(i);
        QLayoutItem *item = wrapper->item;
        Position position = wrapper->position;

        if (position == North) {
            item->setGeometry(QRect(rect.x(), northHeight, rect.width(),
                                    item->sizeHint().height()));

            northHeight += item->geometry().height() + spacing();
        } else if (position == South) {
            item->setGeometry(QRect(item->geometry().x(),
                                    item->geometry().y(), rect.width(),
                                    item->sizeHint().height()));

            southHeight += item->geometry().height() + spacing();

            item->setGeometry(QRect(rect.x(),
                              rect.y() + rect.height() - southHeight + spacing(),
                              item->geometry().width(),
                              item->geometry().height()));
        } else if (position == Center) {
            center = wrapper;
        }
    }

    centerHeight = rect.height() - northHeight - southHeight;

    for (i = 0; i < list.size(); ++i) {
        ItemWrapper *wrapper = list.at(i);
        QLayoutItem *item = wrapper->item;
        Position position = wrapper->position;

        if (position == West) {
            item->setGeometry(QRect(rect.x() + westWidth, northHeight,
                                    item->sizeHint().width(), centerHeight));

            westWidth += item->geometry().width() + spacing();
        } else if (position == East) {
            item->setGeometry(QRect(item->geometry().x(), item->geometry().y(),
                                    item->sizeHint().width(), centerHeight));

            eastWidth += item->geometry().width() + spacing();

            item->setGeometry(QRect(
                              rect.x() + rect.width() - eastWidth + spacing(),
                              northHeight, item->geometry().width(),
                              item->geometry().height()));
        }
    }

    if (center)
        center->item->setGeometry(QRect(westWidth, northHeight,
                                        rect.width() - eastWidth - westWidth,
                                        centerHeight));
}

QSize QBorderLayout::sizeHint() const
{
    return calculateSize(SizeHint);
}

QLayoutItem *QBorderLayout::takeAt(int index)
{
    if (index >= 0 && index < list.size()) {
        ItemWrapper *layoutStruct = list.takeAt(index);
        return layoutStruct->item;
    }
    return 0;
}

void QBorderLayout::add(QLayoutItem *item, Position position)
{
    list.append(new ItemWrapper(item, position));
}

QSize QBorderLayout::calculateSize(SizeType sizeType) const
{
    QSize totalSize;

    for (int i = 0; i < list.size(); ++i) {
        ItemWrapper *wrapper = list.at(i);
        Position position = wrapper->position;
        QSize itemSize;

        if (sizeType == MinimumSize)
            itemSize = wrapper->item->minimumSize();
        else // (sizeType == SizeHint)
            itemSize = wrapper->item->sizeHint();

        if (position == North || position == South || position == Center)
            totalSize.rheight() += itemSize.height();

        if (position == West || position == East || position == Center)
            totalSize.rwidth() += itemSize.width();
    }
    return totalSize;
}

使用

中央窗体使用QTextBrowser,四周以QLabel排列开来。

QTextBrowser *pCentralWidget = new QTextBrowser(this);
pCentralWidget->setPlainText(tr("Central Widget"));

QBorderLayout *pLayout = new QBorderLayout();
pLayout->addWidget(pCentralWidget, QBorderLayout::Center);
pLayout->addWidget(createLabel("North"), QBorderLayout::North);
pLayout->addWidget(createLabel("West"), QBorderLayout::West);
pLayout->addWidget(createLabel("East 1"), QBorderLayout::East);
pLayout->addWidget(createLabel("East 2") , QBorderLayout::East);
pLayout->addWidget(createLabel("South"), QBorderLayout::South);
setLayout(pLayout);

QLabel *MainWindow::createLabel(const QString &text)
{
    QLabel *pLabel = new QLabel(this);
    pLabel->setText(text);
    pLabel->setFrameStyle(QFrame::Box | QFrame::Raised);
    return pLabel;
}

以上是关于Qt之自定义布局管理器(QBorderLayout)的主要内容,如果未能解决你的问题,请参考以下文章

Qt之自定义布局管理器(QFlowLayout)

Qt之自定义搜索框——QLineEdit里增加一个Layout,还不影响正常输入文字(好像是一种比较通吃的方法)

11.QT-布局管理器(Box,Grid,Form,Stacked)

Qt-布局管理器

QT开发(二十一)——QT布局管理器

Qt布局管理器