Daniel Frost
Daniel Frost

Reputation: 51

PyQt5 PushButton Hover Function Call

I want to call a function when the mouse would hover over a push button. There is basically no way I can use QPushButton:hover because the function I wanna call is quite complex and not much related to the button. Here's a sample code.

import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QLabel, QPushButton, QWidget

# ----------------------------------------------------

def firstText(myLabel):

    myLabel.setText("Wassup yall?")


def secondText(myLabel):

    myLabel.setText("Initial Text")

# ----------------------------------------------------

app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle("Wassup?")
window.setFixedWidth(1000)
window.setFixedHeight(600)
window.move(100, 50)


window.setStyleSheet(
    "background-color: rgb(208, 208, 208);"
)


button = QtWidgets.QPushButton(window)
button.setGeometry(100, 50, 120, 60)
button.setStyleSheet(
    "QPushButton {"
    "border: 0px solid;"
    "background-color: rgb(255, 50, 50);"
    "}"
)


label = QtWidgets.QLabel(window)
label.setGeometry(100, 120, 120, 60)
label.setStyleSheet(
    "background-color: rgb(128, 255, 64);"
)
label.setText("Initial Text")


window.show()
sys.exit(app.exec())

So I want the function 'firstText' to be called when I would hover over the button and 'secondText' to be called when I would leave the hover.

Upvotes: 0

Views: 1020

Answers (1)

eyllanesc
eyllanesc

Reputation: 243887

One possible solution is to override the enterEvent and leaveEvent methods:

import sys
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QApplication, QLabel, QPushButton, QWidget


class Button(QPushButton):
    entered = pyqtSignal()
    leaved = pyqtSignal()

    def enterEvent(self, event):
        super().enterEvent(event)
        self.entered.emit()

    def leaveEvent(self, event):
        super().leaveEvent(event)
        self.leaved.emit()


class Widget(QWidget):
    def __init__(self, parent=None):
        super().__init__()

        self.setWindowTitle("Wassup?")
        self.setFixedSize(1000, 600)
        self.move(100, 50)
        self.setStyleSheet("background-color: rgb(208, 208, 208);")

        button = Button(self)
        button.setGeometry(100, 50, 120, 60)
        button.setStyleSheet(
            "QPushButton {"
            "border: 0px solid;"
            "background-color: rgb(255, 50, 50);"
            "}"
        )

        self.label = QLabel(self)
        self.label.setGeometry(100, 120, 120, 60)
        self.label.setStyleSheet("background-color: rgb(128, 255, 64);")
        self.label.setText("Initial Text")

        button.entered.connect(self.handle_entered)
        button.leaved.connect(self.handle_leaved)

    def handle_entered(self):
        self.label.setText("Wassup yall?")

    def handle_leaved(self):
        self.label.setText("Initial Text")


app = QApplication(sys.argv)

window = Widget()
window.show()

sys.exit(app.exec())

Upvotes: 3

Related Questions