| Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date | |
| All threads index page 4 | |
Dear All,
The attached file is a python example code of QThread. It has a dialog. There is a ?Show? button on this dialog. By clicking this button, a QMessageBox will be opened in a new distinct thread and the entire project hangs.
Does anybody have a suggestion for showing a ?QMessageBox? or ?QDialog? in a new distinct thread different from the GUI thread?
Thank you in advance
---------------------------------
Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.#---------------------------------------------------------------------------
# Import the modules
#---------------------------------------------------------------------------
from qt import *
import sys
#===========================================================================
#
# MyThread : the thread class
#
#===========================================================================
class MyThread( QThread ):
#-------------------------------------------------------------------------
# "__init__" : Initialize MyThread class
#-------------------------------------------------------------------------
def __init__( self ):
"""
the initialize of the thread class.
Argument:
None
Returns:
None
"""
QThread.__init__( self )
#---------------------------------------------------------------------------
# mainPart: Dialog main part
#---------------------------------------------------------------------------
def run( self ):
"""
this function is called when the thread start.
Argument:
None
Returns:
None
"""
QMessageBox.warning( None,
"The test message box",
"This is a test message box "
"that is run in the new thread.")
#===========================================================================
#
# MainWin :
#
#===========================================================================
class MainWin( QMainWindow ):
#-------------------------------------------------------------------------
# "__init__" : Initialize MainWin class
#-------------------------------------------------------------------------
def __init__( self ):
"""
the initialize of the MainWin class.
Argument:
None
Returns:
None
"""
QMainWindow.__init__( self )
self.frame = QFrame( self )
self.vBox = QVBoxLayout( self.frame )
self.vBox.setMargin( 5 )
self.vBox.setSpacing( 5 )
self.setCentralWidget( self.frame )
self.drawMainPart( )
#-----------------------------------------------------------------------
# drawMainPart : draw the Main part of the main window.
#-----------------------------------------------------------------------
def drawMainPart( self ):
"""
Draw the Main part.
Argument:
None
Returns:
None
"""
# groupBox1 : label
groupBox1 = QGroupBox( self.frame )
groupBox1.setColumnLayout( 1, Qt.Vertical )
lbl = QLabel( "Click the 'Show' button to "
"show the test message box : ",
groupBox1 )
groupBox1.setFixedHeight( 2*groupBox1.height() )
self.vBox.addWidget( groupBox1, 0 )
# groupBox2 : Buttons
groupBox2 = QGroupBox( self.frame )
groupBox2.setColumnLayout( 0, Qt.Vertical )
hbox = QHBoxLayout( groupBox2.layout() )
cancelBut = QPushButton( u'Cancel', groupBox2 )
showBut = QPushButton( u'Show', groupBox2 )
hbox.addStretch( 1 )
hbox.addWidget( showBut )
hbox.addStretch( 1 )
hbox.addWidget( cancelBut )
hbox.addStretch( 1 )
self.vBox.addWidget( groupBox2, 0 )
self.vBox.setStretchFactor ( groupBox1, 1 )
self.vBox.setStretchFactor ( groupBox2, 0 )
self.connect( showBut, SIGNAL('clicked()'),
self.showTestMessageBox )
self.connect( cancelBut, SIGNAL('clicked()'),
SLOT('close()') )
#---------------------------------------------------------------------------
# showTestMessageBox : Show the test Dialog.
#---------------------------------------------------------------------------
def showTestMessageBox( self ):
"""
This function is called by pressing the "Show"
button and show the test message box.
Arguments:
None
Return:
None
"""
th = MyThread( )
th.start( )
th.wait( )
#===========================================================================
#
# Test
#
#===========================================================================
if __name__ == '__main__':
app = QApplication( sys.argv )
widget = MainWin( )
app.setMainWidget( widget )
widget.show( )
app.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()") )
app.exec_loop( )
Navid Parvini wrote:
> Dear All,
>
> The attached file is a python example code of QThread. It has a dialog. There is a âShowâ button on this dialog. By clicking this button, a QMessageBox will be opened in a new distinct thread and the entire project hangs.
>
>
> Does anybody have a suggestion for showing a âQMessageBoxâ or âQDialogâ in a new distinct thread different from the GUI thread?
>
> Thank you in advance
>
>
>
>
>
>
> ---------------------------------
> Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.
>
Hi Navid,
The safe way to display a dialog with Qt3 is to post a custom event.
Because in python qt.QApp.lock(), qt.QApp.unlock()... don't opperate.
Example:
def postDisplayMessage(self,message) :
event = qt.QCustomEvent(qt.QEvent.User)
event.event_name = "_warning_message"
event._message = message
qt.qApp.postEvent(self,event)
def customEvent(self,event) :
try:
if event.event_name == "__warning_message" :
qt.QMessageBox.warning(None,"The test message
box",vent._message)
except:
import traceback
traceback.print_exc()
begin:vcard
begin:vcard fn;quoted-printable:S=C3=A9bastien Petitdemange n;quoted-printable:Petitdemange;S=C3=A9bastien email;internet:sebastien.petitdemange@xxxxxxx tel;work:1994 x-mozilla-html:TRUE version:2.1 end:vcard
Dear Sebastien,
Thanks a lot for your comment and help.
Would you please help us in order to know where we should add this piece of code.
Thank you in advance
Sébastien Petitdemange <sebastien.petitdemange@xxxxxxx> wrote: Navid Parvini wrote:
> Dear All,
>
> The attached file is a python example code of QThread. It has a dialog. There is a â??Showâ?? button on this dialog. By clicking this button, a QMessageBox will be opened in a new distinct thread and the entire project hangs.
>
>
> Does anybody have a suggestion for showing a â??QMessageBoxâ?? or â??QDialogâ?? in a new distinct thread different from the GUI thread?
>
> Thank you in advance
>
>
>
>
>
>
> ---------------------------------
> Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.
>
Hi Navid,
The safe way to display a dialog with Qt3 is to post a custom event.
Because in python qt.QApp.lock(), qt.QApp.unlock()... don't opperate.
Example:
def postDisplayMessage(self,message) :
event = qt.QCustomEvent(qt.QEvent.User)
event.event_name = "_warning_message"
event._message = message
qt.qApp.postEvent(self,event)
def customEvent(self,event) :
try:
if event.event_name == "__warning_message" :
qt.QMessageBox.warning(None,"The test message
box",vent._message)
except:
import traceback
traceback.print_exc()
begin:vcard
fn;quoted-printable:S=C3=A9bastien Petitdemange
n;quoted-printable:Petitdemange;S=C3=A9bastien
email;internet:sebastien.petitdemange@xxxxxxx
tel;work:1994
x-mozilla-html:TRUE
version:2.1
end:vcard
---------------------------------
Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.
Navid Parvini wrote:
> Dear Sebastien,
>
> Thanks a lot for your comment and help.
> Would you please help us in order to know where we should add this piece of code.
>
> Thank you in advance
>
> Sébastien Petitdemange <sebastien.petitdemange@xxxxxxx> wrote: Navid Parvini wrote:
>> Dear All,
>>
>> The attached file is a python example code of QThread. It has a dialog. There is a “Show� button on this dialog. By clicking this button, a QMessageBox will be opened in a new distinct thread and the entire project hangs.
>>
>>
>> Does anybody have a suggestion for showing a “QMessageBox� or “QDialog� in a new distinct thread different from the GUI thread?
>>
>> Thank you in advance
>>
>>
>>
>>
>>
>>
>> ---------------------------------
>> Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.
>>
>
> Hi Navid,
>
> The safe way to display a dialog with Qt3 is to post a custom event.
> Because in python qt.QApp.lock(), qt.QApp.unlock()... don't opperate.
>
> Example:
>
> def postDisplayMessage(self,message) :
> event = qt.QCustomEvent(qt.QEvent.User)
> event.event_name = "_warning_message"
> event._message = message
> qt.qApp.postEvent(self,event)
>
> def customEvent(self,event) :
> try:
> if event.event_name == "__warning_message" :
> qt.QMessageBox.warning(None,"The test message
> box",vent._message)
> except:
> import traceback
> traceback.print_exc()
> begin:vcard
> fn;quoted-printable:S=C3=A9bastien Petitdemange
> n;quoted-printable:Petitdemange;S=C3=A9bastien
> email;internet:sebastien.petitdemange@xxxxxxx
> tel;work:1994
> x-mozilla-html:TRUE
> version:2.1
> end:vcard
>
>
>
>
> ---------------------------------
> Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.
Hi Navid,
The above 2 methodes may be in your MyThread class but instead of
calling directly QMessageBax.warning in the run methode call
postDisplayMessage like this:
class MyThread( QThread ):
........
........
def run(self) :
"do something without calling any qt lib"
"and post a message"
self.postDisplayMessage(self,"Hello that a warning message")
def postDisplayMessage(self,message) :
event = qt.QCustomEvent(qt.QEvent.User)
event.event_name = "_warning_message"
event._message = message
qt.qApp.postEvent(self,event)
def customEvent(self,event) :
try:
if event.event_name == "__warning_message" :
qt.QMessageBox.warning(None,"The test message
box",vent._message)
except:
import traceback
traceback.print_exc()
Bye
begin:vcard
begin:vcard fn;quoted-printable:S=C3=A9bastien Petitdemange n;quoted-printable:Petitdemange;S=C3=A9bastien email;internet:sebastien.petitdemange@xxxxxxx tel;work:1994 x-mozilla-html:TRUE version:2.1 end:vcard
Navid Parvini wrote: >Â The attached file is a python example code of QThread. It has a > dialog. There is a âShowâ button on this dialog. By clicking this > button, a QMessageBox will be opened in a new distinct thread and the > entire project hangs. > > >Â Â Does anybody have a suggestion for showing a âQMessageBoxâ or > âQDialogâ in a new distinct thread different from the GUI thread? You can't do that in Qt. All GUI-related activities must be on the GUI thread only. What you want to do is to communicate from your worker threads to the GUI (main) thread via events, signals and/or semaphores. The QMessageBox, the QDialog and the rest stay in the main thread. -- [ signature omitted ]
Attachment:
signature.asc
Description: This is a digitally signed message part.