Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy doesn't setPos() move the QGraphicsItems in the scene?
    primarykey
    data
    text
    <p>In the code below, I have three <code>QGraphicsItem</code>s that are laid out by <code>QGraphicsLinearLayout</code>, which is set as layout to a <code>QGraphicsWidget</code>.</p> <pre><code>#include &lt;QApplication&gt; #include &lt;QBrush&gt; #include &lt;QDebug&gt; #include &lt;QGraphicsItem&gt; #include &lt;QGraphicsLayoutItem&gt; #include &lt;QGraphicsLinearLayout&gt; #include &lt;QGraphicsRectItem&gt; #include &lt;QGraphicsScene&gt; #include &lt;QGraphicsView&gt; #include &lt;QGraphicsWidget&gt; #include &lt;QPen&gt; class MyShape : public QGraphicsRectItem, public QGraphicsLayoutItem { public: MyShape(void) { setPen(QPen(QBrush(Qt::black), 1)); setBrush(QBrush(Qt::green)); setRect(0, 0, 20, 20); } virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF&amp; constraint = QSizeF()) const { Q_UNUSED(which); Q_UNUSED(constraint); return boundingRect().size(); } virtual void setGeometry(const QRectF&amp; rect) { setPos(rect.topLeft()); } }; int main(int argc, char** argv) { QApplication app(argc, argv); QGraphicsScene scene; MyShape* shape1 = new MyShape; MyShape* shape2 = new MyShape; MyShape* shape3 = new MyShape; scene.addItem(shape1); scene.addItem(shape2); scene.addItem(shape3); QGraphicsLinearLayout* layout = new QGraphicsLinearLayout; layout-&gt;addItem(shape1); layout-&gt;addItem(shape2); layout-&gt;addItem(shape3); QGraphicsWidget* container = new QGraphicsWidget; container-&gt;setLayout(layout); scene.addItem(container); container-&gt;setPos(300, 300); // This doesn't appear to have any affect // Item for indicating origin QGraphicsRectItem* tmp = scene.addRect(0, 0, 2, 2, QPen(), QBrush(Qt::green)); tmp-&gt;setPos(0, 0); qDebug() &lt;&lt; tmp-&gt;scenePos(); qDebug() &lt;&lt; container-&gt;scenePos(); QGraphicsView view; view.setScene(&amp;scene); view.centerOn(0, 0); view.show(); return app.exec(); } </code></pre> <p>Then, I try to move the <code>QGraphicsWidget</code> within the scene by calling <code>setPos()</code>, but it doesn't appear to work (the <code>QGraphicsItem</code>s remain in the same place). However, <em>something</em> appears to happen since the scrollbars change. It seems that the <code>QGraphicsWidget</code> moves, without taking the <code>QGraphicsItem</code>s along with it.</p> <p>Why?</p> <h2>EDIT:</h2> <p>By suggestion from <em>DerManu</em>, I changed the inheritance of <code>MyShape</code> to <code>QGraphicsWidget</code>, and with the following code, it works:</p> <pre><code>#include &lt;QApplication&gt; #include &lt;QBrush&gt; #include &lt;QDebug&gt; #include &lt;QGraphicsItem&gt; #include &lt;QGraphicsLayoutItem&gt; #include &lt;QGraphicsLinearLayout&gt; #include &lt;QGraphicsRectItem&gt; #include &lt;QGraphicsScene&gt; #include &lt;QGraphicsView&gt; #include &lt;QGraphicsWidget&gt; #include &lt;QPen&gt; class MyShape : public QGraphicsWidget { public: MyShape(void) { setMinimumSize(20, 20); setMaximumSize(20, 20); setPreferredSize(20, 20); } QRectF boundingRect(void) const { QRectF box(0, 0, 22, 22); box.translate(box.center() * -1); return box; } void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) { Q_UNUSED(option); Q_UNUSED(widget); // Set box border appearance painter-&gt;setPen(QPen(Qt::black, 1)); // Set box background appearance painter-&gt;setBrush(QBrush(Qt::green)); QRectF box(0, 0, 20, 20); box.translate(box.center() * -1); painter-&gt;drawRect(box); } }; int main(int argc, char** argv) { QApplication app(argc, argv); QGraphicsScene scene; MyShape* shape1 = new MyShape; MyShape* shape2 = new MyShape; MyShape* shape3 = new MyShape; scene.addItem(shape1); scene.addItem(shape2); scene.addItem(shape3); QGraphicsLinearLayout* layout = new QGraphicsLinearLayout(Qt::Vertical); layout-&gt;addItem(shape1); layout-&gt;addItem(shape2); layout-&gt;addItem(shape3); QGraphicsWidget* container = new QGraphicsWidget; container-&gt;setLayout(layout); scene.addItem(container); container-&gt;setPos(200, 200); // This doesn't appear to have any affect scene.setSceneRect(-300, -300, 600, 600); // Item for indicating origin QGraphicsRectItem* tmp = scene.addRect(0, 0, 2, 2, QPen(), QBrush(Qt::green)); tmp-&gt;setPos(0, 0); qDebug() &lt;&lt; tmp-&gt;scenePos(); qDebug() &lt;&lt; container-&gt;scenePos(); QGraphicsView view; view.setScene(&amp;scene); view.centerOn(0, 0); view.show(); return app.exec(); } </code></pre> <p>So what is it that <code>QGraphicsWidget</code> provides that I missed in the original code such that using <code>setPos</code> on the container item correctly moves the position of the <code>MyShape</code> items?</p>
    singulars
    1. This table or related slice is empty.
    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. 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