Search code examples
pysidemaya

PySide - How to connect a signal to a widget that was created through a method being called inside another method


I'm fairly new to programming so please be patient with me. I'm trying to create a simple script in Autodesk Maya. I've created a method that sets up two check-boxes side by side (See below)...

    def checkboxLayout(self, name1, text1, name2, text2, parentLayout, initState):

        ##create and add horizontal layout
        layout = QtGui.QHBoxLayout()
        parentLayout.addLayout(layout)

        width = 75

        name1 = QtGui.QCheckBox(text1)
        layout.addWidget(name1)
        name1.setMinimumWidth(width)
        name1.setMaximumWidth(width)
        name1.setChecked(initState)

        name2 = QtGui.QCheckBox(text2)
        layout.addWidget(name2)
        name2.setMinimumWidth(width)
        name2.setMaximumWidth(width)

And later on I've called this method twice in order to save me having to write out the same big block of code twice...

 def createLayout(self):
    ##Layout
    mainLayout = QtGui.QVBoxLayout()

    mainLayout.addWidget(self.title)
    mainLayout.addSpacerItem(self.titleSpacer)
    mainLayout.addWidget(self.oldLabel)
    self.checkboxLayout("selection_CB", "Selection", "name_CB", "Name", mainLayout, True)
    mainLayout.addWidget(self.textbox1)
    mainLayout.addSpacerItem(self.midSpacer)
    mainLayout.addWidget(self.newLabel)
    mainLayout.addWidget(self.textbox2)
    self.checkboxLayout("delHistory_CB", "Delete\nHistory", "freezeTrans_CB", "Freeze\nTransforms", mainLayout, False)

    self.buttonLayout = QtGui.QGridLayout()
    mainLayout.addLayout(self.buttonLayout)
    self.buttonLayout.addWidget(self.cancelButton, 0, 0)
    self.buttonLayout.addWidget(self.okButton, 0, 1)

    self.setLayout(mainLayout)

My problem is that when I try to connect a signal to it, it won't work. All the tutorials I've watched so far have only connected signals to widgets that WERE NOT created by calling a method inside another method (I realize that probably isn't the correct terminology but like I said, I'm new to this :( ) I'll post the code that I've written to try and connect the signal below. My guess was that I had to specify the method that created the check box, but I couldn't get that to work either. So how do I get this signal connected? Also feel free to correct my terminology :) Thanks to anyone who can help :)

def createConnections(self):
        self.selection_CB.toggled.connect(self.checkboxLine1_ChangeState)

Solution

  • Where and how are you setting the variable self.selection_CB?

    In your checkboxLayout function you can include a return for your check box like so:

    `return [name1, name2]`
    

    then simply assign them as you're calling the function and connect the events from there:

    self.check1, self.check2 = self.checkboxLayout("selection_CB", "Selection", "name_CB", "Name", mainLayout, True)
    

    Or if they are always being connected to the same functions, then why not just do the connection straight from checkboxLayout?

    name1.stateChanged.connect(self.checkboxLine1_ChangeState)