QScrollArea不尊重contentMargins设置

时间:2011-10-20 00:03:48

标签: qt qgraphicsview qscrollarea

出于某种原因,

QScrollArea在将QGraphicsView设置为小部件时忽略了contentMargins设置。看下面的代码片段,有人可以告诉我,我做错了什么或者它可能是SDK中的错误吗?

Snippet 1(效果很好):

QWidget *appWindow = new QWidget;

QScrollArea *sa = new QScrollArea(appWindow);
sa->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
sa->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
sa->setContentMargins(50, 50, 50, 50);

QWidget *widgetToScroll = new QWidget(sa);
widgetToScroll->resize(5000, 5000);
sa->setWidget(widgetToScroll);

QVBoxLayout *appWindowLayout = new QVBoxLayout(appWindow);
appWindowLayout->addWidget(sa);
appWindow->setLayout(appWindowLayout);

appWindow->show();

Snippet 2(完全忽略了setContentMargins()调用):

QWidget *appWindow = new QWidget;

QScrollArea *sa = new QScrollArea(appWindow);
sa->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
sa->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
sa->setContentMargins(50, 50, 50, 50);

QGraphicsView *widgetToScroll = new QGraphicsView(new QGraphicsScene(sa), sa);
widgetToScroll->setAlignment(Qt::AlignLeft | Qt::AlignTop);
widgetToScroll->resize(5000, 5000);
sa->setWidget(widgetToScroll);

QVBoxLayout *appWindowLayout = new QVBoxLayout(appWindow);
appWindowLayout->addWidget(sa);
appWindow->setLayout(appWindowLayout);

appWindow->show();

感谢。

2 个答案:

答案 0 :(得分:4)

为了使内容边距适用于QScrollArea小部件,我将其子类化并手动设置视口边距(这是QT 4.7中的受保护方法)

// Extended class
class QScrollAreaWithMargins : public QScrollArea
{
public:

    virtual void resizeEvent(QResizeEvent *event) override
    {
        // Define content margins here
        setViewportMargins(5, 0, 0, 0); // <<<<< SET MARGINS HERE
        QScrollArea::resizeEvent(event);
    }
};

// Usage
//...
mEditorScrollArea = new QScrollAreaWithMargins();
//...

答案 1 :(得分:3)

看起来你混淆了如何嵌套QGraphicsView和QGraphicsScene的结构。 (也许这只是一个错字?)

    QGraphicsView *widgetToScroll = new QGraphicsView(new QGraphicsScene(sa), sa);

应改为

    QGraphicsView *widgetToScroll = new QGraphicsView(new QGraphicsScene(), sa);

    QGraphicsView *widgetToScroll = new QGraphicsView();
    sa->setWidget(widgetToScroll);

将QWidget添加到布局时,可以更改窗口小部件的父级。将小部件(或QGraphicsView)设置为QScrollArea时,可以更改该小部件的父级。有关详细信息,请参阅Object Trees & Ownership。因此,如果您想在QScrollArea中设置QGraphicsView,您的代码将如下所示:

    QWidget *appWindow = new QWidget;

    QScrollArea *sa = new QScrollArea(); // No need to specify a parent here if
                                         // you add it to a layout later
    sa->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    sa->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    sa->setContentsMargins(50, 50, 50, 50);

    QGraphicsView *widgetToScroll = new QGraphicsView();
    widgetToScroll->setAlignment(Qt::AlignLeft | Qt::AlignTop);
    widgetToScroll->resize(5000, 5000);
    sa->setWidget(widgetToScroll); // This sets the parent for widgetToScroll

    QVBoxLayout *appWindowLayout = new QVBoxLayout();
    appWindowLayout->addWidget(sa); // This sets the parent for sa
    appWindow->setLayout(appWindowLayout); // This sets the parent for appWindowLayout

    appWindow->show();

作为旁注......

当QGraphicsViews与QGraphicsScene一起使用时,我不是使用QScrollArea的setContentsMargins设置边距,而是使用QGraphicsView自动滚动,只需将场景矩形设置为具有更大的余量,即我的内容大小如此:

    QWidget *appWindow = new QWidget;

    QGraphicsView *widgetToScroll = new QGraphicsView();
    QGraphicsScene *scene = new QGraphicsScene();
    scene->addRect(0,0, 5000, 5000);

    widgetToScroll->setSceneRect(-50,-50, 5050, 5050);
    widgetToScroll->setScene(scene);

    QVBoxLayout *appWindowLayout = new QVBoxLayout(appWindow);
    appWindowLayout->addWidget(widgetToScroll);

    appWindow->setLayout(appWindowLayout);
    appWindow->show();

QGraphicsView包含的不仅仅是在需要时自动滚动。您可以调整其中的所有内容并进行更多调整。它非常适合2D布局,交互和动画。有关详细信息,请参阅http://doc.qt.io/qt-5/graphicsview.html处的Qt图形视图框架。

以下是使用边距和填充时可能有用的更多信息:QStyleSheets使用的The Box Model