Custom PyQt Signal doesn't work. Why?

python
maya
qt
#1

Hi guys! I’m trying to get working solution with custom PyQt signal in Maya and don’t know why it’s not working.

So I have simple widget that creates set with a simple Label as separate class.
In “SelectionSetWidget” class we change lable name by double clicking on it and entering a new name.
When we Enter a new name this name is being printed in the "changeText " function . Also in this function we emit custom signal with a new label text.

Main class “MyTest” should accept this signal self.W.cusomSignal1.connect(self.changeName) and should make another function (def changeName) work.

“changeName” function changes self.SUPER_GLOBAL parameter which should become equal to the text emmited by custom signal from "changeText " function. But it doesn’t work.
So basically I want to change self.SUPER_GLOBAL parameter by custom signal emitted in "changeText " from another class. How can I do this ? Any Ideas?
Thanks!

from PySide2 import QtWidgets, QtCore, QtGui
import maya.cmds as cmds

class SelectionSetWidget(QtWidgets.QWidget):

    cusomSignal1 = QtCore.Signal(str)
    
    def __init__(self, text = None):
        # Widget's text
        self.widgetText = text

        super(SelectionSetWidget, self).__init__()

        #size
        self.setFixedSize(275, 30)

        #background color
        self.setAutoFillBackground(True)
        self.p = self.palette()
        self.p.setColor(self.backgroundRole(), QtGui.QColor(70, 70, 70))
        self.setPalette(self.p)

        # add layout
        self.mainLayout = QtWidgets.QHBoxLayout()
        self.setLayout(self.mainLayout)

        #add label
        self.wgt_label = QtWidgets.QLabel(self.widgetText)

        self.mainLayout.addWidget(self.wgt_label)

        self.changeLabel = QtWidgets.QLineEdit()          # setting hidden line edit to change set name 
        self.mainLayout.addWidget(self.changeLabel)       # adding line adit to main layout 
        self.changeLabel.hide()                           # hide line edit
        self.changeLabel.textChanged.connect(self.changeText)  # connecting to function to change text
        self.changeLabel.returnPressed.connect(self.setLabel_visibility) # connecting to function line edit visibility
        self.changeLabel.editingFinished.connect(self.changeFocus) # connecting to change focus function
    
    def mouseDoubleClickEvent(self, event): # Event to hide label and show line edit
        self.wgt_label.hide()
        self.changeLabel.show()
    
    def changeText(self, text):             #function to change text in setLabel
        self.wgt_label.setText(text)
        self.widgetText = text
        print(self.widgetText)       
        """ Custom signal emits self.widgetText"""
        self.cusomSignal1.emit(self.widgetText) #!!!!!!!!!!!!!!!  
    
    def setLabel_visibility(self):          # function to change setLabel visibility
        self.wgt_label.show()     
        self.changeLabel.hide()
         
    def changeFocus(self):                  # change focus function to remove focus from changeLabel line edit
        self.changeLabel.clearFocus()

class WList(QtWidgets.QWidget):
    #This widget contains other widgets in there

    def __init__(self):

        super(WList, self).__init__()
        
        self.setMinimumSize(280, 100)

        # add some background color
        self.setAutoFillBackground(True)
        self.p = self.palette()
        self.p.setColor(self.backgroundRole(), QtGui.QColor(100, 100, 100))
        self.setPalette(self.p)

        #add layout
        self.mainLayout = QtWidgets.QVBoxLayout()
        self.mainLayout.setAlignment(QtCore.Qt.AlignTop)
        self.setLayout(self.mainLayout)
    
    def addItem(self, widget = None):
        
        self.mainLayout.addWidget(widget)
       
class MyTest(QtWidgets.QDialog):

    def __init__(self):

        super(MyTest, self).__init__()
        
        self.SUPER_GLOBAL = "Set_01"     #!!!!!!!!!!!!!!!!
       
        self.setMinimumSize(300,100)

        # create main layout
        self.mainLayout = QtWidgets.QVBoxLayout()
        self.setLayout(self.mainLayout)

        #create 2 groups
        wlist_01 = WList()
        self.mainLayout.addWidget(wlist_01)

        """
        Connecting custom signal to function
        """
        self.W = SelectionSetWidget()
        self.W.cusomSignal1.connect(self.changeName)

        # create selection sets
        customW_01 = SelectionSetWidget(text = self.SUPER_GLOBAL)
        
        wlist_01.addItem(customW_01)

    ###!!!!!!!!!!!!!!!!!!!!!!!!
    @QtCore.Slot(str)                  
    def changeName(self, text = None):
        self.SUPER_GLOBAL = text
        print("New Name : " + self.SUPER_GLOBAL)    



def main():

    global dialog 

    if cmds.window("mysuperwgt", q=1, exists = 1): 
        cmds.deleteUI("mysuperwgt")
    try:
        dialog.close()
    except:
        pass


dialog = MyTest()
dialog.show()
#2

It looks like you make two SelectionSetWidgets, but only connect your signal to one of them. Because each new widget has a unique signal, you’ll have to connect each instance of the widget’s signal to the function you want to call. Give this a shot and see if it works:

        """
        Connecting custom signal to function
        """
        self.W = SelectionSetWidget(text=self.SUPER_GLOBAL)
        self.W.cusomSignal1.connect(self.changeName)

        wlist_01.addItem(self.W)
1 Like
#3

Thank you!!! It works.
My fault I havn’t noticed that I create a new instance without connecting new signal.
Thanks!