views:

25

answers:

1

Hi,

In my code (python2.6, PyQt4) I do something like this:

def myRun():
    doStuff
thread = QtCore.QThread()
thread.run = myRun
thread.start()

On my gentoo machine, this works perfectly. On a ubunut (9.10, Karmic Koala) it does not work, it says: Type Error: myRun() takes no arguments (1 given)

Did something change in QT? How can I make this work on both machines?

Thanks! Nathan

+1  A: 

I'm not sure how that ever worked; you're supposed to subclass QThread and override the run() method. The "takes no arguments" error is because the QT runtime is trying to pass "self" as the first argument of a class method. The following is closer to what you need:

def myThread(QtCore.QThread):
    def run(self):
        pass

thread = myThread()
thread.start()

UPDATED: Matching the original a bit more.

def myRun():
    doStuff

thread = QtCore.QThread()
thread.run = lambda self: myRun()
thread.start()
sunetos
Well, with a dynamic language like python, the way I did should work, should it not?The problem is, without the self parameter it does not work on the ubuntu machine and with it, it does not work on the gentoo machine.
Nathan
If you want to do it dynamically like your example, you still need your function to accept the self parameter. I'll update it with an example.
sunetos
I am sorry, if I am not clear. The example with the self parameter works on my ubuntu, _ but_ does not work on my gentoo.The example without the self parameter works on my gentoo, but does not work on my ubuntu.
Nathan
Did you try my first example, which shows how the docs say to do it?
sunetos
Yes, that one works. I find it more tedious, but you are right, it solves the problem!
Nathan