I'm currently having an issue getting just the x position of the cursor so I can place a marker on that x and y location. I'm using QGraphicsScene and view to create this circle object at the location of the mouse when the mouse is clicked. Since the QGraphicsEllipseItem takes 4 arguments it seems I need the x and y coordinate separate not just what the position method gives you since it gives both x and y. How do I split the two coordinates up? Here's the code:
import sys
from PyQt4 import QtGui, QtCore
def paintMarkers(self):
self.cursor = QtGui.QCursor()
self.x,y = self.cursor.pos()
self.circleItem = QtGui.QGraphicsEllipseItem(self.x,self.y,10,10)
self.scene.addItem(self.circleItem)
self.circleItem.setPen(QtGui.QPen(QtCore.Qt.red, 1.5))
self.setScene(self.scene)
def mousePressEvent(self,QMouseEvent):
self.view.paintMarkers()
Much thanks!
I'm not 100% clear what your problem is (are you getting an exception? Does it run but you get unexpected output?), but this line looks like the culprit:
This will create
xas an attribute ofself, and then create a local variableythat has no association withselfat all. If you want both of them to be attributes ofself, doIf you were getting an error while trying to do
QGraphicsEllipseItem(self.x,self.y,10,10), this would explain why -self.ydidn't exist, so it would give you an AttributeError.