【问题标题】:Is there a Python class similar to QListIterator in QT?QT中是否有类似于QListIterator的Python类?
【发布时间】:2014-11-05 17:58:54
【问题描述】:

我需要类似于 QListIterator 的功能,例如:
next() - 当然在 python 中找到
peekNext()
上一个()

试图寻找类似 Python 的类。

【问题讨论】:

  • 没有。我的意思是一个用列表构造的类,并且有诸如 next() 和 previous() 和 peeknext() 之类的函数,它们让“窥视”到下一个值而不推进迭代器。 qt-project.org/doc/qt-4.8/qlistiterator.html#details
  • 我在问之前用谷歌搜索过,即使在你回答之后,我也能猜到 python 中没有内置类。我应该提出自己的课程或接受其他人的建议。
  • 请展示此类课程的真实用例。
  • 通过“展示一个真实的用例”,我的意思是提供一些示例代码来说明您为什么需要这样一个类。我的直觉是这是一个XY problem,因此实际上不需要这个类。或者换一种说法:PyQt 不包装 QListIterator 可能是有充分理由的。
  • 我需要遍历一个 sqlite 连接表(firefox places.sqlite 在表内)并使用我在我的 firefox 上的相同书签填充一个 QTreeWidget。我需要这个类来使用递归,因为表是这样组织的,在每个书签的文件夹之后,它的子项一个接一个地列出,所以我可以获得下一行并将其添加到当前文件夹 UNLESS 该行属于上层文件夹,因此如果列表迭代器不是当前文件夹的子文件夹,我不希望列表迭代器前进到下一行。我曾经对 firefox 书签备份 json 文件做同样的事情,但现在它被压缩了。

标签: python qt list iterator pyqt


【解决方案1】:

这就是我最终处理它的方式。 一个基于 UserList 类的类,带有 hasNext() 方法,用于检查是否可以向前迭代,另外两个用于在没有索引增量的情况下获取下一个值,并在索引增量的情况下获取下一个值。 没什么特别的。

class BiDirectList(UserList.UserList):
"""
list with peek into next value method with no index increment
"""
def __init__(self, userList):
    super(BiDirectList, self).__init__(userList)
    self.next = 0

def hasNext(self):
    # check if next value exists
    try:
        if self.data[self.next]:  # data represent the list associated with the class object
            return True
    except IndexError:
        return False

def getNext(self):
    # gets next value and advance index in one
    val = self.data[self.next]
    self.next += 1
    return val

def peekNext(self):
    # gets next value if exists with no index increment
    return self.data[self.next]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多