How To Print More Than One Items In QListWidget If More Than One Items Selected
I have QListWidget and there are strings there, when I select a string, I wanted to display the index number and text of that. But the problem is, if I select more than 1 items, it
Solution 1:
I solved it with this
def but(self):
x = self.listwidget.selectedItems()
for y in x:
print (y.text())
Solution 2:
You need to use QListWidget's selectedItems()
function, which returns a list. currentRow()
only returns a single integer, and is intended to be used only in single-selection instances.
Once you've got the list of QListWidgetItems, you can use the text()
function on each item to retreive the text.
Getting the index is slightly more complicated, you'll have to get a QModelIndex
object from your original QListWidgetItem
using the QListWidget.indexFromItem()
and then use the QModelIndex.row()
function.
Source: http://pyqt.sourceforge.net/Docs/PyQt4/qlistwidget.html#selectedItems
Note: You specified PyQt5 in your tags, but the API of QListWidget remains the same in this case; see the C++ API docs if you want to make sure.
Post a Comment for "How To Print More Than One Items In QListWidget If More Than One Items Selected"