voodoogiant
voodoogiant

Reputation: 2148

qgraphicsitem positions return zero

I'm confused about how Qt is storing the positions on my custom elements. I'm inheriting from QGraphicsRectItem and I see "Page" items in the graphics view, but I noticed when I try to access their positions from pos() or scenePos() they both return (0,0) even though they're both at different locations in the view. I'm not sure if I'm just completely misunderstanding the relative positioning of everything, but I figured at least one of the two functions would return something different.

class Page(QtGui.QGraphicsRectItem):
    def __init__(self, x, y):
        super(Page, self).__init__(x, y, 80, 20)

class Workspace(QtGui.QGraphicsScene):
    def __init__(self, parent):
        super(Workspace, self).__init__(parent)

        page1 = Page(0, 0)
        self.addItem(page1)

        page2 = Page(100, 100)
        self.addItem(page2)

        page2.pos()      # returns (0,0)
        page2.scenePos() # returns (0,0)

Upvotes: 0

Views: 873

Answers (1)

cmannett85
cmannett85

Reputation: 22346

The QGraphicsRectItem's drawn rectangle has nothing to do with the item's position - there is no constructor available to you that sets the position.

What you're doing is creating a QGraphicsRectItem positioned at (0,0) but drawing a rectangle at (100,100) in item coordinates resulting in a bounding rectangle of (0,0,180,120).

You need to create the rectangle in item coordinates, and then move the item using setPos(..).

Upvotes: 3

Related Questions