我正在使用PySide开发应用程序。在编写应用程序中的任何代码之前,我正在进行单元测试。我需要在QTreeWidget中选择一个项目,以便我可以使用QTreeWidget.currentItem
检索它并使用它做一些事情以通过单元测试,我知道我可以使用QTest.mouseClick
点击小部件但是,我和#39;我不确定如何点击QTreeWidget中的项目。
答案 0 :(得分: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())
答案 1 :(得分:0)
我能够在不使用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")
# ...