由于某些原因,当我将contentMargins设置为其小部件时,QScrollArea忽略了QGraphicsView设置。看看下面的代码片段,有没有人能告诉我是我做错了什么,还是SDK中的bug?
代码片段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()调用):
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();谢谢。
发布于 2011-10-20 15:35:04
看起来您混淆了如何嵌套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();顺便说一句。
当使用带有QGraphicsScene的QGraphicsViews,而不是使用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。
发布于 2012-06-30 04:29:47
为了让内容边距在QScrollArea小部件上正常工作,我继承了它并手动设置了视窗边距(这是Qt4.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();
//...https://stackoverflow.com/questions/7829678
复制相似问题