[PyKDE] Threading QApplications. Possible?
David Boddie
david at boddie.org.uk
Tue Sep 21 13:34:43 BST 2004
On Mon, 20 Sep 2004 20:52:01, Trost Johnson wrote:
> Suppose the minimal program:
>
> def foo():
> app=QApplication(sys.argv)
> button=QPushButton("Hello World", None)
> app.setMainWidget(button)
> button.show()
> app.exec_loop()
>
> And I say
>
> >>> thread.start_new_thread (foo, ())
>
> Then the python console freezes....
I'm not completely surprised by this. :-)
> Is it not possible o thread up multiple QApplications?
I don't think you can create multiple QApplications in this way. Not within
the same application, anyway.
> How is this achieved?
I think that you really need QApplication's event loop to be running in
the main Python thread. This prevents you from running it in a separate
thread, but you can still run other tasks in separate Python threads.
Just be careful when communicating back to the QApplication from these
other threads.
Note that there is a separate problem with running the event loop from
within an interactive Python session: the interpreter doesn't regain
control until after the QApplication finishes. I presume that you are
trying to run the event loop in a separate thread to solve this problem.
I found that it is quite useful to consider approaching the problem from
another angle and implement parts of an interactive prompt within a
Qt-based application. Although the behaviour of the interpreter isn't
quite what you'd expect from a normal interactive Python session, you can
get quite far with this approach.
Take a look at qpython from the following page:
http://www.boddie.org.uk/david/Projects/Python/KDE/
The code isn't very nice to look at, and I think that there's a race
condition involving the readline module, but it lets you use Qt-based
modules from within an interactive Python session.
Good luck!
David
___________________________________________________________
$0 Web Hosting with up to 120MB web space, 1000 MB Transfer
10 Personalized POP and Web E-mail Accounts, and much more.
Signup at www.doteasy.com
More information about the PyQt
mailing list