Python - PyQT4 how to detect the mouse click position anywhere in the window?

后端 未结 2 1975
遥遥无期
遥遥无期 2020-12-10 03:32

I have 1024x768 resolution window, when there is a click or mouse over, i want to find the x, y values. How can i do that?

import sys
from PyQt4 import QtGui         


        
相关标签:
2条回答
  • 2020-12-10 04:10

    The other way to get (x,y) coordinates:

    def mouseReleaseEvent(self, QMouseEvent):
        print('(', QMouseEvent.x(), ', ', QMouseEvent.y(), ')')
    
    0 讨论(0)
  • 2020-12-10 04:20
    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)
    
    0 讨论(0)
提交回复
热议问题