[PySide] Connecting multiple Signals/Slots help

max
coding
python
maya
qt

#1

hey!
I’m new to GUI programming and I’m using PySide (as 3ds Max and Maya use it). I’m having a little difficulty trying to get my head around connecting slots and signals. I can connect up single signals no problem but with more complicated signals, I start to struggle a little bit and the documentation (Signals and Slots in PySide) is a little confusing.

I have separated the GUI from the core code and I’m trying to keep it that way.

Here is the GUI code in question (I have made it as simple as possible):

import sys

from PySide import QtGui

import otherlocalModule


class GUIWindow(QtGui.QWidget):
    def __init__(self):
        super(GUIWindow, self).__init__()

        self.setWindowTitle("Example Window")
        self.setGeometry(300, 250, 270, 200)

        self.example_button = QtGui.QPushButton("Export Selection")
        self.freeze_checkbox = QtGui.QCheckBox("Freeze Transform")
        self.del_history_checkbox = QtGui.QCheckBox("Delete History")

        self.example_button.clicked.connect(self.export_selection)
        self.freeze_checkbox.isChecked.connect(self.export_selection)
        self.del_history_checkbox.isChecked.connect(self.export_selection)

        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.example_button)
        layout.addWidget(self.freeze_checkbox)
        layout.addWidget(self.del_history_checkbox)
        self.setLayout(layout)

        self.show()

    def export_selection(self):
        otherlocalModule.export_selection(self.freeze_checkbox.isChecked(), self.del_history_checkbox.isChecked())

if __name__ == '__main__':
    try:
        app = QtGui.QApplication(sys.argv)
        app_window = GUIWindow()
        app.exec_()
        sys.exit(0)
    except SystemExit:
        pass

# otherLocalModule
# half-pseudo code

def export_selection(freeze_transform, delete_history):
    for mesh in get_current_selection():
        if freeze_transform:
            FreezeTransforms(mesh)

        if delete_history:
            DeleteHistory(mesh)

       exportSelection(mesh)

I’m trying to allow the user to customise their export process like Freezing Transforms or Deleting Histories and this connecting of signals/slots is currently what has stumped me so far.

Thanks!
Andrew


#2

Hi, I believe your code will work once you remove the following two lines…

    self.freeze_checkbox.isChecked.connect(self.export_selection)
    self.del_history_checkbox.isChecked.connect(self.export_selection)

This is because you dont want to export when those values are changed, you only want to export when the button is pushed. Equally, you have already (correctly) hooked up the button, and within the export_selection method you’re querying the relevent ui options.


#3

Hi Mike

Thanks for the reponse!
It does connect,but I now have a strange TypeError issue where if I add the directory argument to otherLocalModule and its corresponding function call in my GUI.

Not sure if this is related as I have reloaded both modules, made sure .pyc files have been cleaned so i’m not sure what is going on here.


#4

If you want to pass arguments to a function you are connecting with .connect() you need to use functools.partial or lambda. You want to bind the function call, not get the functions return values.

do this:
button.clicked.connect(functools.partial(my_func, argument))

not this:
button.clicked.connect(my_func(argument))


#5

Ah this is handy to know - thanks :slight_smile:
I’ve adjusted my code to include this :thumbsup: