简体   繁体   中英

Moving from PyQt to PySide using named slots

I'm moving some of our scripts over from PyQt to PySide and had a question regarding slot names. For context, our UI is created with Qt Designer. Saved as .ui files. Converted to .py files using pyside-uic . These scripts are then used inside of Autodesk Maya.

The generated .py script connects signals to slots using the command: QtCore.QMetaObject.connectSlotsByName(Dialog) .

This looks for slots named on_<objectname>_<signalname>() such as on_pushButton_clicked() .

Previously with PyQt4, we named our slots as follows:

@QtCore.pyqtSlot( name = "on_pushButton_clicked" )
def testButton_pressed( self ):
    print "pressed button"

In PySide, this becomes:

@QtCore.Slot( name = "on_pushButton_clicked" )
def testButton_pressed( self ):
    print "pressed button"

This worked with PyQt, but not with PySide. PySide does not recognise the "name" parameter in the slot anymore. The workaround is to rename testButton_pressed() function to on_pushButton_clicked() , but I would prefer to not have to do this for all my scripts. Is there a better way to get PySide to recognise the slot "name" argument?

This is clearly a bug. The PySide documentation says the name argument is supported, but it produces a RuntimeError (using version 1.2.1) when the signal fires. Without the name argument, the signal works normally.

The mechanism for connecting slots by name is definitely still there, because deliberately using an invalid name, eg:

    @QtCore.Slot(name='on_button_clickedXXX')
    def foo(self):
        print('on_button_clicked')

produces this error:

    QMetaObject::connectSlotsByName: No matching signal for on_button_clickedXXX()

Do like this:

@QtCore.Slot(str) 
def on_pushButton_clicked(self, name = "on_pushButton_clicked"):
    print "pressed button"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM