pythonqmlpyside2qt-signalsqt-slot

How can I send a python dictionary to a QML interface with a Signal?


I want to send dictionaries, containing data that I need to use to dynamically create qml objects, from a PySide2 class to a QML interface and since I need to do it in response to certain events, I need to use signals and slots.

Since I've just started to use QML and python I tried to create a simple project just to play around (as you can see from the code)

QML:

import QtQuick 2.10
import QtQuick.Controls 2.2
import QtQuick.Window 2.2
import QtQuick.Controls.Material 2.3
import QtQuick.Layouts 1.0

ApplicationWindow {
    id: mainWindow
    width:640
    height: 480
    title: qsTr("Simple ui")
    visible: true
    locale:locale
    Rectangle {
        id: appWindow
        objectName: "splasso"
        anchors.fill: parent
        color: "yellow"
        Material.accent: Material.DeepPurple
        Material.primary: Material.Cyan
        Component.onCompleted: function(){
            TestVar.theSignal.connect(catchAnswer);
            testList.append(stuff1);
            testList.append(stuff2);
            testList.append(stuff3);
            testCombo.currentIndex = 0;
            //Just a pointless test print
            console.log(JSON.stringify(stuff1));
        }
        function catchAnswer(answer){
            console.log(JSON.stringify(answer));
        }

        ComboBox{
            id: testCombo
            anchors.centerIn: parent
            width: parent.width
            onCurrentIndexChanged: function(){
                TestVar.catchInt(currentIndex);
            }

            model: ListModel{
                id: testList
            }
        }
    }
}

Python 3:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import os
from time import sleep

from PySide2.QtCore import Qt, QObject, Signal, Slot, Property, QThread
from PySide2.QtWidgets import QApplication    
from PySide2.QtQml import QQmlApplicationEngine

class Test1(QObject):
    theSignal = Signal(dict)
    def __init__(self):
        QObject.__init__(self)

    @Slot(int)
    def catchInt(self,caught):
        print("Caught: {0}".format(caught))
        testDict = {"myAnswer":caught}
        self.theSignal.emit(testDict)


if __name__ == "__main__":
    os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
    app = QApplication(sys.argv)
    engine = QQmlApplicationEngine()
    stuff1 = {"text":"Trying"}
    stuff2 = {"text":"To send"}
    stuff3 = {"text":"Dict"}
    ctx = engine.rootContext()
    ctxVar = Test1()
    ctx.setContextProperty("stuff1", stuff1)
    ctx.setContextProperty("stuff2", stuff2)
    ctx.setContextProperty("stuff3", stuff3)
    ctx.setContextProperty("TestVar",ctxVar)
    engine.load('main.qml')
    if not engine.rootObjects():
        sys.exit(-1)

    sys.exit(app.exec_())

The outuput I expected (launching the script with "python3 Test_dict_1.py") was:

Caught: 1
qml: {"myAnswer": 1}
Caught: 2
qml: {"myAnswer": 2}
Caught: 1
qml: {"myAnswer": 1}
...etc...

What I got instead was:

Caught: 1
qml: undefined.
Caught: 2
qml: undefined.
Caught: 1
qml: undefined.
...etc...

Can you tell me what am I doing wrong? Are there errors in the code or this thing cannot be done?


Solution

  • The signature you have to use in the signal is QVariant:

    class Test1(QObject):
        theSignal = Signal('QVariant')
    
        @Slot(int)
        def catchInt(self,caught):
            print("Caught: {0}".format(caught))
            testDict = {"myAnswer":caught}
            self.theSignal.emit(testDict)