在 QListWidget 中获取选定的行 [英] Getting selected rows in QListWidget

查看:1073
本文介绍了在 QListWidget 中获取选定的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Qlistwidget,我可以在其中选择多个项目.我可以在 listwidget 中获取包含所有选定项目的列表,但找不到获取相应行列表的方法.要获取列表小部件中所选项目的列表,我使用了以下代码:

I have a Qlistwidget in which I can select multiple items. I can get a list with all the selected items in the listwidget but can not find a way to get a list of the corresponding rows. To get a list of the selected items in the listwidget I used the following code:

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

要检索我正在寻找的行,例如:

To retrieve the rows I am looking for something like:

a = self.listWidget.selectedIndexes()
print a

但这行不通.我也试过一些代码,导致这样的输出,这不是很有用:

But this does not work. I have also tried some code which resulted in outputs like this, which is not very useful:

<PyQt4.QtGui.QListWidgetItem object at 0x0000000013048B88>
<PyQt4.QtCore.QModelIndex object at 0x0000000014FBA7B8>

推荐答案

奇怪的输出是因为您正在获取 QModelIndexQListWidgetItem 类型的对象.QModelIndex 对象有一个方法来获取它的行,所以你可以使用:

The weird output is because you are getting objects of type QModelIndex or QListWidgetItem. The QModelIndex object has a method to get its row, so you can use:

[x.row() for x in self.listWidget.selectedIndexes()]

获取所有选定索引的列表.

To get a list of all selected indices.

这篇关于在 QListWidget 中获取选定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆