PDA

View Full Version : Overridden pos() in QGraphicsItem to link to QAbstractItemModel



lrb
10th October 2013, 06:36
Gday,

I have subclassed QGraphicsPixmapItem in order to link the position of the item directly to a data model (subclassed from QAbstractItemModel).

I have overridden pos to return values from the model using appropriate QModelIndex objects (I am using PySide for Qt 4.8):


def pos(self):
x = self.model.data(self.x_index)
y = self.model.data(self.y_index)
return QPointF(x, y)

However, pos is never called! I assumed the view (via the scene) would call pos for each item when drawing, however from the source it looks like the view/scene gets the position of a QGraphicsItem directly through 'd_ptr'.

Is what I am trying to do possible? I was hoping there was some method that would form the single point of access to pos but it looks like this is not the case.

Thanks in advance

wysota
10th October 2013, 07:35
pos() is not a virtual method so overriding it does not make sense.

lrb
19th October 2013, 09:39
Cheers. It's a shame Qt wasn't implemented that way, would've made it simple to link graphics items to data models.

wysota
19th October 2013, 09:50
I do not agree. pos() is merely a getter for part of the internal item matrix. If you want to control position of an item through a model, you can freely do that by teaching your model to use pos() and setPos() to modify the item position.