简体   繁体   中英

python qt designer mouse tracking cursor position

I am making a GUI for touchscreen

Pushbuttons in this GUI should generate signal when mouse cursor is on the pushbuttons

But qt designer does not have that kind of signal (I already tried released, clicked, pressed)

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

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

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_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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