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

标签 python pyqt4

下面的当前代码适用于更新主窗口中 2 个文本浏览器中的 x-y 坐标,但当光标位于文本浏览器内部时它不起作用。

对于此示例,我希望坐标仅在光标移动到 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

最佳答案

从您的代码示例来看,您可能已经尝试过事件过滤器,但这可能是最好的解决方案。诀窍是将其安装在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)

关于python - 使用 PyQt4,如何将 mouseMoveEvent 设置为仅在 QMainWindow 中的 QWidget 内部工作,但不在 MainWindow 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40878157/

相关文章:

python - 如何为我的数据创建三个新列?

python - 类型错误 : closeEvent() takes exactly 4 arguments (2 given)

python - 如何在 PyQT 中打开新窗口时关闭窗口?

python - Pycharm:Python Qt代码代码补全

python - 如何理解gunicorn中的workers是如何被消耗的

Python:不完整的 URL 正则表达式输出

python - 如何使用 Flask/SQLalchemy/Alembic 初始化数据库表?

python - 用于在 Python 中编程 GUI 的所见即所得工具?

python - 在 QGraphicsGridLayout 中调整小部件大小

python - pandas to_numeric(…, downcast ='float' ) 失去精度