[PyKDE] QFileDialog blocks QThread?
Sok Ann Yap
sokann at gmail.com
Wed Feb 23 16:17:15 GMT 2005
Running the following PyQt script, whenever I click the Open button,
the QThread will be stopped until I close the file dialog. The
equivalent code in C++ has no such problem.
# thread.py
import sys, time, random
from qt import *
class MyThread(QThread):
def __init__(self, receiver):
QThread.__init__(self)
self.receiver = receiver
self.stopped = 0
def run(self):
rand = random.Random()
while not self.stopped:
time.sleep(1)
num = rand.random()
print num
event = QCustomEvent(10000)
event.setData("%f" % (num,))
QThread.postEvent(self.receiver, event)
def stop(self):
self.stopped = 1
class MainWindow(QMainWindow):
def __init__(self, *args):
QMainWindow.__init__(self, *args)
self.toolbar = QToolBar(self)
self.openbutton = QToolButton(self.toolbar)
self.openbutton.setTextLabel("Open")
self.openbutton.setUsesTextLabel(True)
self.textedit = QTextEdit(self)
self.setCentralWidget(self.textedit)
self.thread = MyThread(self)
self.thread.start()
self.connect(self.openbutton, SIGNAL("clicked()"), self.open)
def __del__(self):
self.thread.stop()
if not self.thread.finished():
self.thread.wait()
def customEvent(self,event):
if event.type() == 10000:
self.textedit.append(event.data())
def open(self):
QFileDialog.getOpenFileNames()
app = QApplication(sys.argv)
mw = MainWindow()
app.setMainWidget(mw)
mw.show()
sys.exit(app.exec_loop())
More information about the PyQt
mailing list