Change the background color when mouse is over the button
self.button.setStyleSheet(
"QPushButton::hover{"
"background-color: #ffd2cf;"
"border: none;"
"}"
)
You can improve your button further:
self.button.setToolTip("Hell o World")
self.button.mousePressEvent = lambda v: self.button.setIconSize(QSize(25, 25))#incresing iconsize
self.button.mouseReleaseEvent = lambda v: self.button.setIconSize(QSize(20, 20))#resetting to original iconsize
self.button.setStyleSheet(
"QPushButton::hover{"
"background-color: #ffd2cf;"
"border: none;"
"}"
)
self.button.myIcon = QIcon("c:/users/user-name/Pictures/icons/delete-row.png")
self.button.setIcon(self.button.myIcon)
self.button.setIconSize(QSize(20, 20))#setting original icon size
Here's a generic DecorableButton
:
from PySide6.QtWidgets import (QPushButton)
from PySide6.QtGui import (QIcon, QMouseEvent)
from PySide6.QtCore import (Qt, QSize)
class DecButton(QPushButton):
def __init__(self, text: str = None, size: QSize = None, iconPath: str = None,
iconSize: QSize = None, onPressIconSizeIncrease: QSize = None,
onFocusBackgroundColor: str = None, toolTip: str = None, parent=None, color=None):
super().__init__(parent=parent)
##initializing UI
self.initUI(text=text, size=size, iconPath=iconPath,
iconSize=iconSize, onPressIconSizeIncrease=onPressIconSizeIncrease,
onFocusBackgroundColor=onFocusBackgroundColor, toolTip=toolTip, color=color)
pass
def initUI(self, text: str = None, size: QSize = None, iconPath: str = None,
iconSize: QSize = None, onPressIconSizeIncrease: int = None,
onFocusBackgroundColor: str = None, toolTip: str = None, color: str = None):
if text is not None:
self.setText(text)
if size is not None:
self.setFixedSize(size)
if iconPath is not None:
self.buttonIcon = QIcon(iconPath)
self.setIcon(self.buttonIcon)
self.buttonIconSize = iconSize
if iconSize:
self.setIconSize(self.buttonIconSize)
self.onPressIconSizeIncrease = onPressIconSizeIncrease
if onFocusBackgroundColor is not None:
self.setStyleSheet(
"QPushButton::hover{"
f"background-color: {onFocusBackgroundColor};"
"border: none;"
"}"
)
if color is not None:
if onFocusBackgroundColor is None:
self.setStyleSheet(
"QPushButton{"
f"background-color: {color};"
"border: none;"
"}"
)
else:
self.setStyleSheet(
"QPushButton{"
f"background-color: {color};"
"border: none;"
"}"
"QPushButton::hover{"
f"background-color: {onFocusBackgroundColor};"
"border: none;"
"}"
)
self.setToolTip(toolTip)
def mousePressEvent(self, event: QMouseEvent) -> None:
super().mousePressEvent(event)
if self.onPressIconSizeIncrease:
self.setIconSize(self.onPressIconSizeIncrease)
def mouseReleaseEvent(self, event: QMouseEvent) -> None:
super().mouseReleaseEvent(event)
if self.onPressIconSizeIncrease:
self.setIconSize(self.buttonIconSize)
if __name__ == "__main__":
from PySide6.QtWidgets import (QApplication, QWidget, QVBoxLayout, QHBoxLayout)
app = QApplication([])
widget = QWidget()
widget.layout = QHBoxLayout()
widget.button = DecButton(iconPath="c:/users/devpa/Pictures/icons/delete-row.png",
iconSize=QSize(25, 25), onPressIconSizeIncrease=QSize(30, 30),
size=QSize(35, 35), onFocusBackgroundColor='#facdcd', color='#fcf8f7')
widget.layout.addWidget(widget.button)
widget.setLayout(widget.layout)
widget.show()
app.exec()
Output Window:
