Getting the qt designer working with Pyside Maya 2014

2,189 views
Skip to first unread message

floyd1510

unread,
Sep 30, 2013, 8:06:21 AM9/30/13
to python_in...@googlegroups.com
Hello,

Can I get an example of getting a .ui (created from the qt designer) loading with Pyside in Maya 2014. I tried to replace the loadUi function of PyQt but somehow I cannot get the UI window up (Maya does not complain about anything when I execute the code, just no result)

This link : http://stackoverflow.com/questions/14892713/how-do-you-load-ui-files-onto-python-classes-with-pyside asks you to override the default function, which I tried, but still could not manage to load the UI

Thank you for the help,
Vikram.

johnvdz

unread,
Oct 1, 2013, 12:14:43 PM10/1/13
to python_in...@googlegroups.com
have a look at nathans Horne's page. theres a link to a code file he
offers for uic replacement using pysideuic. should be all you need.

http://nathanhorne.com/?p=451

john

Vikram Shingrani

unread,
Sep 30, 2013, 10:56:20 PM9/30/13
to python_in...@googlegroups.com
Thanks for the link John. I did replace Nathans code to work with Pyside, but still cannot get it working. Secondly, I have changed it to use the shiboken module to get the Maya window. Here's what I have : 

import pysideuic
import xml.etree.ElementTree as xml
from cStringIO import StringIO
from PySide import QtGui, QtCore
from shiboken import wrapInstance
import maya.OpenMayaUI as apiUI

def getMayaWindow():
    """
    Get the main Maya window as a QtGui.QMainWindow instance
    @return: QtGui.QMainWindow instance of the top level Maya windows
    """
    ptr = apiUI.MQtUtil.mainWindow()
    if ptr is not None:
        return wrapInstance(long(ptr),QtGui.QWidget)

def loadUiType(uiFile):
        """
        Pyside lacks the "loadUiType" command, so we have to convert the ui file to py code in-memory first
        and then execute it in a special frame to retrieve the form_class.
        """
        parsed = xml.parse(uiFile)
        widget_class = parsed.find('widget').get('class')
        form_class = parsed.find('class').text
    
        with open(uiFile, 'r') as f:
            o = StringIO()
            frame = {}
            
            pysideuic.compileUi(f, o, indent=0)
            pyc = compile(o.getvalue(), '<string>', 'exec')
            exec pyc in frame
            
            #Fetch the base_class and form class based on their type in the xml from designer
            form_class = frame['Ui_%s'%form_class]
            base_class = eval('QtGui.%s'%widget_class)
        return form_class, base_class 

#If you put the .ui file for this example elsewhere, just change this path.
listExample_form, listExample_base = loadUiType('E:/example1.ui')
class ListExample(listExample_form, listExample_base):
def __init__(self, parent=getMayaWindow()):
super(ListExample, self).__init__(parent)
self.setupUi(self)

#The names "addItemBtn" and "removeItemBtn"
#come from the "objectName" attribute in Qt Designer
#the attributes to access them are automatically created
#for us when we call setupUi()
#Designer ensures that the names are unique for us.
self.addItemBtn.clicked.connect(self.addItem)
self.removeItemBtn.clicked.connect(self.removeItem)

def addItem(self):
"""
Add a new item to the end of the listWidget
"""
item = QtGui.QListWidgetItem(self.listWidget)
item.setText('Item #%s!'%self.listWidget.count())

def removeItem(self):
"""
Remove the last item from the listWidget
"""
count = self.listWidget.count()
if count:
self.listWidget.takeItem(count-1)

Any ideas?

Thanks you,
Vikram.


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To post to this group, send email to python_inside_maya@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Justin Israel

unread,
Oct 1, 2013, 1:30:33 AM10/1/13
to python_in...@googlegroups.com
Not sure off hand why it doesn't work, but as a side note, this is kind of the reason why I always preferred to just use pyside-uic / pyuic to convert the ui files. Then there is no converting and parsing going on at runtime. 
You would normally do the same thing anyways (at least I do) with resource files, having to use pyside-rcc to convert from rcc -> .py


To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To post to this group, send email to python_in...@googlegroups.com.

Vikram Shingrani

unread,
Oct 1, 2013, 1:47:37 AM10/1/13
to python_in...@googlegroups.com
Can I get a simple running example please?

Thank you

Justin Israel

unread,
Oct 1, 2013, 2:47:14 AM10/1/13
to python_in...@googlegroups.com
Sure...

#---- Multiple inheritance ---
from PySide import QtCore, QtGui 

# COMMAND:  pyside-uic test.ui -o test_ui.py
from test_ui import Ui_MainWindow

class MyWindow(QtGui.QMainWindow, Ui_MainWindow):
    
    def __init__(self, *args, **kwargs):
        super(MyWindow, self).__init__(*args, **kwargs)
        self.setupUi(self)

if __name__ == "__main__":
    app = QtGui.QApplication([])
    window = MyWindow()
    window.show()
    window.raise_()
    app.exec_()
#-------------------------

Once you generate the ui -> py module, you no longer have to do it dynamically at each run.
This is just one of the available patterns for using a ui file. Some people prefer to not subclass directly from the UI class, and instead make it a member of the class to namespace all of the widgets:

#----Single inheritance ---- 
class MyWindow(QtGui.QMainWindow):
    
    def __init__(self, *args, **kwargs):
        super(MyWindow, self).__init__(*args, **kwargs)

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
#-------------------------


johnvdz

unread,
Oct 1, 2013, 11:37:28 PM10/1/13
to python_in...@googlegroups.com
yeah i have to agree with justin on converting your .ui files using pyside-uic. when i first started using pyside i from pyQt i was just using uic loader for pyqt. most of the tools where simple. but once i changed to pyside i was getting al sorts of crashs and redraw problems. pyside and pyqt are similar but i've found that pyside you need to really roll with it. using the pyside-uic converter and just starting from a .py will save you a whole lot of headaches.

if your using .ui files just because you can save designer files and load them straight of. just use a .bat file or .sh to do the conversion within you tool button while your developing. or run a os.system() cmd.

i just put this in a bat if you have afew to do at the same time or just run the command as is. with os.system()

C:\Python27\Scripts\pyside-uic.exe D:\myUI.ui -o D:\myUI.py
or
os.system("C:\Python27\Scripts\pyside-uic.exe D:\myUI.ui -o D:\myUI.py")

then run your pyside code.

john
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To post to this group, send email to python_in...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To post to this group, send email to python_in...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To post to this group, send email to python_in...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To post to this group, send email to python_in...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Count Zer0

unread,
Oct 2, 2013, 1:18:57 AM10/2/13
to python_in...@googlegroups.com
You need to finally draw your window:

Add something like this:

def run():
	if pymel.core.window( 'myTool_window', q = 1, ex = 1 ):
		pymel.core.deleteUI( 'myTool_window' )
	tool = ListExample()
	tool.show()

run()



-jason
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscribe@googlegroups.com.
To post to this group, send email to python_in...@googlegroups.com.

Vikram Shingrani

unread,
Oct 5, 2013, 2:42:45 AM10/5/13
to python_in...@googlegroups.com
Thanks for the help Jason, John and Justin. Got it running finally :)


To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

To post to this group, send email to python_in...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages