title: Fixed size QGraphicsItems in multiple views?
body: <p>I'm visualizing a graph using Qt's graphics view framework. It looks something like this:</p>
  
  <p><img src="https://i.stack.imgur.com/NpHai.png" alt="Example view from the program"></p>
  
  <p>Now, I want the size of the vertices, which I'm drawing as small filled circles, to remain constant relative to the zooming of the view. At the moment I'm accomplishing this by not having my vertices as items in the scene, but instead drawing them in the view's <code>drawForeground(...)</code> function, like this:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>void View::drawForeground(QPainter *painter, const QRectF&amp; rect) {
      painter-&gt;setMatrixEnabled(false);
      painter-&gt;setPen(Qt::NoPen);
      painter-&gt;setBrush(Qt::black);
      painter-&gt;setRenderHint(QPainter::Antialiasing);
  
      // Draw points.
      const QRectF biggerRect = rect.adjusted(-100, -100, 100, 100);
      const float radius = 2.0;
      foreach (const QPointF&amp; point, m_points) {
          if (biggerRect.contains(point)) {
              painter-&gt;drawEllipse(QPointF(mapFromScene(point)), radius, radius);
          }
      }
  
      // ...
  }
  </code></pre>
  
  <p>But this is kludgy and wrong since the vertices are conceptually part of my scene, and should be items in my <code>QGraphicsScene</code>. Also, as an example, I might want to temporarily hide a set of vertices, or transform them, which would be much easier to do if they were proper items in the scene. Last but not least, having the views draw the points breaks the model/view separation and forces the views to know about the points they are displaying (there may be more than one view).</p>
  
  <p>Using this approach I also experienced trouble with the relative positions of vertices (non-items) and edges (items) ending up wrong when <code>render(...)</code>ing a view to a <code>QSvgGenerator</code>, probably due to my trick of disabling the transformation of the painter.</p>
  
  <p>So, my question is: How can I have my vertices as <code>QGraphicsItem</code>s added to my scene, while still having their sizes constant relative to the view, in each view showing the scene?</p>
  
  <p>I have tried setting the <a href="http://qt-project.org/doc/qt-5.0/qtwidgets/qgraphicsitem.html#GraphicsItemFlag-enum" rel="noreferrer">QGraphicsItem::ItemIgnoresTransformations</a> flag on the items. The documentation has this to say about the flag:</p>
  
  <blockquote>
    <p>The item ignores inherited transformations (i.e., its position is still anchored to its parent, but the parent or view rotation, zoom or shear transformations are ignored). This flag is useful for keeping text label items horizontal and unscaled, so they will still be readable if the view is transformed. When set, the item's view geometry and scene geometry will be maintained separately. You must call deviceTransform() to map coordinates and detect collisions in the view. By default, this flag is disabled. This flag was introduced in Qt 4.3.</p>
  </blockquote>
  
  <p>But setting this flag results in all view transformations being ignored, such that points will no longer appear to grow further apart when I zoom in, which is not what I want. I only want the item size (in on-screen pixels) to remain constant.</p>
  
  <p>Since Merlin069 asked for this: The code I used when I tried this flag out was simply adding each vertex to my scene like this:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>QGraphicsEllipseItem *item = scene-&gt;addEllipse(x - 2.0, y - 2.0, 4.0, 4.0);
  item-&gt;setPen(Qt::NoPen);
  item-&gt;setBrush(Qt::black);
  item-&gt;setFlag(QGraphicsItem::ItemIgnoresTransformations);
  </code></pre>
  
  <p>Below is a screenshot of what I got then. Zooming in 2x, 4x, 8x the item sizes remained constant (yay), but so did the on-screen distance between the points :(</p>
  
  <p><img src="https://i.stack.imgur.com/RGAtp.png" alt="Another screenshot"></p>
  
  <p>Any tips/pointers on a solution to this are much appreciated!</p>