PyQT 如何在 QPushbutton 上制作 QEvent.Enter?

Kat*_*ina 2 pyqt

我的主要目标是,我有一个 Qpushbutton 和一个框架,我想要做的是。当我将鼠标悬停在 Qpushbutton 上时,框架会显示出来。使用可见假。有人可以帮助我了解如何举办活动吗?

Gar*_*hes 6

这是一个快速示例,类似于我在上一个问题中给出的示例:

from PyQt4.QtGui import QApplication, QMainWindow, QPushButton, \
            QWidget, QLabel
from PyQt4.QtCore import pyqtSignal            

class HoverButton(QPushButton):
    mouseHover = pyqtSignal(bool)

    def __init__(self, parent=None):
        QPushButton.__init__(self, parent)
        self.setMouseTracking(True)

    def enterEvent(self, event):
        self.mouseHover.emit(True)

    def leaveEvent(self, event):
        self.mouseHover.emit(False)

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent) 
        self.button = HoverButton(self)
        self.button.setText('Button')
        self.label = QLabel('QLabel uses QFrame...', self)
        self.label.move(40, 40)
        self.label.setVisible(False)       
        self.button.mouseHover.connect(self.label.setVisible)

def startmain():
    app = QApplication(sys.argv)
    mainwindow = MainWindow()
    mainwindow.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    import sys
    startmain()
Run Code Online (Sandbox Code Playgroud)