使用 PyQt4,如何将 mouseMoveEvent 设置为仅在 QMainWindow 中的 QWidget 内工作,而不在 MainWindow 中工作 [英] Using PyQt4, how do you set a mouseMoveEvent to only work inside of a QWidget in a QMainWindow but not in the MainWindow
问题描述
我下面的当前代码适用于更新我的 MainWindow 中 2 个 textBrowsers 中的 x-y 坐标,但是当光标位于 textBrowsers 内时它不起作用.
My current code below works for updating the x-y coordinates in 2 textBrowsers in my MainWindow, but it doesn't work when the cursor is inside of the textBrowsers.
对于此示例,我希望坐标仅在光标在 textBrowser_1 内移动时更新,而不会在其他任何地方移动.
For this example, I want the coordinates to ONLY update when the cursor is moving inside of textBrowser_1 and nowhere else.
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 上(如果它有一个):
From your code example, it looks like you may have already tried an event-filter, but that is probably the best solution. The trick is to install it on the viewport of the widget (if it has one):
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)
这篇关于使用 PyQt4,如何将 mouseMoveEvent 设置为仅在 QMainWindow 中的 QWidget 内工作,而不在 MainWindow 中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!