使用PyQt4,如何将mouseMoveEvent设置为仅在QMainWindow中的QWidget内部工作,而不是在MainWindow中工作

时间:2016-11-30 00:17:48

标签: python pyqt4

我目前的代码适用于更新MainWindow中2个textBrowsers中的x-y坐标,但当光标位于textBrowsers内部时,它不起作用。

对于这个例子,我希望当光标移动到textBrowser_1内部时,坐标才会更新。

from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4.QtGui import QApplication, QMainWindow
import sys


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.resize(800, 132)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)
        self.textBrowser_1 = QtGui.QTextBrowser(self.centralwidget)
        self.horizontalLayout.addWidget(self.textBrowser_1)
        self.textBrowser_2 = QtGui.QTextBrowser(self.centralwidget)
        self.horizontalLayout.addWidget(self.textBrowser_2)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        MainWindow.setStatusBar(self.statusbar)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)


class MyMainScreen(QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.ui = Ui_MainWindow()  # This is from a python export from QtDesigner
        self.ui.setupUi(self)
        self.setMouseTracking(True)
        self.ui.textBrowser_1.installEventFilter(self)
        # self.ui.textBrowser_1.setMouseTracking(True)
        # self.ui.menubar.setMouseTracking(True)
        # self.ui.statusbar.setMouseTracking(True)

    def setMouseTracking(self, flag):
        def recursive_set(parent):
            for child in parent.findChildren(QtCore.QObject):
                try:
                    child.setMouseTracking(flag)
                except:
                    pass
                recursive_set(child)
        QtGui.QWidget.setMouseTracking(self, flag)
        recursive_set(self)



    def mouseMoveEvent(self, event):
        self.ui.textBrowser_1.setText(str(event.x()))
        self.ui.textBrowser_2.setText(str(event.y()))
        QtGui.QMainWindow.mouseMoveEvent(self, event)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainscreen = MyMainScreen()
    mainscreen.show()
    app.exec_()

这是程序的样子: mouseTest

1 个答案:

答案 0 :(得分:0)

从您的代码示例中,您可能已经尝试过事件过滤器,但这可能是最佳解决方案。诀窍是将其安装在小部件的viewport上(如果有的话):

class MyMainScreen(QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.textBrowser_1.setMouseTracking(True)
        self.ui.textBrowser_1.viewport().installEventFilter(self)

    def eventFilter(self, source, event):
        if event.type() == QtCore.QEvent.MouseMove:
            self.ui.textBrowser_1.setText(str(event.x()))
            self.ui.textBrowser_2.setText(str(event.y()))
        return QtGui.QMainWindow.eventFilter(self, source, event)