Help: maya UI and QListWidget, creating a list .. python help

Hi guys, I have a little problem with a script that im trying to write for a maya UI, i just recently got into try python QT and pyside to create my maya UI.
I am fairly new to classes as well, which is probably what is getting me a little confused.
But im willing to try understand and achieve this , so if you see something that is completely off don’t hesitate to give me a hint(or more of a kick …) into the right direction :P.

The issue im facing is on how to make a list ( I am using the QListWidget which hopefully is the right choice… ) to work, and be able to do these 3 basic functionalities :

1- As i lunch the UI a def will be listing some stuff that i have in the scene and these will be listed in my QlistWidget (at the moment i can put some stuff inside the widget but it displays with no name…nor im able to do much with it…

2- as i press a “refresh” button , the list of objects in the widget, will be refreshed, which probably means, i will have to delete everything that i have inside the list, then call the Def that will add everything back in again.

3- if i double click on any of the items i want that another Def is called (possibly a select -r item inside maya)

4- if i do multiple selection of something in the list , by pressing a button some other def will act upon highlighted items

I think is all of the basic functionality for a “list” gui , i would like to understand better how these type of UI would work .

so far here is my code : (please excuse my commenting…)



import maya.OpenMayaUI as omui
from PySide import QtCore
from PySide import QtGui
from shiboken import wrapInstance

def maya_main_window():
    main_window_ptr = omui.MQtUtil.mainWindow()
    return wrapInstance(long(main_window_ptr), QtGui.QWidget)

class ssacUI(QtGui.QDialog):
    items = ["ssitemss"] #static element fuck works but ??.i dont want it!??
    def __init__(self, parent= maya_main_window()):
        super(ssacUI, self).__init__(parent)
        
        #since im extending the QDialog Class from qt, i will need to be passing 
        #all of the QDialog methods 
        
        #title
        self.setWindowTitle("primitives")
        #make it a tool window, so it stays on top of maya 
        self.setWindowFlags(QtCore.Qt.Tool)#using tool insted of window
        #set an attr that will tellQT to delete the window once is closed
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)

    #####need to call the layoutMethod that i just created from the init since is a class to be able to visualize the buttons
        self.create_layout()
        self.create_connections()

        self.buttonlist = []
        

#### creation of UI Layout METHOD
    def create_layout(self):
# try adding a QColumnView ?..
        self.listWidget = QtGui.QListWidget(self)

        #add button
        self.cube_btn = QtGui.QPushButton("Cube")
        #add next buttons then have to add it into the layout few lines down
        self.sphere_btn = QtGui.QPushButton("Sphere")
        self.cone_btn = QtGui.QPushButton("Cone")
        self.cylinder_btn = QtGui.QPushButton("Cylinder")
        
        
        
        #--create the layout vbox#$#$#$
        main_layout= QtGui.QVBoxLayout()
        
        ################ to reduce button margins and the spacing
        main_layout.setContentsMargins(2, 2, 2, 2)
        main_layout.setSpacing(2)
        #main_layout.addStretch() if added before the buttons the buttons will stick to the bottom, 
        
        
##add the stuff here after being added up there #--add the widget QButton to the layout
    #the column view..
        main_layout.addWidget(self.listWidget)
        main_layout.addWidget(self.cube_btn)
        ###add also  the other new buttons here
        main_layout.addWidget(self.sphere_btn)
        main_layout.addWidget(self.cone_btn)
        main_layout.addWidget(self.cylinder_btn)
        
        #if addStretch is after the buttons, the button will stay hooked at the top when resizing the window
        main_layout.addStretch()
        
        
        #add the main layout itself to the primitive ui dialog
        self.setLayout(main_layout)
        
        ################
    #create method to connect buttons to commands , signals and slots??
    def create_connections(self):
        #click signal to each method created below
        self.cube_btn.clicked.connect(self.make_cube)   # 
        self.sphere_btn.clicked.connect(self.make_sphere)
        self.cylinder_btn.clicked.connect(self.make_cylinder)
        self.cone_btn.clicked.connect(self.make_list)
        
        #self.listWidget.addItems(self.items)  # this stuff is commented out cos none of this sh" works...        
        #####self.listWidget.addItems(brogna) #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        #for i in range(3):
            #self.listWidget.addItem(self.buttonlist)

##i create method that will run comands         
    def make_cube(self):
        cmds.polyCube()    
        
    def make_sphere(self):
        print "Sphere"
        #cmds.polySphere()  
        
    def make_cylinder(self):
        print "Cylinder"
        #cmds.polyCylinder()  

    def make_cone(self):
        print "Cone"
        #cmds.polyCone()                  

    def make_list(self):
        mylistbitch = [cmds.ls(sl=True)]
        print mylistbitch
#i was a bit fustrated here, i tried to add the selection onto a list, and every subsequently selected objec in the scene would be added as #well in the list, but then i tried to simplify the whole thing to see if adding into the list would work, now it does (only when i use a list) #but it does not work 100%, since the names are not being displayed , so here i might be missing something.
        #self.buttonlist.append(mylistbitch)
        #addtolist = self.buttonlist
        #print "addtolist e' : ", addtolist
        #for i in mylistbitch:
            #self.listWidget.addItem(addtolist)
        self.listWidget.addItems(mylistbitch)  #clicking the button cylinder now adds selection to the list, although it doesn't display the #correct names#

if __name__== "__main__":
    #i put try except so maya doesnt hang up the windows if opened many times in a row.
    #try close the window if it exists, other wise it will just open it 
    try:
        ui.close()
    except:
        pass
        
    ui = ssacUI()
    ui.show()






Your code pretty much works. I uncommented the following line in “create_connections”, and it worked just fine:

self.listWidget.addItems(self.items)

The one problem I did notice was in the “make_list” function. The first line should be:

mylistbitch = cmds.ls(sl=True)

(You don’t need to wrap it in square brackets. The “ls” command will return a list.)

this is a big topic. there are easy ways to make a crude list gui that works most of the time, but the real trick is writing a gui that can manage itself, such as updating itself when items are renamed or deleted by other actions.

chapter 5 of the book Practical Maya Programming with Python is an excellent resource.

Thanks a lot , with your corrections seems to work! Im also chasing to buy the book, it seems a really good read.

will keep updating here as i go along.

cheers