I'm developing an application with PySide. I'm doing the unit tests before writing any code in the application. I need to select an item inside QTreeWidget so I can use QTreeWidget.currentItem
to retrieve it and do some stuff with it in order to pass the unit test, I know that I can click widgets using QTest.mouseClick
however, I'm not sure how to click a item inside a QTreeWidget.
Asked
Active
Viewed 1,116 times
1

shackra
- 277
- 3
- 16
- 56
2 Answers
2
What you want is to click from the QModelIndex
and use the viewport()
method:
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())

Ronan Paixão
- 8,297
- 1
- 31
- 27
1
I was able to achieve what I wanted without using QTest.mouseClick
.
Here is the code:
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")
# ...

shackra
- 277
- 3
- 16
- 56
-
You actually use `Qtest.mouseClick`, so why do you say you don't use it? – NoDataDumpNoContribution Jun 16 '14 at 11:12
-
Because clicking on the `QTreeWidget` not necessarily means that I clicked the `QtreeWidgetItem` I want? Or at least that's what I thought... – shackra Jun 16 '14 at 17:01