Search code examples
pythonqtpyqtqmlpyside6

How to update textEdit properties in Qt Qml using dictionary from python?


There are 4 textEdit's in my qml code. (There is no reason to leave a minimal code snippet of it, it can be the most basic ones...)

The format of the dictionary from python looks like this:

{'textEdit1': ['999999', '999999', '999999', '999999', '999999', '999999'], 'textEdit2': ['Barbara ', 'Marieke', 'Ramses', 'Reatie', 'Gaby', 'Marthe'], 'textEdit3': ['Bijvank', 'Sassen', 'Man', 'Projecten', 'Knol', 'Noordijk'], 'textEdit4': ['', '', '', '', '', '']}

The keys of the dictionary are exactly named as the id of the textEdit's.

The idea is to update the appropriate textEdits by id's (keys of the dictionary) and using the values of the dictionary as textEdit.text (text property of textEdit).

I wrote the following javascript function when the signal is received from python:

function onSendBackDictionaryTextEditData(myDictionary) {
    for (const key in myDictionary) {
        if (myDictionary.hasOwnProperty(key)) {
            const value = myDictionary[key];
            //console.log(`${key}: ${value}`);
            var target = `${key}`
            var textEditValues = `${value}`
            target.text = textEditValues
            console.log(textEditValues)
        }
    }
}

I can see all the keys and values received if i do console.log, but unfortunately I cannot update the text property. Can someone provide a minimal code snippet how to solve this issue?

I am using PySide6.

Here comes a minimal code snippet from the main.py file and from the main.qml file.

main.py:

import sys
from pathlib import Path

from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine
from PySide6.QtCore import QObject, Slot, Signal


class Main(QObject):

    onSendBackDictionaryTextEditData = Signal('QVariant')


    def __init__(self, parent=None):
        super(Main, self).__init__(parent)

    @Slot()
    def populatingTextEdit(self):
        textEditData = {'textEdit1': ['999999', '999999', '999999', '999999', '999999', '999999'], 'textEdit2': ['Barbara ', 'Marieke', 'Ramses', 'Reatie', 'Gaby', 'Marthe'], 'textEdit3': ['Bijvank', 'Sassen', 'Man', 'Projecten', 'Knol', 'Noordijk'], 'textEdit4': ['', '', '', '', '', '']}
        self.sendBackSwishQrTextEditData.emit(textEditData)


if __name__ == "__main__":
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()

    main = Main()
    engine.rootContext().setContextProperty("main", main)
    qml_file = Path(__file__).resolve().parent / "main.qml"
    engine.load(qml_file)
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec())

main.qml:

import QtQuick
import QtQuick.Window
import QtQuick.Controls
import QtQuick.Layouts

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    Rectangle {
        id: bg
        color: "#000000"
        anchors.fill: parent

        RowLayout {
            id: rowLayout
            x: 0
            y: 0
            width: 640
            height: 202

            Rectangle {
                id: textEditrectangle1
                width: 200
                height: 200
                color: "#ffffff"
                Layout.fillHeight: true
                Layout.fillWidth: true

                TextEdit {
                    id: textEdit1
                    text: qsTr("")
                    anchors.fill: parent
                    font.pixelSize: 12
                    font.family: "Arial"
                }
            }

            Rectangle {
                id: textEditrectangle2
                width: 200
                height: 200
                color: "#ffffff"
                Layout.fillWidth: true
                Layout.fillHeight: true

                TextEdit {
                    id: textEdit2
                    text: qsTr("")
                    anchors.fill: parent
                    font.pixelSize: 12
                    font.family: "Arial"
                }
            }

            Rectangle {
                id: textEditrectangle3
                width: 200
                height: 200
                color: "#ffffff"
                Layout.fillWidth: true
                Layout.fillHeight: true

                TextEdit {
                    id: textEdit3
                    text: qsTr("")
                    anchors.fill: parent
                    font.pixelSize: 12
                    font.family: "Arial"
                }
            }

            Rectangle {
                id: textEditrectangle4
                width: 200
                height: 200
                color: "#ffffff"
                Layout.fillWidth: true
                Layout.fillHeight: true

                TextEdit {
                    id: textEdit4
                    text: qsTr("")
                    anchors.fill: parent
                    font.pixelSize: 12
                    font.family: "Arial"
                }
            }
        }
    }
    Connections {
        target: main

        function onSendBackDictionaryTextEditData(myDictionary) {
            for (const key in myDictionary) {
                if (myDictionary.hasOwnProperty(key)) {
                    const value = myDictionary[key];
                    const TextEditElement = `${key}`;
                    const TextEditValues = `${value}`;
                    console.log(`${key}, ${value}`)
                }
            }
        }
    }
    Component.onCompleted: {
        main.populatingTextEdit()
    }
}

Solution

  • I have solved it see the updated qml code (thanks for the hint to create dict Object):

    import QtQuick
    import QtQuick.Window
    import QtQuick.Controls
    import QtQuick.Layouts
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        property var dict: Object({
                textEdit1: textEdit1,
                textEdit2: textEdit2,
                textEdit3: textEdit3,
                textEdit4: textEdit4
            })
    
        Rectangle {
            id: bg
            color: "#000000"
            anchors.fill: parent
    
            RowLayout {
                id: rowLayout
                x: 0
                y: 0
                width: 640
                height: 202
    
                Rectangle {
                    id: textEditrectangle1
                    width: 200
                    height: 200
                    color: "#ffffff"
                    Layout.fillHeight: true
                    Layout.fillWidth: true
    
                    TextEdit {
                        id: textEdit1
                        text: qsTr("")
                        anchors.fill: parent
                        font.pixelSize: 12
                        font.family: "Arial"
                    }
                }
    
                Rectangle {
                    id: textEditrectangle2
                    width: 200
                    height: 200
                    color: "#ffffff"
                    Layout.fillWidth: true
                    Layout.fillHeight: true
    
                    TextEdit {
                        id: textEdit2
                        text: qsTr("")
                        anchors.fill: parent
                        font.pixelSize: 12
                        font.family: "Arial"
                    }
                }
    
                Rectangle {
                    id: textEditrectangle3
                    width: 200
                    height: 200
                    color: "#ffffff"
                    Layout.fillWidth: true
                    Layout.fillHeight: true
    
                    TextEdit {
                        id: textEdit3
                        text: qsTr("")
                        anchors.fill: parent
                        font.pixelSize: 12
                        font.family: "Arial"
                    }
                }
    
                Rectangle {
                    id: textEditrectangle4
                    width: 200
                    height: 200
                    color: "#ffffff"
                    Layout.fillWidth: true
                    Layout.fillHeight: true
    
                    TextEdit {
                        id: textEdit4
                        text: qsTr("")
                        anchors.fill: parent
                        font.pixelSize: 12
                        font.family: "Arial"
                    }
                }
            }
        }
        Connections {
            target: main
    
            function onSendBackDictionaryTextEditData(myDictionary) {
                for (const key in myDictionary) {
                    if (myDictionary.hasOwnProperty(key) && dict.hasOwnProperty(key)) {
                        var textEdit = dict[key];
                        if (textEdit !== null && textEdit instanceof TextEdit) {
                            textEdit.text = myDictionary[key].join("\n");
                        }
                    }
                }
            }
        }
        Component.onCompleted: {
            main.populatingTextEdit()
        }
    }