我需要知道如何使鼠标悬停在小部件上时出现高亮显示的标签(或小框),例如当您使用浏览器并将鼠标放在(重新加载/返回/等...)按钮上时,一个小框出现并告诉您此按钮的作用我希望所有小部件都可以使用,而不仅仅是工具栏上的小部件
正如评论或ekhumoro所说setToolTip是解决方案
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("highligted 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_())