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


On Tue, Sep 20, 2016 at 10:40 AM likage <dissidia....@gmail.com> wrote:

> 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()
>
>
> --
> 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+unsubscr...@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
> <https://groups.google.com/d/msgid/python_inside_maya/b37a7724-68d9-4ae8-811f-0d7233b7daf7%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1yuEpyL-NUu_D4ORjpwSfUH%2BOtjx6Zf9dXnk%3D1WJdnjw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to