PyQt4:如何迭代 QListWidget 中的所有项目

发布于 2024-10-10 14:01:22 字数 321 浏览 0 评论 0原文

目前,我在继承 QtGui.QListWidget 的类中使用以下 while 循环来迭代所有项目:

    i = 0
    while i < self.count():
        item = self.item(i)

        i += 1

我希望我可以使用:

for item in self.items():

但 items() 方法需要一个 QMimeData 对象,我不知道如何构造该对象以返回所有项目项目。有没有比上面的 while 循环更干净的方法?

Currently, I use the following while loop in a class that inherits QtGui.QListWidget to iterate all items:

    i = 0
    while i < self.count():
        item = self.item(i)

        i += 1

I was hoping I could use:

for item in self.items():

but the items() method wants a QMimeData object which I don't know how to construct to return all items. Is there a cleaner approach than my while loop above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

伤痕我心 2024-10-17 14:01:22

我也不知道MIME类型是什么,也找不到方便的方法。但是你可以编写一个像这样的简单方法并完成:

def iterAllItems(self):
    for i in range(self.count()):
        yield self.item(i)

它甚至是懒惰的(生成器)。

I don't know what's it with the MIME type either, and I couldn't find a convenience method either. But you could write a simple method like this and be done:

def iterAllItems(self):
    for i in range(self.count()):
        yield self.item(i)

It's even lazy (a generator).

邮友 2024-10-17 14:01:22

只是添加我的 2 美分,因为我正在寻找这个:

itemsTextList =  [str(listWidget.item(i).text()) for i in range(listWidget.count())]

Just to add my 2 cents, as I was looking for this:

itemsTextList =  [str(listWidget.item(i).text()) for i in range(listWidget.count())]
绿光 2024-10-17 14:01:22

我知道这已经很旧了,但是,我刚刚发现了一个函数 findItems(text, Qt. QListWidget 中的 MatchFlags)。因此,要迭代所有项目:

#listWidget is a QListWidget full of items
all_items = listWidget.findItems('', QtCore.Qt.MatchRegExp)
for item in all_items:
  print item

并对项目执行您需要的任何操作 =)

I know this is old but, I just found out a function findItems(text, Qt.MatchFlags) in QListWidget. So, to iterate all items:

#listWidget is a QListWidget full of items
all_items = listWidget.findItems('', QtCore.Qt.MatchRegExp)
for item in all_items:
  print item

And do whatever you need with the item =)

千笙结 2024-10-17 14:01:22
items = []
for index in xrange(self.listWidget.count()):
     items.append(self.listWidget.item(index))
items = []
for index in xrange(self.listWidget.count()):
     items.append(self.listWidget.item(index))
谈场末日恋爱 2024-10-17 14:01:22

正如其他登陆此处寻求有关 PyQt5 的相同信息的注释一样,这里略有不同。

正如上面针对 PyQt4 的用户 Pythonic 所说,您仍然可以使用以下方法直接索引项目:

item_at_index_n = list_widget.item(n)

但是要获取用于迭代的项目列表,使用空字符串和 MatchRegExp 标志似乎不再起作用。

在 PyQt5 中执行此操作的一种方法是:

all_items = list_widget.findItems('*', PyQt5.Qt.MatchWildcard)

我仍在掌握 PyQt,因此很可能有一种我还没有遇到的更简单/更简单/更优雅的方法。但我希望这有帮助!

Just as a note for others landing here seeking the same information about PyQt5, it's slightly different here.

As user Pythonic stated above for PyQt4, you can still directly index an item using:

item_at_index_n = list_widget.item(n)

But to acquire a list of items for iteration, using an empty string and the MatchRegExp flag doesn't seem to work any more.

One way to do this in PyQt5 is:

all_items = list_widget.findItems('*', PyQt5.Qt.MatchWildcard)

I'm still getting to grips with PyQt, so there may well be an easier / simpler / more elegant way that I simply haven't come across yet. But I hope this helps!

深白境迁sunset 2024-10-17 14:01:22
items = []
for index in range(self.listWidget.count()):
    items.append(self.listWidget.item(index))

如果您希望获得项目的文本

for item in items:
   print(item.text())
items = []
for index in range(self.listWidget.count()):
    items.append(self.listWidget.item(index))

If your wish to get the text of the items

for item in items:
   print(item.text())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文