简体   繁体   English

Python Qt Designer鼠标跟踪光标位置

[英]python qt designer mouse tracking cursor position

I am making a GUI for touchscreen 我正在为触摸屏制作GUI

Pushbuttons in this GUI should generate signal when mouse cursor is on the pushbuttons 当鼠标光标位于按钮上时,此GUI中的按钮应生成信号

But qt designer does not have that kind of signal (I already tried released, clicked, pressed) 但是qt designer没有这种信号(我已经尝试过释放,单击,按下)

Therefore, I think constantly tracking cursor position can be a solution 因此,我认为不断跟踪光标位置可能是一个解决方案

However, I have no idea how to implement mouse tracking (such as mousemoveEvent) in the code generated by qt designer and pyuic 但是,我不知道如何在qt designer和pyuic生成的代码中实现鼠标跟踪(例如mousemoveEvent)。

If I use the mouse tracking code from other examples, it does not work... 如果我使用其他示例中的鼠标跟踪代码,则无法使用...

Please help me 请帮我

Here is the code what contains essential parts only 这是仅包含必要部分的代码

from PyQt4 import QtCore, QtGui
import sys


try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)



class Ui_MainWindow(object):

    def mouseMoveEvent(self, event):
        current = QtGui.QCursor.pos()
        x = current.x()
        y = current.y()
        print("Mouse %d %d" % (x,y))

    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(1920, 720)






import resources_rc

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.setMouseTracking(True)
    MainWindow.show()
    sys.exit(app.exec_())

I solved this problem 我解决了这个问题

I used a code from https://github.com/bkach/earthquakeviz/blob/master/pyqt.py 我使用了来自https://github.com/bkach/earthquakeviz/blob/master/pyqt.py的代码

Making one more class is working 再上一堂课

Complete code 完整的代码

from PyQt4 import QtCore, QtGui
import sys


try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)


class Ui_MainWindow(object):

    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(1920, 720)
        MainWindow.setMouseTracking(True)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setMouseTracking(True)
        self.centralwidget.setObjectName("centralwidget")
        MainWindow.setCentralWidget(self.centralwidget)




class MainWIndowTest(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.centralwidget.installEventFilter(self)

    def eventFilter(self, object, event):
        if (event.type() == QtCore.QEvent.MouseMove):
            pos = event.pos()
            print("%d, %d" % (pos.x(), pos.y()))

        return QtGui.QWidget.eventFilter(self, object, event)

    def mouseMoveEvent(self, event):
        print("Moved")




import resources_rc

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    win = MainWIndowTest()
    win.show()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM