@eyllanesc所说的可能是这样的:
from PyQt5.QtWidgets import (QApplication, QLabel, QWidget, QPushButton,
QComboBox, QHBoxLayout, QVBoxLayout)
from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot, QEvent, Qt
class ContextHelpSignals(QObject):
""" Used to send signals globally through the application
Eventually will be a Global Singelton """
textHelp = pyqtSignal(str)
def __init__(self):
super(ContextHelpSignals, self).__init__()
# Eventually will be a Global Singleton
_contextHelp = ContextHelpSignals()
class ContextHelpBaseClass(QObject):
""" All Widget that have context help inherits this class """
def __init__(self, **kw):
super(ContextHelpBaseClass, self).__init__()
self.helpText = None
def mousePressEvent(self, event):
''' THIS DISABLE WIDGETS NORMAL CLICK BEHAVIOR '''
_contextHelp.textHelp.emit(self.helpText)
# How can emit a signal and then pass this event to the normal widget
print(type(super()))
def SetHelpText(self, helpText): # ???
self.helpText = helpText
class ContexHelpDisplay(QLabel):
"""Dislpay Context Help frow widgets that have Context Help"""
def __init__(self, text):
super(ContexHelpDisplay, self).__init__()
self.setText(text)
_contextHelp.textHelp.connect(self.__displayHelp)
# Need to pass event to original widget
# type.mousePresseEvent() - How do I get type?
@pyqtSlot(str)
def __displayHelp(self, contextHelpText):
self.setText(contextHelpText)
class ContextHelpButton(QPushButton, ContextHelpBaseClass):
"""QPush Button with Context Help"""
def __init__(self, text):
super(ContextHelpButton, self).__init__()
self.setText(text)
self.helpText = "This is <b>QPushButton</b> Context Help Text"
class ContextHelpComboBox(QComboBox): # - --> , ContextHelpBaseClass):
"""QPush Button with Context Help"""
def __init__(self):
super(ContextHelpComboBox, self).__init__()
self.helpText = "This is <b>QComboBox</b> Context Help Text"
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
self.setupUI()
def setupUI(self):
self.resize(250, 150)
self.setWindowTitle('Context Help Example')
self.show()
button = ContextHelpButton("Test Button")
self.comboBox = ContextHelpComboBox() # + self.
self.comboBox.addItem("Test Item 1")
self.comboBox.addItem("Test Item 2")
self.comboBox.addItem("Test Item 3")
self.comboBox.installEventFilter(self) # <- Installs an event filter filterObj on this object.
self.helpTextDisplay = ContexHelpDisplay("Context Help")
vBox = QVBoxLayout()
vBox.addWidget(button)
vBox.addWidget(self.comboBox)
hBox = QHBoxLayout()
hBox.addLayout(vBox)
hBox.addWidget(self.helpTextDisplay)
self.setLayout(hBox)
# Filters events if this object has been installed as an event filter for the watched object.
def eventFilter(self, obj, event):
if event.type() == QEvent.MouseButtonPress and obj is self.comboBox:
self.helpTextDisplay.setText(self.comboBox.helpText)
return super(MainWindow, self).eventFilter(obj, event)
if __name__ == "__main__":
app = QApplication([])
ex = MainWindow()
exit(app.exec_())