尝试检索 QListView 元素的文本内容时返回空字符串

Empty string returned while trying to retrieve the text contents of an element of QListView

我正在尝试自动化基于 PyQt 的应用程序,该应用程序使用 Squish 以图标模式使用 QListView 为了 select 视图中的特定项目,我需要首先识别项目的文本。我正在使用下面的代码来做同样的事情

targetList = waitForObject("{name='someListView' type='QListView'}")

object.children(targetList)[11].model().data(object.children(targetList)[11]).toString()

这里object.children(targetList)[11]的类型是QModelIndex

但是上面的代码总是returns一个空字符串。

是否有任何其他方法来检索文本数据

我宁愿只使用 QListView API。因此,如果 targetList 对象是有效的,即它是由 waitForObject 函数找到的,我会写:

targetList = waitForObject("{name='someListView' type='QListView'}")

model = targetList.model()
col = targetList.modelColumn
idx = model.index(11, col)
itemString = idx.data().toString()

这是一个例子:

def main():
    # Use itemviews example included in Squish:
    startApplication("itemviews")

    # Configure QListView to use IconMode as
    # mentioned by original poster:
    obj = waitForObject("{occurrence='2' type='QListView' unnamed='1' visible='1'}")
    obj.setViewMode(QListView.IconMode)

    # Get desired item via object.children();
    # this yields a "wrapped" QModelIndex which
    # features a property "text" which contains
    # the desired text:
    it = object.children(obj)[4]

    test.log("item text: %s" % it.text)