threading, qt, and the freakout

B

Ben Floyd

Hey everyone,

Why doesn't this work? The code speaks much more clearly than I do,
so i shortened it and pasted it below. Running this
and clicking on 'Break Me' will... freak out the window...

But... only when the pushbutton_obj.setText("Updated") line
is in the thread. If I comment out the threading code
and uncomment the line below "#WORKS", the button text
updates fine. But in the thread it breaks...

I feel like I'm being stupid here, so ANY suggestions are
much appreciated.

Thanks all.
Ben Floyd
(e-mail address removed)


#############CUT#############
from qt import *
import sys
import threading

class Form1(QMainWindow):
def __init__(self,parent = None,name = None,fl = 0):
QMainWindow.__init__(self,parent,name,fl)
self.setCentralWidget(QWidget(self,"qt_central_widget"))
self.pushButton1 = QPushButton(self.centralWidget(),"pushButton1")
self.pushButton1.setText("Break Me")
self.connect(self.pushButton1,SIGNAL("clicked()"),self.doSomething)

def doSomething(self):
a = DoStuff()
a.go(self.pushButton1)

class DoStuff:
def go(self, pushbutton_obj):
def runThread():
#DOESNT WORK
pushbutton_obj.setText("Updated")

go = threading.Thread(target=runThread)
go.start()

#WORKS
#pushbutton_obj.setText("Updated")

if __name__ == "__main__":
app = QApplication(sys.argv)
f = Form1()
f.show()
app.setMainWidget(f)
app.exec_loop()
 
K

Ken Godee

Ben said:
Hey everyone,

Why doesn't this work? The code speaks much more clearly than I do,
so i shortened it and pasted it below. Running this
and clicking on 'Break Me' will... freak out the window...

You can not mix GUI threads and non GUI threads.

Just changing text may seem to work, but
even that will get you eventually.

Here's an article that explains how to do it...

http://www.informit.com/articles/article.asp?p=30708
 
P

Phil Thompson

What versions of Python, PyQt, SIP, Qt?

Qt imposes restrictions on which parts of the API can be called in
different threads - check the Qt documentation.

Python has bugs in its thread implementation. These (the ones that affect
PyQt anyway) are fixed in Python 2.4.

Also see the "Support for Threads" section in the PyQt documentation.

Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top