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

时间:2018-02-01 10:11:03

标签: qt pyqt5 squish

我正在尝试自动化基于PyQt的应用程序,该应用程序使用Squish在图标模式下使用QListView 为了在视图中选择特定项目,我需要首先识别项目的文本。我使用以下代码执行相同的操作

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

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

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

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

是否有其他方法可以检索文本数据

2 个答案:

答案 0 :(得分:2)

我宁愿只使用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()

答案 1 :(得分:1)

以下是一个例子:

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)