Figuring out PyQT

Hey all,

This my first post here. Needed some help with the PyQT system in Maya. I used the QT designer to make a .ui file. Used the pyuic4.bat file to convert it into a .py (test2.py)file :

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'test2.ui'
#
# Created: Thu Sep 24 12:19:21 2009
#      by: PyQt4 UI code generator 4.4.3
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.verticalLayoutWidget = QtGui.QWidget(Form)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(210, 30, 160, 80))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtGui.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setObjectName("verticalLayout")
        self.pushButton = QtGui.QPushButton(self.verticalLayoutWidget)
        self.pushButton.setObjectName("pushButton")
        self.verticalLayout.addWidget(self.pushButton)
        self.checkBox = QtGui.QCheckBox(self.verticalLayoutWidget)
        self.checkBox.setObjectName("checkBox")
        self.verticalLayout.addWidget(self.checkBox)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        self.pushButton.setText(QtGui.QApplication.translate("Form", "MyButton", None, QtGui.QApplication.UnicodeUTF8))
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
        self.checkBox.setText(QtGui.QApplication.translate("Form", "CheckBox", None, QtGui.QApplication.UnicodeUTF8))

To call this file in Maya i created another file (mytest.py) :

import sys
from PyQt4 import QtCore, QtGui
from test2 import Ui_Form
import pumpThread as pt


dialog=None

class MyDialog(QtGui.QDialog,Ui_Form):
	
	def __init__(self):
		QtGui.QDialog.__init__(self)
		self.setupUi( self )

def mytest():
    global app
    global dialog
    pt.initializePumpThread()
    app=QtGui.qApp
    dialog = MyDialog()
    dialog.show()
    app.connect(app, QtCore.SIGNAL("lastWindowClosed()"),
                            app, QtCore.SLOT("quit()"))

This created the UI in Maya with a button and a checkbox. Now this was with the documentation shipped with Maya. Getting ahead, I needed help to on the syntax/format to say create a sphere in Maya using the button.

Thanks for the help,

Regards,

Vikram

The way I do it is I add an extra slot to the dialog inside the designer and then connect all button’s pressed signal to that slot, once you get the python version replace the slot1 instances with the functions you want called, let me know if you need an example and I’ll post one up

I was looking at documentation for using PyQt4 in Maya 2008. Although useful, I can’t seem to find Qt in any of Maya’s paths… guess that’s something that needs to be installed…?

Yeah, you need to install it from their website, Riverbank Computing | Download

[QUOTE=floyd1510;4559]Hey all,
Now this was with the documentation shipped with Maya.
[/QUOTE]

Where in the Maya docs did you find this Qt info? I have been looking around and can’t seem to locate it.

Also, does anyone have any good links which talk about Qt and Motionbuilder and/or Maya specifically? Since both apps all ready have an application instance running, I’m curious about thislast bit of the code posted above:

app.connect(app, QtCore.SIGNAL("lastWindowClosed()"), app, QtCore.SLOT("quit()"))

Is this line always required in a good piece of UI code to handle window destruction, or will Maya/Motionbuilder handle this for you? Leaving out that line seems to make no difference.

Take for instance, this example in MotionBuilder:

import sys
from PyQt4 import QtGui, QtCore

app = QtGui.qApp

widget = QtGui.QWidget()
widget.resize(250, 150)
widget.setWindowTitle('simple')
widget.show()

#app.connect(app, QtCore.SIGNAL("lastWindowClosed()"), app, QtCore.SLOT("quit()"))

This still runs with that last line commented, and doesn’t crash if you exit MotionBuilder or close out all the windows. Can anyone elaborate on this?

Thanks!
-csa

Found my answer to this part of my question:

[QUOTE=csa3d;4697]Where in the Maya docs did you find this Qt info?[/QUOTE]

C:\Program Files\Autodesk\Maya2009\devkit\other\PyQtScripts

-csa

I am a web developer and looking for for some new ideas about the web 2.0 thats why I am here, but still unable to find the required information but I hope I will find it soon

[QUOTE=lkruel;4570]The way I do it is I add an extra slot to the dialog inside the designer and then connect all button’s pressed signal to that slot, once you get the python version replace the slot1 instances with the functions you want called, let me know if you need an example and I’ll post one up[/QUOTE]

Hi Kruel,

I was hoping if you can help me out with an example on how a slot can be instanced. I recently got the PyQt setup installed to work with Maya 2011. I did a few tests with custom UI’s and also got them to load in Maya. Now Based on your advice, I also created a custom slot and connected the button pressed signal to the new custom slot which I added to the dialog of the UI.

On generation of the .py file, I see the custom slot I created with the connection as n the code below:

from PyQt4 import QtCore, QtGui

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(396, 300)
        self.tabWidget = QtGui.QTabWidget(Dialog)
        self.tabWidget.setGeometry(QtCore.QRect(10, 10, 371, 241))
        self.tabWidget.setObjectName("tabWidget")
        self.tab = QtGui.QWidget()
        self.tab.setObjectName("tab")
        self.label = QtGui.QLabel(self.tab)
        self.label.setGeometry(QtCore.QRect(20, 50, 141, 16))
        self.label.setObjectName("label")
        self.textEdit = QtGui.QTextEdit(self.tab)
        self.textEdit.setGeometry(QtCore.QRect(210, 50, 141, 21))
        self.textEdit.setObjectName("textEdit")
        self.pushButton = QtGui.QPushButton(self.tab)
        self.pushButton.setGeometry(QtCore.QRect(250, 160, 75, 24))
        self.pushButton.setObjectName("pushButton")
        self.tabWidget.addTab(self.tab, "")
        self.tab_2 = QtGui.QWidget()
        self.tab_2.setObjectName("tab_2")
        self.label_2 = QtGui.QLabel(self.tab_2)
        self.label_2.setGeometry(QtCore.QRect(30, 30, 151, 21))
        self.label_2.setObjectName("label_2")
        self.DampValue = QtGui.QSlider(self.tab_2)
        self.DampValue.setGeometry(QtCore.QRect(100, 120, 160, 16))
        self.DampValue.setOrientation(QtCore.Qt.Horizontal)
        self.DampValue.setObjectName("DampValue")
        self.checkBox = QtGui.QCheckBox(self.tab_2)
        self.checkBox.setGeometry(QtCore.QRect(100, 90, 151, 19))
        self.checkBox.setObjectName("checkBox")
        self.radioButton = QtGui.QRadioButton(self.tab_2)
        self.radioButton.setGeometry(QtCore.QRect(100, 160, 82, 19))
        self.radioButton.setObjectName("radioButton")
        self.radioButton_2 = QtGui.QRadioButton(self.tab_2)
        self.radioButton_2.setGeometry(QtCore.QRect(210, 160, 82, 19))
        self.radioButton_2.setObjectName("radioButton_2")
        self.label_3 = QtGui.QLabel(self.tab_2)
        self.label_3.setGeometry(QtCore.QRect(30, 40, 181, 31))
        self.label_3.setObjectName("label_3")
        self.tabWidget.addTab(self.tab_2, "")
        self.progressBar = QtGui.QProgressBar(Dialog)
        self.progressBar.setGeometry(QtCore.QRect(10, 270, 131, 23))
        self.progressBar.setProperty("value", QtCore.QVariant(24))
        self.progressBar.setObjectName("progressBar")

        self.retranslateUi(Dialog)
        self.tabWidget.setCurrentIndex(0)
        [B]QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("pressed()"), Dialog.NickCustomSlot)[/B]
        QtCore.QMetaObject.connectSlotsByName(Dialog)

Could you suggest how this custom slot can be instanced so that I can use the UI button to drive a custom command/function in Maya…?

Greatly appreciate any help.

Cheers,

Nikhil.

Ok I got that figured out… we need to create a dynamic property within QT and link that property to call a custom function/procedure within Maya. Makes things really fast & intuitive now with Qt nicely integrated within Maya!

Cheers!

Nikhil.

sorry, just saw this,

yeah all you need to do is replace the slot name with your function

QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(“pressed()”), myfunction)

hi all.

I would like some help with pyqt UI to work with Maya.

The code below gives this error.

Error: IndentationError: unindent does not match any outer indentation level

What I want is to get btn1 and btn2 to work with


import sys
from PyQt4 import QtCore, QtGui
from zGUI.testUI import Ui_Dialog
import maya.cmds as cmds
#import pumpThread as pt

dialog=None

class MyDialog(QtGui.QDialog,Ui_Dialog):

	def __init__(self):
		QtGui.QDialog.__init__(self)
		self.setupUi( self )
		#self.connect(self.btn1, QtCore.SIGNAL("clicked()"), self.HI)
                QtCore.QObject.connect(self.btn1, QtCore.SIGNAL("pressed()"), createCube())
                QtCore.QObject.connect(self.btn2, QtCore.SIGNAL("pressed()"), createSphere())
	#def HI(self):
            #cmds.polySphere()
        def createCube():
            cmds.polyCube()
        def createSphere():
            cmds.polySphere()

def myMakeStuff():
    global app
    global dialog
    app=QtGui.qApp
    dialog = MyDialog()
    dialog.show()
	# remove line below to avoid pumpThread
    #app.connect(app, QtCore.SIGNAL("lastWindowClosed()"), app, QtCore.SLOT("quit()"))

myMakeStuff()

It appears that you’re using different tab widths.

Hence the indentation error: # Error: IndentationError: unindent does not match any outer indentation level #

Try this.


import sys
from PyQt4 import QtCore, QtGui
from zGUI.testUI import Ui_Dialog
import maya.cmds as cmds
#import pumpThread as pt

dialog=None

class MyDialog(QtGui.QDialog,Ui_Dialog):

    def __init__(self):
        QtGui.QDialog.__init__(self)
        self.setupUi( self )
        #self.connect(self.btn1, QtCore.SIGNAL("clicked()"), self.HI)
        QtCore.QObject.connect(self.btn1, QtCore.SIGNAL("pressed()"), createCube())
        QtCore.QObject.connect(self.btn2, QtCore.SIGNAL("pressed()"), createSphere())

#def HI(self):
    #cmds.polySphere()
def createCube():
    cmds.polyCube()
def createSphere():
    cmds.polySphere()

def myMakeStuff():
    global app
    global dialog
    app=QtGui.qApp
    dialog = MyDialog()
    dialog.show()
    # remove line below to avoid pumpThread
    #app.connect(app, QtCore.SIGNAL("lastWindowClosed()"), app, QtCore.SLOT("quit()"))

myMakeStuff()


I don’t have PyQt setup on my current machine so I can’t really check it.