Search code examples
qtunit-testingpyqtpysideqtestlib

PySide - Click an item inside of a QTreeWidget with QTest


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.


Solution

  • 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")
    
    # ...