[PyQt] Modal dialogs

874 views
Skip to first unread message

Pierre A

unread,
Jun 16, 2009, 3:52:08 PM6/16/09
to python_inside_maya
Hi,

I'm experiencing a strange behavior with modal dialogs.
Let's see the following code snippet: (Im in a method's class context)

def launchDialog(self):
diag = QtGui.QDialog(self)
ret = diag.exec_()
print ret

I can see the dialog during a few miliseconds, then it disapears. The
return value is always 0

Now, another piece of code:
def launchDialog(self):
self.diag = QtGui.QDialog(self)
ret = self.diag.exec_()
print ret

I'm just adding the dialog as a member of the class. The dialog
becomes modal, but exec_() returns immediately.

I have the same problems with the convenience methods of
QtGui.QMessageBox and QtGui.QInputDialog, they are closed immediately
too...


I'm in a particular state in maya. I have two different GUI launched.
They both use pumpthread:
## LAUNCH APP
window = None
app = None
def LaunchApp():
global app
global window
pt.initializePumpThread()
app = QtGui.QApplication(sys.argv)
window = MyWindowClass()
window.show()

If only one GUI is launched, diag.exec_() doesn't disappear. :'( But I
really need to have multiple windows
I'm stuck! I don't know how to solve this. Perhaps I could instanciate
the QtApp in another module which would act as a singleton and share
the QApplication with the different GUI?

Thanks!

Pierre

John Creson

unread,
Jun 16, 2009, 4:10:34 PM6/16/09
to python_in...@googlegroups.com
I think this may be Python cleaning up :)

Once your GUIs start getting more complex, you have to use the global application 

instantiation and global windows variables.  

If you don't use both the global application instantiation and global windows variables, 

then the resultant GUI will appear and disappear very quickly as Python cleans up its 

local variables.  




import sys  

import PyQt4 as qt  

app=None  

win=None  

def windo():  
global app  
global win  
app = qt.QApplication(sys.argv)  
win = qt.QLabel("Hello world!",None)  
win.show()  

windo()  


John Creson

unread,
Jun 16, 2009, 4:22:36 PM6/16/09
to python_in...@googlegroups.com
Also,

you can’t have more than one qt.QApplication(sys.argv) running at once.  


In your second example, you are using pumpThread which works around defining the app in a bad way, but then you re-define it the way that doesn't make maya happy.

Perhaps just comment out the app= line in the second example.

They can all call pumpThread, since this is protected from starting more than once.

Pierre A

unread,
Jun 17, 2009, 5:21:40 AM6/17/09
to python_inside_maya
Thanks for the help!

I've found the mistake. QApplication was instantiated more than one
time because a little "app.quit()" was hidden somewhere... Now I have
two GUI behaving correctly (well, I hope so)

By the way, if pumpThread is not the more elegant workaround, is there
any other ( and smarter :) ) way to do this? Why do we need to call a
refresh thread that calls every 0.01 sec : utils.executeDeferred
( app.processEvents() )?
Can't we simply launch QApplicaton and all the GUI in another thread?
Sorry to bother you with that, I think this issue must have been
already pushed in the list.

Thanks again
Pierre
> > On Tue, Jun 16, 2009 at 3:52 PM, Pierre A <pierre.auge...@heroldfamily.biz

wbate

unread,
Jun 17, 2009, 10:44:41 AM6/17/09
to python_inside_maya
When I open a new GUI, I check to see if there already is a
QApplication and create one only if none exists, same with the
specific main window I want to open:

app = QtGui.QApplication.instance()
if app :
warnings.warn( "QApplication instance already exists!",
category=UserWarning, stacklevel=1 )
else :
app = QtGui.QApplication(sys.argv)

haveWindow = False
windowList = app.topLevelWidgets()
for foundWindow in windowList :
if foundWindow.objectName() == 'myWindowName' :
haveWindow = True
window = foundWindow

if not haveWindow :
window = QtGui.QMainWindow()
window.setObjectName("myWindowName")

So far I have not needed globals or pumpThread...

will.

John Creson

unread,
Jun 17, 2009, 10:59:14 AM6/17/09
to python_in...@googlegroups.com
Some people have had success with the full alternate thread path.  Please share and do what makes the most sense to you.  There may have been an issue with executeInMainThreadWithResult in certain versions making it difficult to get needed maya information back to functions that needed it, but I'm a little fuzzy on that right now. 

Will, are you working in an alternate thread or more modal window like way?  PumpThread came into existence to referee which app is really the mainWindow, since they didn't seem to be playing nicely.

Has anyone had luck on a Mac?
Reply all
Reply to author
Forward
0 new messages