Singleshot: SLOT with arguments

我的未来我决定 提交于 2019-12-06 14:43:54

问题


I have a strange problem. Here is my code:

def method1(self, arg1, delay=True):
    """This is a method class"""

    def recall(arg1):
        self.method1(arg1, delay=False)
        return

    if delay:
            print "A: ", arg1, type(arg1)
            QtCore.QTimer.singleShot(1, self, QtCore.SLOT(recall(int)), arg1)
            return

    print "B: ", arg1, type(arg1)

So I get this in console:

A:  0 <type 'int'>
B:  <type 'int'> <type 'type'>

In "B" you should get the same than in "A". Anyone knows what's wrong? How can I get the arg1 value instead of its type? This is not making any sense...

PS: I'm trying something like this: http://lists.trolltech.com/qt-interest/2004-08/thread00659-0.html


回答1:


It looks like when you are calling this:

QtCore.QTimer.singleShot(1, self, QtCore.SLOT(recall(int)), arg1)

are you meaning to call this instead?

QtCore.QTimer.singleShot(1, self, QtCore.SLOT(recall(arg1)), arg1)

you are passing int as the first argument of recall, which passes it directly to method1. int is a type object (the type of integers).




回答2:


The parameter passed to the SLOT function must be a string with types as parameters, not a direct call, so you can't connect a slot with parameters to a signal without any parameter.

You can use a lambda function instead:

QtCore.QTimer.singleShot(1, lambda : self.recall(arg1))


来源:https://stackoverflow.com/questions/7489262/singleshot-slot-with-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!