1

I have a QGraphicsScene which displays custom objects that inherit from QGraphicsItem. As an example, I want to display triangles that are made up like this:

struct Triangle {
    Point a;
    Point b;
    Point c;
};

The QGraphicsItem would look something like this:

class TriangleItem : public QGraphicsItem {
  public:
    Triange(QGraphicsItem* parent = 0);
    ...
  private:
    IndexTo _triangle;
};

However, I don't want to store the triangles within the QGraphicsItem directly, but rather in a std::vector or some other contigious data structure and keep them in a centralized place. There reason for this is, that I will have a bunch of algorithms performing transformations on these triangles. Since there are many of these triangles, my algorithms should be cache efficient and the datastructure should be easily iterateable.

The QGraphicsItems or the QGraphicsScene would then need hold a pointer/index to the triangles in this model. The Triangles in the scene are dynamic and can be moved/added/deleted (with the standard qt flags ItemIsSelectable, ItemIsMoveable). The problem is that a pointer into a std::vector is not guaranteed to stay valid.

I think it might be awkward to make the QGraphicsScene into an QAbstractItemView, so I am thinking about making my own model. However I am not quite sure on how to store the triangles internally and how to keep the model and the scene synchronized. How would you do this?

fuji
  • 1,173
  • 1
  • 10
  • 27
  • You can easy iterate over items using items(). QGraphicsScene stores items in bsp tree, so it not a simple vector. Probably you should implement your own scene. Also you can manually sync your vector with scene. – RazrFalcon Sep 19 '15 at 21:30

0 Answers0