eventFilter
import sys
from PyQt5.QtWidgets import (QLabel, QApplication, QWidget, QVBoxLayout)
from PyQt5.QtCore import (QSize, QEvent)
class Widget(QWidget):
def __init__(self, parent=None):
try:
super(QWidget, self).__init__(parent)
self.setFixedSize(QSize(200, 200))
self.setStyleSheet("background-color: gray;")
self.layout = QVBoxLayout()
self.label = QLabel()
self.label.setFixedSize(QSize(100, 20))
self.label.setStyleSheet("background-color: #000000; color: yellow;")
self.label.setText("highlighted label")
self.label.setVisible(False)
self.layout.addWidget(self.label)
self.setLayout(self.layout)
self.installEventFilter(self)
except Exception as e:
print(e)
def eventFilter(self, _object, event):
try:
print(f"{_object} {event}")
if event.type() == QEvent.Enter:
self.label.setVisible(True)
elif event.type() == QEvent.Leave:
self.label.setVisible(False)
return False
except Exception as e:
print(e)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())