1

我正在用 PySide 开发一个应用程序。在应用程序中编写任何代码之前,我正在做单元测试。我需要在 QTreeWidget 中选择一个项目,以便我可以使用QTreeWidget.currentItem它来检索它并用它做一些事情以通过单元测试,我知道我可以使用单击小部件,QTest.mouseClick但是我不确定如何单击其中的项目一个 QTreeWidget。

4

2 回答 2

2

您想要的是单击QModelIndex并使用该viewport()方法:

def clickIndex(tree_view, index):
    model = tree_view.model()
    # If you have some filter proxy/filter, don't forget to map
    index = model.mapFromSource(index)
    # Make sure item is visible
    tree_view.scrollTo(index)
    item_rect = tree_view.visualRect(index)
    QTest.mouseClick(tree_view.viewport(), Qt.LeftButton, Qt.NoModifier, item_rect.center())
于 2015-01-13T03:31:28.013 回答
1

我能够在不使用QTest.mouseClick.

这是代码:

from src import ui
from nose.tools import eq_
from PySide.QtCore import Qt
from PySide.QtTest import QTest

if QtGui.qApp is None:
    QtGui.QApplication([])

appui = ui.Ui()

# ...

def test_movedown_treewidget():
    item = appui.tblURLS.topLevelItem(0)
    appui.tblURLS.setCurrentItem(item)
    QTest.mouseClick(appui.pbtMoveDOWN, Qt.LeftButton)
    # After that click, the connected slot was executed
    # and did something with the current selected widget
    item = appui.tblURLS.topLevelItem(0)

    eq_(item.text(2), u"http://www.amazon.com/example2")


def test_moveup_treewidget():
    item = appui.tblURLS.topLevelItem(1)
    appui.tblURLS.setCurrentItem(item)
    QTest.mouseClick(appui.pbtMoveUP, Qt.LeftButton)
    # After that click, the connected slot was executed
    # and did something with the current selected widget
    item = appui.tblURLS.topLevelItem(0)

    eq_(item.text(2), u"http://www.amazon.com/example1")

# ...
于 2014-06-13T22:57:06.663 回答