import sys

from PySide2.QtCore import QUrl, QObject, Property, Signal, Slot
from PySide2.QtQml import QQmlApplicationEngine
from PySide2.QtWidgets import QApplication


class TestModel(QObject):

    nameChanged = Signal()
    descriptionChanged = Signal()

    def __init__(self):
        QObject.__init__(self)

    def get_name(self):
        return self._name
    def set_name(self, name):
        self._name = name
        self.nameChanged.emit()
    name = Property(str, get_name, set_name, notify=nameChanged)

    def get_description(self):
        return self._description
    def set_description(self, description):
        self._description = description
        self.descriptionChanged.emit()
    description = Property(str, get_description, set_description, notify=descriptionChanged)


class TestQObject(QObject):

    def __init__(self):
        QObject.__init__(self)

    @Slot(result='QVariantList')
    def working_get_all(self):
        self._entries = []
        for i in range(5):
            model = TestModel()
            model.name = 'Name ' + str(i)
            model.description = 'Description ' + str(i)
            self._entries.append(model)
        return self._entries

    @Slot(result='QVariantList')
    def not_working_get_all(self):
        entries = []
        for i in range(5):
            model = TestModel()
            model.name = 'Name ' + str(i)
            model.description = 'Description ' + str(i)
            entries.append(model)
        return entries


if __name__ == "__main__":

    app = QApplication(sys.argv)
    engine = QQmlApplicationEngine()

    testQObject = TestQObject()
    engine.rootContext().setContextProperty("testQObject", testQObject)

    engine.load(QUrl('main.qml'))

    # Qml file error handling
    if not engine.rootObjects():
        sys.exit(-1)

    sys.exit(app.exec_())
