473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

PyQt app in seperate thread

I am writing a plugin for a piece of software in python, and I want to
start up a PyQt GUI in the plugin, without stalling the main thread
while the gui is running (later i will want to pass messages between
the main thread and the gui thread).

I'm new to pyqt, so I'm probably doing something very silly, but for
the moment I'm just trying to get the first pyqt tutorial example
running in a seperate thread:

----8<--------

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore

class MyThread( QtCore.QThread ):
def __init__( self ):
QtCore.QThread.__init__( self )

def run( self ):
app = QtGui.QApplication( sys.argv )
hello = QtGui.QPushButton( 'Hello world!' )
hello.resize( 500, 500 )
hello.show()
app.exec_()
QtCore.QThread.terminate( )

mt = MyThread()
mt.start()
print 'Main thread continuing...'
mt.wait()
print 'GUI thread finished.'

----8<--------

The app starts up (with a warning WARNING: QApplication was not created
in the main() thread. ). I get a window, but no button, and clicking on
the close button does nothing and I have to force the program to quit.

There's got to be a way to get this working, right? Can anyone help me
along the right path?

Cheers,

Anders

Nov 22 '06 #1
9 12238
On Wednesday 22 November 2006 12:37 pm, anders wrote:
I am writing a plugin for a piece of software in python, and I want to
start up a PyQt GUI in the plugin, without stalling the main thread
while the gui is running (later i will want to pass messages between
the main thread and the gui thread).

I'm new to pyqt, so I'm probably doing something very silly, but for
the moment I'm just trying to get the first pyqt tutorial example
running in a seperate thread:

----8<--------

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore

class MyThread( QtCore.QThread ):
def __init__( self ):
QtCore.QThread.__init__( self )

def run( self ):
app = QtGui.QApplication( sys.argv )
hello = QtGui.QPushButton( 'Hello world!' )
hello.resize( 500, 500 )
hello.show()
app.exec_()
QtCore.QThread.terminate( )

mt = MyThread()
mt.start()
print 'Main thread continuing...'
mt.wait()
print 'GUI thread finished.'

----8<--------

The app starts up (with a warning WARNING: QApplication was not created
in the main() thread. ). I get a window, but no button, and clicking on
the close button does nothing and I have to force the program to quit.

There's got to be a way to get this working, right? Can anyone help me
along the right path?
Read http://doc.trolltech.com/4.2/threads.html

In particular the bit that says that exec_() must be called from the main
thread and not from a QThread.

Phil
Nov 22 '06 #2

Phil Thompson wrote:
On Wednesday 22 November 2006 12:37 pm, anders wrote:
I am writing a plugin for a piece of software in python, and I want to
start up a PyQt GUI in the plugin, without stalling the main thread
while the gui is running (later i will want to pass messages between
the main thread and the gui thread).

I'm new to pyqt, so I'm probably doing something very silly, but for
the moment I'm just trying to get the first pyqt tutorial example
running in a seperate thread:

----8<--------

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore

class MyThread( QtCore.QThread ):
def __init__( self ):
QtCore.QThread.__init__( self )

def run( self ):
app = QtGui.QApplication( sys.argv )
hello = QtGui.QPushButton( 'Hello world!' )
hello.resize( 500, 500 )
hello.show()
app.exec_()
QtCore.QThread.terminate( )

mt = MyThread()
mt.start()
print 'Main thread continuing...'
mt.wait()
print 'GUI thread finished.'

----8<--------

The app starts up (with a warning WARNING: QApplication was not created
in the main() thread. ). I get a window, but no button, and clicking on
the close button does nothing and I have to force the program to quit.

There's got to be a way to get this working, right? Can anyone help me
along the right path?

Read http://doc.trolltech.com/4.2/threads.html

In particular the bit that says that exec_() must be called from the main
thread and not from a QThread.

Phil

OK so that's all good... so how do I go about doing what I want then?
Can I set up a window in the second thread and start its event loop
without running the event loop in the core app?

Nov 22 '06 #3
On Wednesday 22 November 2006 2:06 pm, anders wrote:
Phil Thompson wrote:
On Wednesday 22 November 2006 12:37 pm, anders wrote:
I am writing a plugin for a piece of software in python, and I want to
start up a PyQt GUI in the plugin, without stalling the main thread
while the gui is running (later i will want to pass messages between
the main thread and the gui thread).
>
I'm new to pyqt, so I'm probably doing something very silly, but for
the moment I'm just trying to get the first pyqt tutorial example
running in a seperate thread:
>
----8<--------
>
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
>
class MyThread( QtCore.QThread ):
def __init__( self ):
QtCore.QThread.__init__( self )
>
def run( self ):
app = QtGui.QApplication( sys.argv )
hello = QtGui.QPushButton( 'Hello world!' )
hello.resize( 500, 500 )
hello.show()
app.exec_()
QtCore.QThread.terminate( )
>
>
>
mt = MyThread()
mt.start()
print 'Main thread continuing...'
mt.wait()
print 'GUI thread finished.'
>
----8<--------
>
The app starts up (with a warning WARNING: QApplication was not created
in the main() thread. ). I get a window, but no button, and clicking on
the close button does nothing and I have to force the program to quit.
>
There's got to be a way to get this working, right? Can anyone help me
along the right path?
Read http://doc.trolltech.com/4.2/threads.html

In particular the bit that says that exec_() must be called from the main
thread and not from a QThread.

Phil

OK so that's all good... so how do I go about doing what I want then?
Can I set up a window in the second thread and start its event loop
without running the event loop in the core app?
No. Read the second sentence of the paragraph I referred to. Also read the
section "QObject Reentrancy".

Phil
Nov 22 '06 #4

Phil Thompson wrote:
On Wednesday 22 November 2006 2:06 pm, anders wrote:
Phil Thompson wrote:
On Wednesday 22 November 2006 12:37 pm, anders wrote:
I am writing a plugin for a piece of software in python, and I want to
start up a PyQt GUI in the plugin, without stalling the main thread
while the gui is running (later i will want to pass messages between
the main thread and the gui thread).

I'm new to pyqt, so I'm probably doing something very silly, but for
the moment I'm just trying to get the first pyqt tutorial example
running in a seperate thread:

----8<--------

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore

class MyThread( QtCore.QThread ):
def __init__( self ):
QtCore.QThread.__init__( self )

def run( self ):
app = QtGui.QApplication( sys.argv )
hello = QtGui.QPushButton( 'Hello world!' )
hello.resize( 500, 500 )
hello.show()
app.exec_()
QtCore.QThread.terminate( )



mt = MyThread()
mt.start()
print 'Main thread continuing...'
mt.wait()
print 'GUI thread finished.'

----8<--------

The app starts up (with a warning WARNING: QApplication was not created
in the main() thread. ). I get a window, but no button, and clicking on
the close button does nothing and I have to force the program to quit.

There's got to be a way to get this working, right? Can anyone help me
along the right path?
>
Read http://doc.trolltech.com/4.2/threads.html
>
In particular the bit that says that exec_() must be called from the main
thread and not from a QThread.
>
Phil
OK so that's all good... so how do I go about doing what I want then?
Can I set up a window in the second thread and start its event loop
without running the event loop in the core app?

No. Read the second sentence of the paragraph I referred to. Also read the
section "QObject Reentrancy".

Phil
OK I see that now. Thanks for pointing that out. So basically, I can't
do what I want at all. That's a bit of a pain. Is there no way of
tricking Qt into thinking I'm running it in the main thread?

A

Nov 22 '06 #5
On 22 Nov 2006 06:43:55 -0800, anders <an*************@gmail.comwrote:
>
Phil Thompson wrote:
On Wednesday 22 November 2006 2:06 pm, anders wrote:
Phil Thompson wrote:
On Wednesday 22 November 2006 12:37 pm, anders wrote:
I am writing a plugin for a piece of software in python, and I want to
start up a PyQt GUI in the plugin, without stalling the main thread
while the gui is running (later i will want to pass messages between
the main thread and the gui thread).
>
I'm new to pyqt, so I'm probably doing something very silly, but for
the moment I'm just trying to get the first pyqt tutorial example
running in a seperate thread:
>
----8<--------
>
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
>
class MyThread( QtCore.QThread ):
def __init__( self ):
QtCore.QThread.__init__( self )
>
def run( self ):
app = QtGui.QApplication( sys.argv )
hello = QtGui.QPushButton( 'Hello world!' )
hello.resize( 500, 500 )
hello.show()
app.exec_()
QtCore.QThread.terminate( )
>
>
>
mt = MyThread()
mt.start()
print 'Main thread continuing...'
mt.wait()
print 'GUI thread finished.'
>
----8<--------
>
The app starts up (with a warning WARNING: QApplication was not created
in the main() thread. ). I get a window, but no button, and clicking on
the close button does nothing and I have to force the program to quit.
>
There's got to be a way to get this working, right? Can anyone help me
along the right path?

Read http://doc.trolltech.com/4.2/threads.html

In particular the bit that says that exec_() must be called from the main
thread and not from a QThread.

Phil
>
OK so that's all good... so how do I go about doing what I want then?
Can I set up a window in the second thread and start its event loop
without running the event loop in the core app?
No. Read the second sentence of the paragraph I referred to. Also read the
section "QObject Reentrancy".

Phil

OK I see that now. Thanks for pointing that out. So basically, I can't
do what I want at all. That's a bit of a pain. Is there no way of
tricking Qt into thinking I'm running it in the main thread?
It's possible with hackery from C++ but I seriously doubt you can do
it from PyQt.

A

--
http://mail.python.org/mailman/listinfo/python-list
Nov 22 '06 #6
OK I see that now. Thanks for pointing that out. So basically, I can't
do what I want at all. That's a bit of a pain. Is there no way of
tricking Qt into thinking I'm running it in the main thread?
Maybe you can either invert the thread-roles - that is, run your "main"
application in a thread, and if needed start the Qt-thing, or you might
consider spawning a process and using pyro. Which will work very neat, done
so myself.

Diez
Nov 22 '06 #7

Diez B. Roggisch wrote:
OK I see that now. Thanks for pointing that out. So basically, I can't
do what I want at all. That's a bit of a pain. Is there no way of
tricking Qt into thinking I'm running it in the main thread?

Maybe you can either invert the thread-roles - that is, run your "main"
application in a thread, and if needed start the Qt-thing, or you might
consider spawning a process and using pyro. Which will work very neat, done
so myself.

Diez
Yeah I was thinking that's going to have to be the way to go... I can't
run the main app in a child thread, so I'll have to spawn the GUI as a
seperate process and communicate with it. Didn't know about pyro
though, thanks for the link. You've used it successfully with PyQt in a
seperate process?

Cheers,

Anders

Nov 22 '06 #8
anders wrote:
>
Diez B. Roggisch wrote:
OK I see that now. Thanks for pointing that out. So basically, I can't
do what I want at all. That's a bit of a pain. Is there no way of
tricking Qt into thinking I'm running it in the main thread?

Maybe you can either invert the thread-roles - that is, run your "main"
application in a thread, and if needed start the Qt-thing, or you might
consider spawning a process and using pyro. Which will work very neat,
done so myself.

Diez

Yeah I was thinking that's going to have to be the way to go... I can't
run the main app in a child thread, so I'll have to spawn the GUI as a
seperate process and communicate with it. Didn't know about pyro
though, thanks for the link. You've used it successfully with PyQt in a
seperate process?
Yup. I used it to spawn a python interpreter in a subprocess, and
communicate some stuff between that and my main GUI app. The idea was to
have a scriptable application, which allowed you could kill the script.

Diez
Nov 22 '06 #9
anders wrote:
OK I see that now. Thanks for pointing that out. So basically, I can't
do what I want at all. That's a bit of a pain. Is there no way of
tricking Qt into thinking I'm running it in the main thread?
I have an app which runs Qt in a separate thread and allows the user to send
it python commands from the main thread. Have a look at this code to see
how it works:

http://svn.gna.org/viewcvs/veusz/bra...30&view=markup

Jeremy

--
Jeremy Sanders
http://www.jeremysanders.net/
Nov 23 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Serge | last post by:
Hi, I have some intensive code that is running on my main thread. I try to show a status update on a 'status form'. The problem that i have is that because it is running in the same thread the...
12
by: Brian Keating EI9FXB | last post by:
Hello all, Wonder what approach is used for this problem. I have a MDIApplication, the MDIClinets are to be in a seperate thread. So I've done something like this, // Create a new Show...
4
by: redneon | last post by:
I've set up a seperate thread and put a timer in it but for some reason it's tick event is never fired. This is what I have... .... Thread timerThread = new Thread(new ThreadStart(startTimer));...
2
by: mwazir | last post by:
Hi all, I have a process thats starts in my application and only terminates when my application is terminated. I want to write the output and the errors of this process to a seperate log file....
7
by: Joe | last post by:
I need a control to always run in a separate thread from the application. I'm not too sure where to begin with this since the control could be dropped on the form at design time. One possible...
1
by: Mike TI | last post by:
April 20, 2006 Hi all I want to invoke a job on a seperate thread and then close it on completion. Can anyone point me out to a working example. Thank you. Mike TI
3
by: Martin Pöpping | last post by:
Hello, I want to change a ProgressBar in a separate Thread. My current code is the following: private void changeProgressBarThread() { changeProgressBar(); }
2
by: Jorgen Bodde | last post by:
Hi all, I want to make a small batch copy tool that scans for certain files, and copies them to a specified directory. Since the files are huge (AVI / DIVX) typical 300 to 700 Mb, I want to...
2
by: binarybrain | last post by:
I have encountered a problem that I am having trouble solving. I have one main UI. From this UI you can bring up a new non-modal Windows form that displays some other data. However, each time a...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.