Martin
Martin

Reputation: 115

How to simulate a QTest.mousePress event on a QListWidgetItem?

Forgive me if the question has already been asked, but I couldn't find the answer anywhere.

I am trying to test a small gui that contains a QListWidget and a QTreeWidget. More specifically, I want to test the drag and drop behavior from one of the QListWidgetItem of the QListWidget to the QTreeWidget. The ui works as intented but the problem comes when testing the drag and drop behavior as I am attempting to use QTest.mousePress() on the item, as this method only takes a QWidget as an input, and not a QListWidgetItem.

import sys
from PySide2 import QtWidgets, QtGui, QtCore


def startup():
    app = QtWidgets.QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())


class MainWindow(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        main_layout = QtWidgets.QHBoxLayout(self)
        self.source = SourceList()

        main_layout.addWidget(self.source)

        self.destination = DestinationTree()
        main_layout.addWidget(self.destination)

        self.add_items(self.source)

    def add_items(self, list_widget):
        items = ['apple', 'banana']

        for item in items:
            list_widget_item = QtWidgets.QListWidgetItem(item)
            list_widget.addItem(list_widget_item)

    def item_count(self):
        number_of_items = 0
        iterator = QtWidgets.QTreeWidgetItemIterator(self)
        while iterator.value():
            number_of_items += 1
            iterator += 1

        return number_of_items


class SourceList(QtWidgets.QListWidget):
    def __init__(self, parent=None):
        super(SourceList, self).__init__(parent)
        self.setViewMode(QtWidgets.QListWidget.IconMode)
        self.setDragEnabled(True)

    def startDrag(self, supportedActions):
        items = self.selectedItems()
        name = items[0].text()
        drag = QtGui.QDrag(self)
        ba = bytearray(name, 'utf-8')
        drag_mime_data = QtCore.QMimeData()
        drag_mime_data.setData('MoveQComponentItem', QtCore.QByteArray(ba))
        drag.setMimeData(drag_mime_data)
        drag.exec_(QtCore.Qt.MoveAction)


class DestinationTree(QtWidgets.QTreeWidget):
    def __init__(self, parent=None):
        super(DestinationTree, self).__init__(parent)
        self.setAcceptDrops(True)

    def dragEnterEvent(self, drag_event):
        mime_data = drag_event.mimeData()
        if mime_data.hasFormat('MoveQComponentItem'):
            drag_event.acceptProposedAction()

    def dragMoveEvent(self, drag_event):
        return

    def dragLeaveEvent(self, drag_event):
        return

    def dropEvent(self, drop_event):
        print('Entering Drop event')
        byte_array = drop_event.mimeData().data('MoveQComponentItem')
        name = byte_array.data().decode('utf8')
        print(name)

        item = QtWidgets.QTreeWidgetItem()
        item.setText(0, name)
        self.addTopLevelItem(item)

    def item_count(self):
        number_of_items = 0
        iterator = QtWidgets.QTreeWidgetItemIterator(self)
        while iterator.value():
            number_of_items += 1
            iterator += 1

        return number_of_items


if __name__ == '__main__':
    startup()

I'd like to test this with something similar to this :

    def test_shouldAddOneWidgetToTheTree_whenDragingFromListItemToTree(self):
        my_q_list_widget_item = main_window.source.item(0)
        tree_widget = main_window.destination

        QtTest.QTest.mousePress(my_q_list_widget_item, QtCore.Qt.LeftButton)
        QtTest.QTest.mouseMove(tree_widget)
        QtTest.QTest.mouseRelease(tree_widget, QtCore.Qt.LeftButton)

        count = tree_widget.item_count()
        assert count == 1

Ideally I'd need a solution that works both on python 2 and 3, also if that helps, I'm using pytest. Any idea would be greatly appreciated :)

Upvotes: 2

Views: 644

Answers (0)

Related Questions