4

I want to know how can i convert a QPointf position into a QPoint position in PyQt, because i need to set the mouse position using the position of an item. I've been trying with this, but nothing happens:

xq=node.pos().x()
yq= node.pos().y()
x=int(xq)
y=int(yq)
QtGui.QCursor.setPos(x,y)

Thanks in advance!

jamylak
  • 128,818
  • 30
  • 231
  • 230
karensantana
  • 1,599
  • 4
  • 21
  • 34

2 Answers2

7

Try these commands in the interpreter:

from PyQt4 import QtGui, QtCore
point = QtCore.QPointF(100.2, 144.23)
QtGui.QCursor.pos(), QtGui.QCursor.setPos(point.toPoint()), QtGui.QCursor.pos()
# (PyQt4.QtCore.QPoint(1335, 691), None, PyQt4.QtCore.QPoint(100, 144))

But the real problem is probably that you use item coordinates, which you have to map to global coordinates.

See this answer. Basically he maps coordinates in the following way:

item -> scene -> view -> global

Community
  • 1
  • 1
gatto
  • 2,947
  • 1
  • 21
  • 24
-1

Answer to the question in the title: https://doc.qt.io/qt-6/qpointf.html#toPoint

from PyQt6 import QtCore
pointf = QtCore.QPointF(100.2, 144.23)
point = pointf.toPoint()
print(point) # PyQt6.QtCore.QPoint(100, 144)
iTrooz
  • 1
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/34596694) – Chenmunka Jul 01 '23 at 18:41