Python PyQt5在pyuic生成的代码之外添加eventFilter
我是这里的新Qt用户。 我有一个项目,我要使用pyuic生成的Python PyQt5在pyuic生成的代码之外添加eventFilter,python,pyqt,pyqt5,eventfilter,Python,Pyqt,Pyqt5,Eventfilter,我是这里的新Qt用户。 我有一个项目,我要使用pyuic生成的.py文件,但我没有访问权 我还应该在一些对象上安装事件过滤器。是否可以在生成的.py文件之外使用object.installEventFilter() 主窗口.py class Ui_MainWindow(QtWidgets.QMainWindow): self.titleLabel = QtWidgets.QLabel(MainWindow) from PyQt5 import QtCore, QtGui, QtWidgets
.py
文件,但我没有访问权
我还应该在一些对象上安装事件过滤器。是否可以在生成的.py
文件之外使用object.installEventFilter()
主窗口.py
class Ui_MainWindow(QtWidgets.QMainWindow):
self.titleLabel = QtWidgets.QLabel(MainWindow)
from PyQt5 import QtCore, QtGui, QtWidgets
from main_window import Ui_MainWindow
class Session (object):
def __init__(self):
self.mainUI = None
def eventFilter(self, source, event):
eventReturn = False
if(event.type() == QtCore.QEvent.MouseButtonDblClick and
source is self.lblTitle):
eventReturn = self._labelTitle(source, event)
return eventReturn
def _labelTitle(self, widget, event):
retVal = True
print("works, Title")
def GUIcontroller():
import sys
app = QtWidgets.QApplication(sys.argv)
thisSession = Session()
MainWindow = QtWidgets.QMainWindow()
thisSession.mainUI = Ui_MainWindow()
thisSession.mainUI.setupUi(MainWindow)
thisSession.mainUI.titleLabel.installEventFilter(???)
MainWindow.show()
sys.exit(app.exec_())
if __name__ == "__main__":
GUIcontroller()
前端.py
class Ui_MainWindow(QtWidgets.QMainWindow):
self.titleLabel = QtWidgets.QLabel(MainWindow)
from PyQt5 import QtCore, QtGui, QtWidgets
from main_window import Ui_MainWindow
class Session (object):
def __init__(self):
self.mainUI = None
def eventFilter(self, source, event):
eventReturn = False
if(event.type() == QtCore.QEvent.MouseButtonDblClick and
source is self.lblTitle):
eventReturn = self._labelTitle(source, event)
return eventReturn
def _labelTitle(self, widget, event):
retVal = True
print("works, Title")
def GUIcontroller():
import sys
app = QtWidgets.QApplication(sys.argv)
thisSession = Session()
MainWindow = QtWidgets.QMainWindow()
thisSession.mainUI = Ui_MainWindow()
thisSession.mainUI.setupUi(MainWindow)
thisSession.mainUI.titleLabel.installEventFilter(???)
MainWindow.show()
sys.exit(app.exec_())
if __name__ == "__main__":
GUIcontroller()
事件筛选器仅在QoObject中起作用,在代码中使用的对象不起作用,考虑到上述情况,可能的解决方案是:
从PyQt5导入QtCore、QtGui、qtwidget
从主窗口导入Ui\u主窗口
课堂会话(QtCore.QObject):
定义初始化(自我,用户界面):
超级()。\uuuu初始化\uuuu(ui)
self.\u ui=ui
self.ui.installEventFilter(self)
@财产
def用户界面(自我):
返回自我
def eventFilter(自身、源、事件):
如果event.type()==QtCore.QEvent.MouseButtonDblClick且源为self.ui:
打印(“双击”)
return super().eventFilter(源,事件)
def GUIcontroller():
导入系统
app=qtwidts.QApplication(sys.argv)
MainWindow=QtWidgets.QMainWindow()
mainUI=Ui\u主窗口()
mainUI.setupUi(主窗口)
thisSession=会话(mainUI.titleLabel)
MainWindow.show()
sys.exit(app.exec_())
如果名称=“\uuuuu main\uuuuuuuu”:
GUIcontroller()
事件过滤器仅在QoObject中起作用,在代码中使用的对象不起作用,考虑到上述情况,一种可能的解决方案是:
从PyQt5导入QtCore、QtGui、qtwidget
从主窗口导入Ui\u主窗口
课堂会话(QtCore.QObject):
定义初始化(自我,用户界面):
超级()。\uuuu初始化\uuuu(ui)
self.\u ui=ui
self.ui.installEventFilter(self)
@财产
def用户界面(自我):
返回自我
def eventFilter(自身、源、事件):
如果event.type()==QtCore.QEvent.MouseButtonDblClick且源为self.ui:
打印(“双击”)
return super().eventFilter(源,事件)
def GUIcontroller():
导入系统
app=qtwidts.QApplication(sys.argv)
MainWindow=QtWidgets.QMainWindow()
mainUI=Ui\u主窗口()
mainUI.setupUi(主窗口)
thisSession=会话(mainUI.titleLabel)
MainWindow.show()
sys.exit(app.exec_())
如果名称=“\uuuuu main\uuuuuuuu”:
GUIcontroller()
效果很好!现在我所有的活动都集中在一个地方了,谢谢!真是太棒了!现在我所有的活动都集中在一个地方了,谢谢!