Python - PyQT4 how to detect the mouse click position anywhere in the window?
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
class Example(QtWidgets.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def mousePressEvent(self, QMouseEvent):
print(QMouseEvent.pos())
def mouseReleaseEvent(self, QMouseEvent):
cursor = QtGui.QCursor()
print(cursor.pos())
def initUI(self):
qbtn = QtWidgets.QPushButton('Quit', self)
qbtn.resize(qbtn.sizeHint())
qbtn.move(50, 50)
self.setGeometry(0, 0, 1024, 768)
self.setWindowTitle('Quit button')
self.setWindowFlags(self.windowFlags() | QtCore.Qt.FramelessWindowHint)
self.show()
def main():
app = QtWidgets.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Output:
PyQt4.QtCore.QPoint(242, 285)
PyQt4.QtCore.QPoint(1741, 423)
PyQt4.QtCore.QPoint(439, 372)
The other way to get (x,y) coordinates:
def mouseReleaseEvent(self, QMouseEvent):
print('(', QMouseEvent.x(), ', ', QMouseEvent.y(), ')')