[PyQt] get list of selected items in a listWidget

Ulrich Berning ulrich.berning at denviso.de
Tue Mar 27 08:47:54 BST 2012


The shortest way is:

 

print [str(x.text()) for x in self.listWidget.selectedItems()]

 

Ulli

 

-----

 

Von: pyqt-bounces at riverbankcomputing.com
[mailto:pyqt-bounces at riverbankcomputing.com] Im Auftrag von Christos
Parliaros
Gesendet: Montag, 26. März 2012 23:52
An: pyqt at riverbankcomputing.com
Betreff: Re: [PyQt] get list of selected items in a listWidget

 

well, I found my self another way, similar but shorter.

only the printItem() function have been changed from the previous code.

 

        def printItemText(self):
                    items = self.listWidget.selectedItems()
                    x=[]
                    for i in list(items):
                                x.append(str(i.text()))
                    print x 

 

On Mon, Mar 26, 2012 at 9:39 PM, Christos Parliaros <cparliaros at gmail.com>
wrote:

Is there any better, more efficient way to achieve the following:

 

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import ui_list
class Test(QDialog,ui_list.Ui_Dialog):
def __init__(self, parent=None):
super(Test, self).__init__(parent)
self.setupUi(self)
for i in range(10):
item = QListWidgetItem("Item %i" % i)
self.listWidget.addItem(item)
  #self.listWidget.itemClicked.connect(self.printItemText)
self.connect(self.listWidget, SIGNAL("itemSelectionChanged()"),
self.printItemText)
  
def printItemText(self):
items = self.listWidget.selectedItems()
x=[]
for i in range(len(items)):
x.append(str(self.listWidget.selectedItems()[i].text()))
print x  
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
form = Test()
form.show()
app.exec_()

 

I couldnt find any way to get a list with all the selected items as text, so
I had to create this loop.

 

Here is the code from the ui_list.py from qtdesigner:

 

from PyQt4 import QtCore, QtGui
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s
class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(231, 315)
        Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog",
"Dialog", None, QtGui.QApplication.UnicodeUTF8))
        self.listWidget = QtGui.QListWidget(Dialog)
        self.listWidget.setGeometry(QtCore.QRect(10, 10, 211, 291))
 
self.listWidget.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)
        self.listWidget.setObjectName(_fromUtf8("listWidget"))
        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)
    def retranslateUi(self, Dialog):
        pass

 

thanks in advance

-- 
Christos Parliaros
VFX Technical Director and Artist
www.cparliaros.com





 

-- 
Christos Parliaros
VFX Technical Director and Artist
www.cparliaros.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20120327/00c6c9b9/attachment-0001.html>


More information about the PyQt mailing list