我在将PyQt4的代码转换为PyQt5时遇到了两个困难,首先是创建了一个信号字典,其中包含了信号的名称和信号的名称。
MY_SIGNALS = {
'clicked': SIGNAL ('clicked'),
'currentIndexChanged': SIGNAL ('currentIndexChanged')
...
}
第二,在一个函数中,我收到一个小组件和一个信号名,这样的方式是
def connect_actions (received_action, my_control):
...
connect (my_control, MY_SIGNALS [received_action], function_exec)
一个解决方案是检查接收到的字符串和小部件的
if my_control is QtWidgets.QPushButton:
if received_action is "clicked":
my_pushbutton.clicked.connect (function_exec)
但有几十个widget和token,有没有办法像PyQt4一样,把代码改成按名称分配token?
我怎样才能改编这段代码?
一个可能的解决方案是使用 getattr
:
def connect_actions(received_action, my_control):
getattr(my_control, received_action).connect(function_exec)