简体   繁体   English

使用命名槽从PyQt移到PySide

[英]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. 我要将一些脚本从PyQt移到PySide,并对插槽名称有疑问。 For context, our UI is created with Qt Designer. 对于上下文,我们的UI是使用Qt Designer创建的。 Saved as .ui files. 另存为.ui文件。 Converted to .py files using pyside-uic . 使用pyside-uic转换为.py文件。 These scripts are then used inside of Autodesk Maya. 这些脚本然后在Autodesk Maya内部使用。

The generated .py script connects signals to slots using the command: QtCore.QMetaObject.connectSlotsByName(Dialog) . 生成的.py脚本使用以下命令将信号连接到插槽: QtCore.QMetaObject.connectSlotsByName(Dialog)

This looks for slots named on_<objectname>_<signalname>() such as on_pushButton_clicked() . 这将查找名为on_<objectname>_<signalname>()插槽,例如on_pushButton_clicked()

Previously with PyQt4, we named our slots as follows: 以前使用PyQt4,我们将插槽命名如下:

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

In PySide, this becomes: 在PySide中,这变为:

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

This worked with PyQt, but not with PySide. 这适用于PyQt,但不适用于PySide。 PySide does not recognise the "name" parameter in the slot anymore. PySide无法再识别插槽中的“名称”参数。 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. 解决方法是将testButton_pressed()函数重命名为on_pushButton_clicked() ,但我希望不必对所有脚本都执行此操作。 Is there a better way to get PySide to recognise the slot "name" argument? 是否有更好的方法让PySide识别插槽“名称”参数?

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. PySide文档说支持name参数,但是在信号触发时会生成RuntimeError (使用1.2.1版)。 Without the name argument, the signal works normally. 如果没有name参数,信号将正常工作。

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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