【问题标题】:How should a test be written for a delegate for editing?应该如何为代表编写测试以进行编辑?
【发布时间】:2018-01-11 02:50:23
【问题描述】:

我有一个树视图,其中某些字段需要使用自定义委托进行编辑。代表提供QListView 用于值选择。似乎应该使用QAbstractItemView.edit() 方法从测试中启动编辑,但我不知道如何访问创建的编辑器(QListView),因此我可以为测试选择合适的元素。

这是我在切换到 QListVew 之前与 QComboBox 代表一起进行的测试的一部分,但它似乎过于手动。

for index, enumerator in enumerate(group.children):
    editor = delegate.createEditor(
        parent=viewport,
        option=None,
        index=target_index,
    )

    editor.setCurrentIndex(index)
    delegate.setModelData(editor, model, target_index)
    assert enumerator.uuid == item.enumeration_uuid

https://github.com/altendky/st/commit/643c5c30f87fc3bfd8b422687e81f740ec36ef44#diff-06bc81dbbd9f7a12878169d5238e1572R846

【问题讨论】:

    标签: testing model-view-controller qt5 pyqt5


    【解决方案1】:

    这是我想出的。

    https://github.com/altendky/st/blob/089432162b9e8ca67eafdfcc2a4ecc34e8f0e96e/epyqlib/tests/test_attrsmodel.py#L848

    for row, enumerator in enumerate(group.children):
        assert view.edit(
            target_index,
            PyQt5.QtWidgets.QAbstractItemView.AllEditTriggers,
            None,
        )
        editor, = view.findChildren(PyQt5.QtWidgets.QListView)
    
        index = editor.model().index(row, 0, editor.rootIndex())
        editor.setCurrentIndex(index)
        editor.clicked.emit(index)
    
        # this is fun.  if you get weird issues try doing this more times
        for _ in range(3):
            application.processEvents()
    
        assert enumerator.uuid == item.enumeration_uuid
    

    请注意,我将编辑器的点击信号连接到发布回车键事件,因为视图被硬编码以捕获回车事件并完成编辑。

    【讨论】:

      猜你喜欢
      • 2011-11-10
      • 2011-07-06
      • 2019-08-19
      • 2021-10-19
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 2012-08-20
      • 2017-10-08
      相关资源
      最近更新 更多