PyQt Model/View not showing in Maya

76 views
Skip to first unread message

likage

unread,
Sep 19, 2016, 6:40:02 PM9/19/16
to Python Programming for Autodesk Maya
Hi all, I have just started learning on pyqt4 model/view and I had thought of trying out a very simple code to populate maya items into a QListView. As I run the following code in an editor (without those maya modules/lines), I am able to see QListView being called and populated with the information.

As soon as I tried running the code in Maya, the session keeps crashing on me unless I comment out the lines on 'app' and 'sys.exit'
But even so, if those lines are commented out, while it does pops up a window, nothing was populated within.

Can someone kindly advise?

# PyQt4 Modules
from PyQt4 import QtGui, QtCore, uic
import sip
import sys
import maya.cmds as cmds
import maya.OpenMayaUI as mui

def getMayaWindow():
    ptr
= mui.MQtUtil.mainWindow()
   
return sip.wrapinstance(long(ptr), QtCore.QObject)

def main():
    dialog
= BasicDialog()
    dialog
.show()
   
   
class BasicDialog(QtGui.QDialog):
   
def __init__(self, parent=getMayaWindow()):
       
super(BasicDialog, self).__init__(parent)
       
#app = QtGui.QApplication(sys.argv)
       
#app.setStyle("cleanLooks")
   
       
# Data
        data
= QtCore.QStringList()    
       
#data << "one" << "two" << "three" << "four" << "five"  
       
        geos
= cmds.ls(type = "mesh")
        sel
= cmds.listRelatives(geos, parent = True)
       
for item in sel:
            data
.append(QtCore.QString(str(item)))
       
       
# Model
        model
= QtGui.QStringListModel(data)
       
        listView
= QtGui.QListView()
        listView
.show()
        listView
.setModel(model)
       
       
#sys.exit(app.exec_())
main
()


Justin Israel

unread,
Sep 19, 2016, 7:17:52 PM9/19/16
to python_in...@googlegroups.com
Creating a QApplication is only something you would want to do for a standalone application. Maya is already a Qt application and already has an instance of one. There can only be one and its a shared global instance.

It would be better to make use of "if main" for this:

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    main()
    app.exec_()

You definitely do not want that stuff in the constructor of your class, because it needs to happen in your main, only once. Not for every instance you create. Same goes for setting the style.

A concept you are missing here in your example is the idea of parent-child relationships between widgets. What you have in your dialog is that the constructor creates a list widget, with no parent, and shows it. This would end up creating two independent widgets.  What you want is to either set the parent for that list widget, or add it to a layout:

self._listWidget = QtGui.QListWidget(self)

And maybe:

layout = QtGui.QVBoxLayout(self)
layout.addWidget(self._listWidget)


Justin


--
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 view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/b37a7724-68d9-4ae8-811f-0d7233b7daf7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

likage

unread,
Sep 19, 2016, 8:20:44 PM9/19/16
to Python Programming for Autodesk Maya
@Justin, thanks for getting back to me.
So, I did a rewrite, however as long as I did not comment out both the 'app = QtGui.QApplication(sys.argv)' and 'app.exec_()', running the code would still cause my Maya to hang... Any ideas?

But still, I would like to have some insights if the code that I have written is the correct way to go?


# PyQt4 Modules
from PyQt4 import QtGui, QtCore
import sys
import maya.cmds as cmds


class BasicDialog(QtGui.QWidget):
   
def __init__(self):
       
QtGui.QWidget.__init__(self)
       
self.initData()
       
self.initUI()
   
   
def initData(self):
        data
= QtCore.QStringList()
       
#data = []

        geos
= cmds.ls(type = "mesh")
        sel
= cmds.listRelatives(geos, parent = True)
       
for item in sel:
            data
.append(QtCore.QString(str(item)))
       
       
# Model

       
self.ui_model = QtGui.QStringListModel(data)
       
   
def initUI(self):
        listView
= QtGui.QListView()
        listView
.setModel(self.ui_model)
       
        layout
= QtGui.QVBoxLayout()
        layout
.addWidget(listView)
       
self.setLayout(layout)
       

if __name__ == "__main__":
   
#app = QtGui.QApplication(sys.argv)
    win
= BasicDialog()
    win
.show()
   
#app.exec_()


Also, another question I have is, is using 'data = []' the same as 'data = QtCore.QStringList()'? In my case, which would be better? Seeing that both seems to be giving me the same results..

Justin Israel

unread,
Sep 19, 2016, 8:30:42 PM9/19/16
to python_in...@googlegroups.com
On Tue, Sep 20, 2016 at 12:20 PM likage <dissid...@gmail.com> wrote:
@Justin, thanks for getting back to me.
So, I did a rewrite, however as long as I did not comment out both the 'app = QtGui.QApplication(sys.argv)' and 'app.exec_()', running the code would still cause my Maya to hang... Any ideas?

if "if __name__" stuff should not be executed when you import into maya. It would only happen when you run the tool standalone. I dont see a reason why maya should hang, if what you are doing in Maya is just creating an instance of your BasicDialog and calling show()
 

But still, I would like to have some insights if the code that I have written is the correct way to go?

Are you working with an older Maya (such as pre 2014?). As of current, Maya ships with PySide. You shouldn't need to create QString or QStringList (if I remember right). They should be able to handle python strings and lists?

 
--
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.

Alok Gandhi

unread,
Sep 19, 2016, 9:28:41 PM9/19/16
to python_in...@googlegroups.com
Instead of commenting out, you can check for existence of an application instance.

def run():
    app = QtGui.QApplication.instance()
    createNew = app is None

    if createNew:
        app = QtGui.QApplication(sys.argv)

    win = BasicDialog()
    win.show()
    if createNew:
        sys.exit(app.exec_())

if __name__ == '__main__':
    run()


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

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1aizs_uhCPvNPeisy%2Bhd191mFs0KY60Vqbonm_uG7urQ%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.



--
Reply all
Reply to author
Forward
0 new messages