PyQt - Change QPushButton Icon on hover and pressed

PyQt - Change QPushButton Icon on hover and pressed

In PyQt, you can change the icon of a QPushButton when it is hovered over or pressed by utilizing the setStyleSheet method along with pseudo-states like :hover and :pressed. Here's an example:

from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget from PyQt5.QtGui import QIcon class MyWindow(QWidget): def __init__(self): super().__init__() self.init_ui() def init_ui(self): layout = QVBoxLayout() # Create a QPushButton with an initial icon self.button = QPushButton(QIcon('icon_normal.png'), 'Click me', self) # Connect signals for hover and pressed events self.button.clicked.connect(self.on_pressed) self.button.installEventFilter(self) layout.addWidget(self.button) self.setLayout(layout) self.setWindowTitle('QPushButton Icon Change') self.setGeometry(300, 300, 300, 200) def on_pressed(self): print('Button pressed!') def eventFilter(self, obj, event): if obj == self.button and event.type() == event.HoverEnter: # Change the icon when the button is hovered self.button.setIcon(QIcon('icon_hover.png')) elif obj == self.button and event.type() == event.HoverLeave: # Change the icon back to the normal state self.button.setIcon(QIcon('icon_normal.png')) return super().eventFilter(obj, event) if __name__ == '__main__': app = QApplication([]) window = MyWindow() window.show() app.exec_() 

In this example:

  • The QPushButton is created with an initial icon ('icon_normal.png').
  • The eventFilter method is used to detect hover events (event.HoverEnter and event.HoverLeave).
  • When the button is hovered over, the icon is changed to a different one ('icon_hover.png').
  • When the button is no longer hovered, the icon is changed back to the normal state.
  • The clicked signal is connected to the on_pressed method to handle the button press event.

Make sure to replace 'icon_normal.png' and 'icon_hover.png' with the actual paths to your icon files.

Examples

  1. "PyQt QPushButton change icon on hover"

    • Code:
      from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget from PyQt5.QtCore import Qt class CustomButton(QPushButton): def __init__(self, parent=None): super().__init__(parent) self.setIcon(QIcon('normal_icon.png')) self.setStyleSheet("border: 1px solid black;") def enterEvent(self, event): self.setIcon(QIcon('hover_icon.png')) def leaveEvent(self, event): self.setIcon(QIcon('normal_icon.png')) app = QApplication([]) window = QWidget() layout = QVBoxLayout() button = CustomButton() layout.addWidget(button) window.setLayout(layout) window.show() app.exec_() 
    • Description: Subclasses QPushButton to create a custom button with changing icons on hover.
  2. "PyQt QPushButton change icon on pressed"

    • Code:
      from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget from PyQt5.QtCore import Qt class CustomButton(QPushButton): def __init__(self, parent=None): super().__init__(parent) self.setIcon(QIcon('normal_icon.png')) self.setStyleSheet("border: 1px solid black;") def mousePressEvent(self, event): self.setIcon(QIcon('pressed_icon.png')) def mouseReleaseEvent(self, event): self.setIcon(QIcon('normal_icon.png')) app = QApplication([]) window = QWidget() layout = QVBoxLayout() button = CustomButton() layout.addWidget(button) window.setLayout(layout) window.show() app.exec_() 
    • Description: Subclasses QPushButton to create a custom button with changing icons on mouse press.
  3. "PyQt QPushButton change icon on hover and pressed"

    • Code:
      from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget from PyQt5.QtCore import Qt class CustomButton(QPushButton): def __init__(self, parent=None): super().__init__(parent) self.setIcon(QIcon('normal_icon.png')) self.setStyleSheet("border: 1px solid black;") def enterEvent(self, event): self.setIcon(QIcon('hover_icon.png')) def leaveEvent(self, event): self.setIcon(QIcon('normal_icon.png')) def mousePressEvent(self, event): self.setIcon(QIcon('pressed_icon.png')) def mouseReleaseEvent(self, event): self.setIcon(QIcon('normal_icon.png')) app = QApplication([]) window = QWidget() layout = QVBoxLayout() button = CustomButton() layout.addWidget(button) window.setLayout(layout) window.show() app.exec_() 
    • Description: Combines hover and pressed effects in a custom QPushButton with changing icons.
  4. "PyQt QPushButton change icon color on hover"

    • Code:
      from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget from PyQt5.QtCore import Qt class CustomButton(QPushButton): def __init__(self, parent=None): super().__init__(parent) self.setIcon(QIcon('normal_icon.png')) self.setStyleSheet("border: 1px solid black; color: black;") def enterEvent(self, event): self.setStyleSheet("border: 1px solid black; color: blue;") def leaveEvent(self, event): self.setStyleSheet("border: 1px solid black; color: black;") app = QApplication([]) window = QWidget() layout = QVBoxLayout() button = CustomButton() layout.addWidget(button) window.setLayout(layout) window.show() app.exec_() 
    • Description: Changes the icon color on hover while keeping the original border color.
  5. "PyQt QPushButton change icon size on hover"

    • Code:
      from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget from PyQt5.QtCore import Qt class CustomButton(QPushButton): def __init__(self, parent=None): super().__init__(parent) self.setIcon(QIcon('normal_icon.png')) self.setIconSize(QSize(50, 50)) self.setStyleSheet("border: 1px solid black;") def enterEvent(self, event): self.setIconSize(QSize(60, 60)) def leaveEvent(self, event): self.setIconSize(QSize(50, 50)) app = QApplication([]) window = QWidget() layout = QVBoxLayout() button = CustomButton() layout.addWidget(button) window.setLayout(layout) window.show() app.exec_() 
    • Description: Changes the icon size on hover in a custom QPushButton.
  6. "PyQt QPushButton change icon opacity on hover"

    • Code:
      from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget from PyQt5.QtCore import Qt class CustomButton(QPushButton): def __init__(self, parent=None): super().__init__(parent) self.setIcon(QIcon('normal_icon.png')) self.setStyleSheet("border: 1px solid black;") def enterEvent(self, event): icon = self.icon() icon.addPixmap(icon.pixmap(QSize(50, 50)).transformed( QTransform().scale(1.2, 1.2)), QIcon.Normal) self.setIcon(icon) def leaveEvent(self, event): icon = self.icon() icon.addPixmap(icon.pixmap(QSize(50, 50)).transformed( QTransform().scale(1, 1)), QIcon.Normal) self.setIcon(icon) app = QApplication([]) window = QWidget() layout = QVBoxLayout() button = CustomButton() layout.addWidget(button) window.setLayout(layout) window.show() app.exec_() 
    • Description: Changes the icon opacity on hover using pixmap transformations.
  7. "PyQt QPushButton change icon dynamically"

    • Code:
      from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget from PyQt5.QtGui import QIcon class CustomButton(QPushButton): def __init__(self, parent=None): super().__init__(parent) self.icon_normal = QIcon('normal_icon.png') self.icon_hover = QIcon('hover_icon.png') self.icon_pressed = QIcon('pressed_icon.png') self.setIcon(self.icon_normal) self.setStyleSheet("border: 1px solid black;") def enterEvent(self, event): self.setIcon(self.icon_hover) def leaveEvent(self, event): self.setIcon(self.icon_normal) def mousePressEvent(self, event): self.setIcon(self.icon_pressed) def mouseReleaseEvent(self, event): self.setIcon(self.icon_normal) app = QApplication([]) window = QWidget() layout = QVBoxLayout() button = CustomButton() layout.addWidget(button) window.setLayout(layout) window.show() app.exec_() 
    • Description: Dynamically changes the icon on hover, press, and release events in a custom QPushButton.
  8. "PyQt QPushButton change icon with animation"

    • Code:
      from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget from PyQt5.QtCore import QPropertyAnimation, QEasingCurve class CustomButton(QPushButton): def __init__(self, parent=None): super().__init__(parent) self.setIcon(QIcon('normal_icon.png')) self.setStyleSheet("border: 1px solid black;") self.animation = QPropertyAnimation(self, b"iconSize") self.animation.setEasingCurve(QEasingCurve.OutElastic) def enterEvent(self, event): self.animation.setStartValue(self.iconSize()) self.animation.setEndValue(self.iconSize() * 1.2) self.animation.start() def leaveEvent(self, event): self.animation.setStartValue(self.iconSize()) self.animation.setEndValue(self.iconSize() / 1.2) self.animation.start() app = QApplication([]) window = QWidget() layout = QVBoxLayout() button = CustomButton() layout.addWidget(button) window.setLayout(layout) window.show() app.exec_() 
    • Description: Changes the icon size with animation on hover using QPropertyAnimation.
  9. "PyQt QPushButton change icon with fade effect"

    • Code:
      from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget from PyQt5.QtCore import Qt, QPropertyAnimation, QEasingCurve class CustomButton(QPushButton): def __init__(self, parent=None): super().__init__(parent) self.setIcon(QIcon('normal_icon.png')) self.setStyleSheet("border: 1px solid black;") self.animation = QPropertyAnimation(self, b"windowOpacity") self.animation.setEasingCurve(QEasingCurve.OutCubic) self.animation.setDuration(300) def enterEvent(self, event): self.animation.setStartValue(self.windowOpacity()) self.animation.setEndValue(0.5) self.animation.start() def leaveEvent(self, event): self.animation.setStartValue(self.windowOpacity()) self.animation.setEndValue(1.0) self.animation.start() app = QApplication([]) window = QWidget() layout = QVBoxLayout() button = CustomButton() layout.addWidget(button) window.setLayout(layout) window.show() app.exec_() 
    • Description: Changes the icon opacity with fade effect on hover using QPropertyAnimation.
  10. "PyQt QPushButton change icon with color effect"

    • Code:
      from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget from PyQt5.QtGui import QColor, QPalette from PyQt5.QtCore import Qt class CustomButton(QPushButton): def __init__(self, parent=None): super().__init__(parent) self.setIcon(QIcon('normal_icon.png')) self.setStyleSheet("border: 1px solid black;") self.color_effect = QGraphicsColorizeEffect() self.setGraphicsEffect(self.color_effect) def enterEvent(self, event): self.color_effect.setColor(QColor(0, 0, 255)) # Blue color self.color_effect.setStrength(0.5) def leaveEvent(self, event): self.color_effect.setColor(QColor(255, 255, 255)) # White color self.color_effect.setStrength(1.0) app = QApplication([]) window = QWidget() layout = QVBoxLayout() button = CustomButton() layout.addWidget(button) window.setLayout(layout) window.show() app.exec_() 
    • Description: Changes the icon color with a colorize effect on hover using QGraphicsColorizeEffect.

More Tags

testing uicontrol jwe python-importlib ansible-handlers hostname amazon-elb http-redirect matrix-multiplication pandas

More Programming Questions

More Math Calculators

More Internet Calculators

More Everyday Utility Calculators

More Electronics Circuits Calculators