[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    Re: [PyQt] get list of selected items in a listWidget
From:       "Ulrich Berning" <ulrich.berning () denviso ! de>
Date:       2012-03-27 7:47:54
Message-ID: 000301cd0bed$eb7a7c00$c26f7400$ () berning () denviso ! de
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


The shortest way is:

 

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

 

Ulli

 

-----

 

Von: pyqt-bounces@riverbankcomputing.com
[mailto:pyqt-bounces@riverbankcomputing.com] Im Auftrag von Christos
Parliaros
Gesendet: Montag, 26. März 2012 23:52
An: pyqt@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@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


[Attachment #5 (text/html)]

<html xmlns:v="urn:schemas-microsoft-com:vml" \
xmlns:o="urn:schemas-microsoft-com:office:office" \
xmlns:w="urn:schemas-microsoft-com:office:word" \
xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" \
xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type \
content="text/html; charset=iso-8859-1"><meta name=Generator content="Microsoft Word \
12 (filtered medium)"><style><!-- /* Font Definitions */
@font-face
	{font-family:"Cambria Math";
	panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
	{font-family:Tahoma;
	panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0cm;
	margin-bottom:.0001pt;
	font-size:12.0pt;
	font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
	{mso-style-priority:99;
	color:blue;
	text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{mso-style-priority:99;
	color:purple;
	text-decoration:underline;}
span.apple-tab-span
	{mso-style-name:apple-tab-span;}
span.hoenzb
	{mso-style-name:hoenzb;}
span.E-MailFormatvorlage19
	{mso-style-type:personal-reply;
	font-family:"Calibri","sans-serif";
	color:#1F497D;}
.MsoChpDefault
	{mso-style-type:export-only;}
@page WordSection1
	{size:612.0pt 792.0pt;
	margin:70.85pt 70.85pt 2.0cm 70.85pt;}
div.WordSection1
	{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=DE link=blue vlink=purple><div \
class=WordSection1><p class=MsoNormal><span lang=EN-US \
style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>The \
shortest way is:<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US \
style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p>&nbsp;</o:p></span></p><p \
class=MsoNormal><span lang=EN-US \
style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>print \
[str(x.text()) for x in self.listWidget.selectedItems()]<o:p></o:p></span></p><p \
class=MsoNormal><span lang=EN-US \
style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p>&nbsp;</o:p></span></p><p \
class=MsoNormal><span lang=EN-US \
style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Ulli<o:p></o:p></span></p><p \
class=MsoNormal><span lang=EN-US \
style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p>&nbsp;</o:p></span></p><p \
class=MsoNormal><span lang=EN-US \
style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>-----<o:p></o:p></span></p><p \
class=MsoNormal><span lang=EN-US \
style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p>&nbsp;</o:p></span></p><div \
style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p \
class=MsoNormal><b><span \
style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>Von:</span></b><span \
style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> \
pyqt-bounces@riverbankcomputing.com [mailto:pyqt-bounces@riverbankcomputing.com] \
<b>Im Auftrag von </b>Christos Parliaros<br><b>Gesendet:</b> Montag, 26. März 2012 \
23:52<br><b>An:</b> pyqt@riverbankcomputing.com<br><b>Betreff:</b> Re: [PyQt] get \
list of selected items in a listWidget<o:p></o:p></span></p></div><p \
class=MsoNormal><o:p>&nbsp;</o:p></p><div><p class=MsoNormal><b>well, I found my self \
another way, similar but shorter.</b><o:p></o:p></p></div><div><p \
class=MsoNormal><b>only the printItem() function have been changed from the previous \
code.</b><o:p></o:p></p></div><div><p \
class=MsoNormal><o:p>&nbsp;</o:p></p></div><blockquote \
style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm \
6.0pt;margin-left:4.8pt;margin-right:0cm'><p class=MsoNormal><span \
class=apple-tab-span><span style='color:#009900'>        </span></span><span \
style='color:#009900'>def printItemText(self):<br><span class=apple-tab-span>         \
</span>items = self.listWidget.selectedItems()<br><span class=apple-tab-span>         \
</span>x=[]<br><span class=apple-tab-span>                    </span>for i in \
list(items):<br><span class=apple-tab-span>                                \
</span>x.append(str(i.text()))<br><span class=apple-tab-span>                    \
</span>print x<span class=apple-tab-span> \
</span></span><o:p></o:p></p></blockquote><p \
class=MsoNormal><o:p>&nbsp;</o:p></p><div><p class=MsoNormal>On Mon, Mar 26, 2012 at \
9:39 PM, Christos Parliaros &lt;<a \
href="mailto:cparliaros@gmail.com">cparliaros@gmail.com</a>&gt; \
wrote:<o:p></o:p></p><div><p class=MsoNormal><b>Is there any better, more efficient \
way to achieve the following:</b><o:p></o:p></p></div><div><p \
class=MsoNormal><o:p>&nbsp;</o:p></p></div><blockquote \
style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm \
6.0pt;margin-left:4.8pt;margin-right:0cm'><p class=MsoNormal><span \
style='color:#009900'>from PyQt4.QtCore import *<br>from PyQt4.QtGui import \
*<br>import ui_list<br>class Test(QDialog,ui_list.Ui_Dialog):<br>def __init__(self, \
parent=None):<br>super(Test, self).__init__(parent)<br>self.setupUi(self)<br>for i in \
range(10):<br>item = QListWidgetItem(&quot;Item %i&quot; % \
i)<br>self.listWidget.addItem(item)<br>&nbsp; \
#self.listWidget.itemClicked.connect(self.printItemText)<br>self.connect(self.listWidget, \
SIGNAL(&quot;itemSelectionChanged()&quot;), self.printItemText)<br>&nbsp; <br>def \
printItemText(self):<br>items = self.listWidget.selectedItems()<br>x=[]<br>for i in \
range(len(items)):<br>x.append(str(self.listWidget.selectedItems()[i].text()))<br>print \
x &nbsp;<br>if __name__ == &quot;__main__&quot;:<br>import sys<br>app = \
QApplication(sys.argv)<br>form = \
Test()<br>form.show()<br>app.exec_()</span><o:p></o:p></p></blockquote><div><p \
class=MsoNormal><o:p>&nbsp;</o:p></p></div><div><p class=MsoNormal><b>I couldnt find \
any way to get a list with all the selected items as text, so I had to create this \
loop.</b><o:p></o:p></p></div><div><p \
class=MsoNormal><o:p>&nbsp;</o:p></p></div><div><p class=MsoNormal><b>Here is the \
code from the ui_list.py from qtdesigner:</b><o:p></o:p></p></div><div><p \
class=MsoNormal><o:p>&nbsp;</o:p></p></div><div><blockquote \
style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm \
6.0pt;margin-left:4.8pt;margin-right:0cm'><p class=MsoNormal><span \
style='color:#009900'>from PyQt4 import QtCore, QtGui<br>try:<br>&nbsp; &nbsp; \
_fromUtf8 = QtCore.QString.fromUtf8<br>except AttributeError:<br>&nbsp; &nbsp; \
_fromUtf8 = lambda s: s<br>class Ui_Dialog(object):<br>&nbsp; &nbsp; def \
setupUi(self, Dialog):<br>&nbsp; &nbsp; &nbsp; &nbsp; \
Dialog.setObjectName(_fromUtf8(&quot;Dialog&quot;))<br>&nbsp; &nbsp; &nbsp; &nbsp; \
Dialog.resize(231, 315)<br>&nbsp; &nbsp; &nbsp; &nbsp; \
Dialog.setWindowTitle(QtGui.QApplication.translate(&quot;Dialog&quot;, \
&quot;Dialog&quot;, None, QtGui.QApplication.UnicodeUTF8))<br>&nbsp; &nbsp; &nbsp; \
&nbsp; self.listWidget = QtGui.QListWidget(Dialog)<br>&nbsp; &nbsp; &nbsp; &nbsp; \
self.listWidget.setGeometry(QtCore.QRect(10, 10, 211, 291))<br>&nbsp; &nbsp; &nbsp; \
&nbsp; self.listWidget.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)<br>&nbsp; \
&nbsp; &nbsp; &nbsp; \
self.listWidget.setObjectName(_fromUtf8(&quot;listWidget&quot;))<br>&nbsp; &nbsp; \
&nbsp; &nbsp; self.retranslateUi(Dialog)<br>&nbsp; &nbsp; &nbsp; &nbsp; \
QtCore.QMetaObject.connectSlotsByName(Dialog)<br>&nbsp; &nbsp; def \
retranslateUi(self, Dialog):<br>&nbsp; &nbsp; &nbsp; &nbsp; \
pass</span><o:p></o:p></p></blockquote></div><div><p \
class=MsoNormal><o:p>&nbsp;</o:p></p></div><div><p class=MsoNormal><b>thanks in \
advance</b><o:p></o:p></p></div><p class=MsoNormal><span class=hoenzb><span \
style='color:#888888'>-- </span></span><span style='color:#888888'><br><span \
class=hoenzb>Christos Parliaros</span><br><span class=hoenzb>VFX Technical Director \
and Artist</span><br><span class=hoenzb><a href="http://www.cparliaros.com" \
target="_blank">www.cparliaros.com</a></span></span><o:p></o:p></p></div><p \
class=MsoNormal><br><br clear=all><o:p></o:p></p><div><p \
class=MsoNormal><o:p>&nbsp;</o:p></p></div><p class=MsoNormal>-- <br>Christos \
Parliaros<br>VFX Technical Director and Artist<br><a href="http://www.cparliaros.com" \
target="_blank">www.cparliaros.com</a><o:p></o:p></p></div></body></html>



_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic