QScrollArea不遵守contentMargins设置 [英] QScrollArea not respecting contentMargins setting

查看:136
本文介绍了QScrollArea不遵守contentMargins设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

QScrollArea 由于某种原因在以下情况下忽略了contentMargins设置:我将 QGraphicsView 设置为其小部件.看看下面的代码片段,有人可以告诉我我做错了什么吗,或者它可能是SDK中的错误吗?

QScrollArea, for some reason, is ignoring the contentMargins setting when I set QGraphicsView as its widget. Looking at the snippet below, can someone please tell if I'm doing something wrong or it could be a bug in the SDK?

片段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();

代码段2(就像 setContentMargins()一样,调用将被完全忽略):

Snippet 2 (It's like setContentMargins() call is ignored completely):

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();

谢谢.

推荐答案

您似乎在混淆如何嵌套QGraphicsView和QGraphicsScene的结构. (也许只是错字?)

It looks like you are confusing the structure of how to nest a QGraphicsView and a QGraphicsScene. (Maybe it was just a typo?)

    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时,将更改该小部件的父级.请参见对象树&所有权以获取更多信息.因此,如果您想在QScrollArea内设置QGraphicsView,您的代码将如下所示:

When you add a QWidget to a layout, you change the widget's parent. When you set a widget (or QGraphicsView) to a QScrollArea, you change that widget's parent. See Object Trees & Ownership for more information. So if you wanted to set up your QGraphicsView inside a QScrollArea your code would look like this:

    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自动滚动并仅将场景rect设置为具有更大的边距,以使内容的大小像这样:

When using QGraphicsViews with a QGraphicsScene, instead of setting the margins using a QScrollArea's setContentsMargins, I use the QGraphicsView automatic scrolling and just set the scene rect to have a larger margin that the size of my content like so:

    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 了解更多信息.

The QGraphicsView includes quite a bit more than just automatic scrolling when needed. You can resize everything inside of it and quite a bit more. It is great for 2D layouts, interactions and animations. See Qt's Graphics View Framework at http://doc.qt.io/qt-5/graphicsview.html for more information.

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

Here is more information that may be useful when using margins and paddings: The Box Model used by QStyleSheets.

这篇关于QScrollArea不遵守contentMargins设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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