QGraphicView未在完整布局中最大化 [英] QGraphicView not maximized in complete layout

查看:15
本文介绍了QGraphicView未在完整布局中最大化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下具有四个QGraphicView对象的图形用户界面

您可以在每个视图下看到它有四个工具按钮。为了最大化一个视图,我连接了工具按钮的信号来隐藏其他三个视图的槽,并将所有四个视图的大小策略设置为展开。但它仅与视图1和视图4一起运行良好。其他视图显示以下结果:

我正在使用网格布局。视图2在左侧留下间隙,而视图3在右侧留下间隙。当我隐藏剩余部分时,我找不到任何理由说明为什么视图2和视图3留有间隙。

推荐答案

下面的代码可以在Qt4和Qt5下运行。它利用状态机框架来简化状态转换的管理。一个视图可见的状态是未显示任何视图的状态的子状态。这样就不需要显式的信号槽连接,也不需要保存视图和按钮的列表。

State类仅用于调试目的,以可视化状态转换。

唉,有个臭虫(比赛?)至少在Qt 4.8.5中,这使得属性赋值并不总是与嵌套状态一起正常工作。当使用具有重叠属性的嵌套状态时,尽管机器进入了正确的状态,但有时所有的视图和按钮都将被隐藏。下面的代码中有解决此问题的方法。

请注意,网格小部件不能有行/列跨度!=1的任何其他小部件。如果有,隐藏小部件周围的间距仍然可见。这就是我使用两个嵌套布局的原因。

#include <QApplication>
#include <QWidget>
#include <QGridLayout>
#include <QPushButton>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QStateMachine>
#include <QPlainTextEdit>
#include <QPointer>

static const int N = 4;
static QPointer<QPlainTextEdit> logView;

class State : public QState
{
public:
    explicit State(const QString& name, QState* parent = 0) : QState(parent) {
        setObjectName(name);
    }
protected:
    virtual void onEntry(QEvent*) {
        QString state = objectName();
        QState* parent = this;
        while ((parent = parent->parentState()) && !parent->objectName().isEmpty() )
        {
            state = parent->objectName() + "->" + state;
        }
        logView->appendHtml(QString("<font color="green">Entering state: <b>%1</b></font>").arg(state));
    }
    virtual void onExit(QEvent*) {
        QString state = objectName();
        QState* parent = this;
        while ((parent = parent->parentState()) && !parent->objectName().isEmpty() )
        {
            state = parent->objectName() + "->" + state;
        }
        logView->appendHtml(QString("<font color="red">Exiting state: <b>%1</b></font>").arg(state));
    }
};

class Widget : public QWidget {
public:
    explicit Widget(bool qt4) {
        QGridLayout *vert = new QGridLayout(this);
        QGridLayout *layout = new QGridLayout;
        layout->setContentsMargins(0, 0, 0, 0);
        logView = new QPlainTextEdit;
        logView->appendPlainText(QString("Qt %1").arg(QT_VERSION_STR));
        vert->addLayout(layout, 0, 0, 1, 1);
        vert->addWidget(logView, 1, 0, 1, 1);
        QStateMachine * const machine = new QStateMachine(this);
        machine->setObjectName("machine");
        State * const all = new State("all", machine);
        State * const none = new State("none", machine);
        QList<QState*> ones;
        for (int i = 0; i < N; ++ i) {
            const QString label = QString("View %1").arg(i+1);
            ones << new State(label, none);
        }
        for (int i = 0; i < N; ++ i) {
            QState *one = ones[i];
            QGraphicsView *view = new QGraphicsView;
            QGraphicsScene *scene = new QGraphicsScene(view);
            scene->addText(one->objectName());
            view->setScene(scene);
            layout->addWidget(view, 2*(i/2), i%2, 1, 1);
            QPushButton *button = new QPushButton(one->objectName());
            layout->addWidget(button, 2*(i/2)+1, i%2, 1, 1);
            button->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
            all->assignProperty(view, "visible", true);
            all->assignProperty(button, "visible", true);
            if (qt4) {
                // Workaround for a bug: properties in nested states are
                // sometimes not set correctly, so we explicitly set all properties
                // in one state.
                foreach (QState* s, ones) {
                    s->assignProperty(view, "visible", s == one);
                    s->assignProperty(button, "visible", s == one);
                }
            } else {
                none->assignProperty(view, "visible", false);
                none->assignProperty(button, "visible", false);
                one->assignProperty(view, "visible", true);
                one->assignProperty(button, "visible", true);
            }
            all->addTransition(button, SIGNAL(clicked()), one);
            one->addTransition(button, SIGNAL(clicked()), all);
            if (!none->initialState()) none->setInitialState(one);
        }
        machine->setInitialState(all);
        machine->start();
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w(QT_VERSION < QT_VERSION_CHECK(5,0,0));
    w.show();
    return a.exec();
}

这篇关于QGraphicView未在完整布局中最大化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆