MAYA 2018 PyQt Changing PushButton Icon when clicked - How?

qt
python
maya

#1

Hi Guys! I’m new to this community and to programming in python at all, so don’t judge me for stupid questions :slight_smile:

I’m trying to create one script for Maya and which will use similar functionality to PS layers visibility with eye icon.
So basically I’m using QPushButton class with icon. And I want to change this eye icon when button is clicked.
I’m using two icons (opened eye and closed eye) and function to connect button.setIcon(), but I can’t find what state or flag should I use to query which icon is used.
Here is my code :

self.eyeicon_1 = QtGui.QIcon(os.path.join(ROOT, “icons”, “vis.svg”))
self.eyeicon_2 = QtGui.QIcon(os.path.join(ROOT, “icons”, “invis.svg”))
self.visButton.setIcon(self.eyeicon_1)
self.visButton.setIconSize(QtCore.QSize(16,16))
self.visButton.setFlat(True)

self.visButton.clicked.connect(self.eyeChanged)

def eyeChanged():
if self.visButton.????() == self.eyeicon_1:
    self.visButton.setIcon(self.eyeicon_2)
elif self.visButton.????() == self.eyeicon_2:
    self.visButton.setIcon(self.eyeicon_1)   

Could you please help me to find a solution?
Thanks!


#2

Hey, are you sure you’re not looking for a checkbox widget then?

As a minimal example

import sys
  
from PySide2 import QtWidgets
from PySide2 import QtCore

app = QtWidgets.QApplication(sys.argv)
checkbox = QtWidgets.QCheckBox()
checkbox.setStyleSheet(
    "QCheckBox::indicator:checked {image: url(.//res//checked.png);}"
    "QCheckBox::indicator:unchecked {image: url(.//res//error.png);}"
)
checkbox.show()
app.exec_()

This is a QCheckBox that uses Stylesheets to set the indicator.

Using your preexisting code, you’re probably looking to check self.visButton.icon() https://srinikom.github.io/pyside-docs/PySide/QtGui/QAbstractButton.html#PySide.QtGui.PySide.QtGui.QAbstractButton.icon


#3

Hi, thank you for your answer.
I’ve tried to use self.visButton.icon() , but somehow it doesn’t work… It should actually, but doesn’t. And I don’t know why.

Probably I’ll try to use checkbox solution.
Thanks!


#4

Hi, I’ve decided to use CSS style sheet and it only works if I write direct path to my images (like C:/projects/… ).
But I don’t know how to use my working root directory {image: url(???//res//checked.png);}

If you could help me how to set up my working root directory, that would be great!
Thanks


#5

So basically I need to understand how Maya’s PySide treats CSS style current directory


#6

My example used relative paths. Single dot should be current dir. Just like relative import in python is done.

The structure for my example was

checkbox/
|main.py
|res/
|-checked.png
|-error.png