get value from multiple selections QListWidget

1,879 views
Skip to first unread message

Panupat Chongstitwattana

unread,
Oct 25, 2012, 7:08:46 AM10/25/12
to python_in...@googlegroups.com
I'm wondering if there's a cleaner way to do this than the way I'm doing?

Let's say I have "myListWidget" which is multiple-selection enabled QListWidget, I would then do this to print out the values of selected rows.

for item in list(myListWidget.selectedItems())
    print myListWidget.item(myListWidget.row(item)).text()

I feel like I'm going through a lot of things to get to that text(). Is there a cleaner or shorter way to do what I'm doing?

Thanks.

Ali Khanbabaei

unread,
Oct 25, 2012, 7:34:42 AM10/25/12
to python_in...@googlegroups.com
#you can use pymel
import pymel.core as pm
pm.uitypes.TextScrollList('listWidgetName').getSelectItem()

Panupat Chongstitwattana

unread,
Oct 25, 2012, 7:57:02 AM10/25/12
to python_in...@googlegroups.com
Ah that looks pretty neat. Thanks Ali, I should really learn pymel.

Another question about PyQt .... when re-running the script, how can I close the previous UI if it already exists?

I tried

mc.windows(QtObjectName, exists=True)

this returns True if the Qt window is opened. However,

mc.deleteUI(QtObjectName)

this one doesn't do anything. Is there different command I should use?

Thanks :)


Ali Khanbabaei

unread,
Oct 25, 2012, 9:02:04 AM10/25/12
to python_in...@googlegroups.com
it is true.it should delete window. 
send a part of your code that i see 

Justin Israel

unread,
Oct 25, 2012, 12:34:04 PM10/25/12
to python_in...@googlegroups.com
Not sure why you would want to use pymel operations on the UI elements when you already have the more robust PyQt interface to them.
The QListWidgetItem's being returned have a text() method already, so you won't need to look them up twice:


for item in myListWidget.selectedItems():
    print item.text()

or, you can do it in a list comprehension:

textValues = [item.text() for item in myListWidget.selectedItems()]

There is more than one way to find an existing window...
To find a window that already exists, by name, you can use OpenMayaUI.MQtUtil.findWindow()

from PyQt4 import QtGui

win = QtGui.QDialog()
win.setObjectName("fooWindow")

import maya.OpenMayaUI as mui
if mui.MQtUtil.findWindow("fooWindow"):
    print "Exists!"

This approach will return an actual QWidget swig pointer. 
If you just want to know if it exists, by name, you can use the cmds.window()

cmds.window("fooWindow", exists=True)

Judah Baron

unread,
Nov 7, 2012, 1:01:05 PM11/7/12
to python_inside_maya
Seconded there. If you are writing raw Qt UI I'd stick with it without exiting that "scope". As for the complexity of your sample code, that seems completely reasonable to me.

You could also subclass QListWidgetItem (not sure if that's the actual class name...) and handle a lot of the customization there. For instance, you could override its __str__ method to return the text attribute, allowing you to print these items directly, or handle them as strings under certain circumstances.

ex:
print my_itemtype_instance

Also, I think this *should* work:
print " ".join(my_itemtype_instance_array)

At any rate, the examples are not the best, and I'd probably never do that:), but I think you're on the right track. Writing UI code is complex and relatively voluminous when compared with other types of coding. For every line of UI code you don't write, you are giving up some amount of control, and buying into someone else's idea of what is acceptable in terms of performance and behavior. If you handle this on your end, you will end up with more code, but you will be able to form it as you need in your particular use case.

-Judah
Reply all
Reply to author
Forward
0 new messages