On Sat, Jun 25, 2022 at 4:12 PM johancc <[email protected]> wrote:

> Q: How to add the hover state to this button I have (as an image)? I need
> it to have the "hand icon" when hovering and also some sort of overlay like
> a darkening/lighting effect. Is it possible with my current code? I am
> using pyside/qt inside a DCC application, in this case Autodesk Maya 2022.
> Thank you.
>
> from PySide2.QtWidgets import QApplication, QPushButton
> from PySide2.QtGui import QPixmap, QIcon
> from PySide2.QtCore import QSize
>
> def clicked():
>     print("Button clicked!")
>
> window_wid = QtWidgets.QWidget()
> vlayout_wid = QtWidgets.QVBoxLayout()
>
>
> # Set Push Button
> button = QPushButton()
>
> button.setMaximumSize(200,100)
>
> vlayout_wid.addWidget(button)
>
> # Set image in Push Button
> pixmap = QPixmap("my_image.jpg")
> button_icon = QIcon(pixmap)
> button.setIcon(button_icon)
> button.setIconSize(QSize(200,100))
>
> #button.show()
> button.clicked.connect(clicked)
>
> window_wid.setLayout(vlayout_wid)
> window_wid.show()
>
> [image: qt.png]
>

QIcon has an "active" mode state but the widget has to support it and it
seems QPushButton does not use the active state of the QIcon during hover.
You could use a custom stylesheet, but that has the effect of needing to be
fully styled or your button looks weird.

So a pretty easy solution is to just have a custom button class, where you
have full control over what to do when hover starts and stops:

class MyPushButton(QPushButton):

    def __init__(self, *a, **kw):
        super(MyPushButton, self).__init__(*a, **kw)
        self._icon_normal = QIcon(QPixmap("my_image.jpg"))
        self._icon_over = QIcon(QPixmap("my_image_over.jpg"))

    def enterEvent(self, event):
        self.setIcon(self._icon_over)
        return super(MyPushButton, self).enterEvent(event)

    def leaveEvent(self, event):
        self.setIcon(self._icon_normal)
        return super(MyPushButton, self).enterEvent(event)


You can do any other settings you want in addition to changing the icon.

Justin


> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/28a6b738-6028-4504-b057-6c526bc37da9n%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/28a6b738-6028-4504-b057-6c526bc37da9n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0XOa1jdNdBzD9hqc4tpcbd6q3XZZDYq8A5qj3HsEcBvw%40mail.gmail.com.

Reply via email to