pyside 代码行 'combo.activated[str].connect(self.onActivated)' 中括号的含义是什么?

Ale*_*lex 5 python qt pyside

pyside 教程中,我在 QtGui.QComboBox 示例中看到以下行:

combo.activated[str].connect(self.onActivated)  
Run Code Online (Sandbox Code Playgroud)

这个表达[str]在这种情况下意味着什么?示例中和pyside 文档中均未对此进行解释。而且从原始的Qt 文档中也不清楚该表达式的[str]实际含义。

我很清楚索引列表和字典,但在给定的上下文中,似乎对类方法进行了索引。

Jon*_*art 5

这里,activated是一个重载信号,并且[str]str类型重载的索引。

信号重载的原因与 C++ 中函数被视为重载的原因相同:有两个具有相同名称、采用不同参数的函数:

void QComboBox::activated ( int index ) [signal]
void QComboBox::activated ( const QString & text ) [signal]
Run Code Online (Sandbox Code Playgroud)

Python 没有 C++ 所具有的强类型函数重载。因此,PyQt 通过拥有可连接的插槽字典来处理此问题。这本字典的关键是type你的句柄将接受的参数。

wastl 链接到的页面上的这个示例很好地描述了它:

from PyQt4.QtGui import QComboBox

class Bar(QComboBox):

    def connect_activated(self):
        # The PyQt4 documentation will define what the default overload is.
        # In this case it is the overload with the single integer argument.
        self.activated.connect(self.handle_int)

        # For non-default overloads we have to specify which we want to
        # connect.  In this case the one with the single string argument.
        # (Note that we could also explicitly specify the default if we
        # wanted to.)
        self.activated[str].connect(self.handle_string)

    def handle_int(self, index):
        print "activated signal passed integer", index

    def handle_string(self, text):
        print "activated signal passed QString", text
Run Code Online (Sandbox Code Playgroud)

进一步阅读: