Autodesk points out a nice way to delegate the PyQt4 event loop to a
thread. This has the advantage that you don't call app.exec_(), and
thus maya remains responsive while your Qt app is running.

Here's a python module (slightly adapted from Autodesk's included
pumpThread.py) that sets up the event processing:

<pre># MayaQt.py
import maya.cmds as cmds
import maya.utils as utils
import threading
import time
import sys
from PyQt4 import QtCore, QtGui

_thread = None
app = None

def process_qt_events():
        global app
        def processor():
                app.processEvents()

        while True:
                time.sleep(0.01)
                utils.executeDeferred( processor )

def initialize():
        global _thread
        global app
        if _thread == None:
                app = QtGui.QApplication(sys.argv)
                _thread = threading.Thread( target = process_qt_events, args = 
() )
                _thread.start()
</pre>

And here's a simple "Hello World" example:

<pre>from PyQt4 import QtGui
import MayaQt

# this step is required to initialize the global QApplication and
start the event loop
# you can access the QApplication as MayaQt.app
# it's important that you do NOT create your own QApplication instance
-- doing so will crash Maya
MayaQt.initialize()

s = QtGui.QLabel("Hello World")
s.show()
</pre>
-- 
http://groups.google.com/group/python_inside_maya

Reply via email to