Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It looks like you are confusing the structure of how to nest a QGraphicsView and a QGraphicsScene. (Maybe it was just a typo?)</p> <pre><code> QGraphicsView *widgetToScroll = new QGraphicsView(new QGraphicsScene(sa), sa); </code></pre> <p>should be changed to</p> <pre><code> QGraphicsView *widgetToScroll = new QGraphicsView(new QGraphicsScene(), sa); </code></pre> <p>or</p> <pre><code> QGraphicsView *widgetToScroll = new QGraphicsView(); sa-&gt;setWidget(widgetToScroll); </code></pre> <p>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 <a href="http://doc.qt.io/qt-5/objecttrees.html" rel="nofollow noreferrer">Object Trees &amp; Ownership</a> for more information. So if you wanted to set up your QGraphicsView inside a QScrollArea your code would look like this:</p> <pre><code> QWidget *appWindow = new QWidget; QScrollArea *sa = new QScrollArea(); // No need to specify a parent here if // you add it to a layout later sa-&gt;setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); sa-&gt;setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); sa-&gt;setContentsMargins(50, 50, 50, 50); QGraphicsView *widgetToScroll = new QGraphicsView(); widgetToScroll-&gt;setAlignment(Qt::AlignLeft | Qt::AlignTop); widgetToScroll-&gt;resize(5000, 5000); sa-&gt;setWidget(widgetToScroll); // This sets the parent for widgetToScroll QVBoxLayout *appWindowLayout = new QVBoxLayout(); appWindowLayout-&gt;addWidget(sa); // This sets the parent for sa appWindow-&gt;setLayout(appWindowLayout); // This sets the parent for appWindowLayout appWindow-&gt;show(); </code></pre> <p>As a side note...</p> <p>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:</p> <pre><code> QWidget *appWindow = new QWidget; QGraphicsView *widgetToScroll = new QGraphicsView(); QGraphicsScene *scene = new QGraphicsScene(); scene-&gt;addRect(0,0, 5000, 5000); widgetToScroll-&gt;setSceneRect(-50,-50, 5050, 5050); widgetToScroll-&gt;setScene(scene); QVBoxLayout *appWindowLayout = new QVBoxLayout(appWindow); appWindowLayout-&gt;addWidget(widgetToScroll); appWindow-&gt;setLayout(appWindowLayout); appWindow-&gt;show(); </code></pre> <p>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 <a href="http://doc.qt.io/qt-5/graphicsview.html" rel="nofollow noreferrer">http://doc.qt.io/qt-5/graphicsview.html</a> for more information.</p> <p>Here is more information that may be useful when using margins and paddings: <a href="http://doc.qt.io/qt-5/stylesheet-customizing.html#box-model" rel="nofollow noreferrer">The Box Model</a> used by QStyleSheets.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload