从 python 获取值并将其添加到 ListView

Get values from python and add it into a ListView

我的目标是拥有一个 ListView,我可以从请求中添加元素,因此我首先创建了一个向其中添加内容的按钮。我正在使用 findChild 函数定位 listView 并因此追加一些东西,但是 Error: 'PySide2.QtCore.QObject' object has no attribute 'append' 是我得到的全部。

这是我的 python 代码:

import sys
import os

from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine
from PySide2.QtCore import QObject, Slot

class Upaio(QObject):
    def __init__(self, *args, **kwargs):
        super(Upaio, self).__init__(*args, **kwargs)
        self.build_screen()
    
    def build_screen(self):
        # instancia GUI
        self.app = QGuiApplication(sys.argv)
        
        # instancia engine
        self.engine = QQmlApplicationEngine()
        self.engine.load(os.path.join("ui", "main.qml"))

        self.engine.rootContext().setContextProperty("connection", self)

        if not self.engine.rootObjects():
            sys.exit(-1)
        sys.exit(self.app.exec_())

    @Slot(None)
    def start(self):
        self.screen = self.engine.rootObjects()[0]
        self.list_result = self.screen.findChild(QObject, id="list_result")
        self.list_result.append({score: "test"})



def main():
    global upaio
    upaio = Upaio()

if __name__ == "__main__":
    main()

和 main.qml:

import QtQuick 2.13
import QtQuick.Window 2.13
import QtQuick.Controls 2.15

Window {
    id: main_window
    width: 640
    height: 560
    visible: true
    color: "#e0e0e0"
    title: "UpAIO"
    property alias img_logoSource: img_logo.source

    Rectangle {
        id: rec_body
        x: 0
        y: 49
        width: 639
        height: 510
        color: "#ececec"
        border.color: "#00000000"

        Rectangle {
            id: rec_start
            x: 251
            y: 458
            width: 139
            height: 41
            color: "#5253ee"
            radius: 8
            border.color: "#5253ee"
            border.width: 0

            Text {
                id: lbl_start
                x: 53
                y: 11
                color: "#ffffff"
                text: qsTr("Start")
                font.pixelSize: 16
            }

            MouseArea {
                id: msa_start
                x: 0
                y: 0
                width: 139
                height: 41

                Connections {
                    target: msa_start
                    onClicked: connection.start()
                }
            }
        }

        Text {
            id: lbl_post
            x: 511
            y: 12
            width: 49
            height: 35
            color: "#000000"
            text: qsTr("Postos")
            font.pixelSize: 16
            horizontalAlignment: Text.AlignLeft
            verticalAlignment: Text.AlignVCenter
            font.family: "Tahoma"
        }

        Rectangle {
            id: rec_result
            x: 14
            y: 294
            width: 612
            height: 151
            color: "#ffffff"
            radius: 8
            border.color: "#00000000"

            ListView {
                id: list_result
                anchors.fill: parent
                boundsBehavior: Flickable.StopAtBounds
                model: ListModel {
                    ListElement {
                    }

                    ListElement {
                    }

                    ListElement {
                    }

                    ListElement {
                    }

                    ListElement {
                    }

                    ListElement {
                    }
                }
                boundsMovement: Flickable.StopAtBounds
                clip: true
                delegate: Item {
                    x: 5
                    width: 80
                    height: 40
                    Row {
                        id: row6
                        Rectangle {
                            width: 40
                            height: 40
                            color: colorCode
                        }

                        Text {
                            text: name
                            anchors.verticalCenter: parent.verticalCenter
                            font.bold: true
                        }
                        spacing: 10
                    }
                }
            }
        }

        Text {
            id: lbl_action
            x: 86
            y: 12
            width: 37
            height: 35
            color: "#000000"
            text: qsTr("Ação")
            font.pixelSize: 16
            horizontalAlignment: Text.AlignLeft
            verticalAlignment: Text.AlignVCenter
            font.family: "Tahoma"
        }

        Text {
            id: lbl_result
            x: 282
            y: 258
            width: 71
            height: 35
            color: "#000000"
            text: qsTr("Resultado")
            font.pixelSize: 16
            horizontalAlignment: Text.AlignLeft
            verticalAlignment: Text.AlignVCenter
            font.family: "Tahoma"
        }

        Text {
            id: lbl_line
            x: 297
            y: 12
            width: 41
            height: 35
            color: "#000000"
            text: qsTr("Linha")
            font.pixelSize: 16
            horizontalAlignment: Text.AlignLeft
            verticalAlignment: Text.AlignVCenter
            font.family: "Tahoma"
        }

        Rectangle {
            id: rec_line
            x: 227
            y: 47
            width: 183
            height: 200
            color: "#ffffff"
            radius: 8

            ListView {
                id: list_line
                anchors.fill: parent
                boundsBehavior: Flickable.StopAtBounds
                boundsMovement: Flickable.StopAtBounds
                clip: true
                model: ListModel {
                    ListElement {
                        name: "Grey"
                        colorCode: "grey"
                    }

                    ListElement {
                        name: "Red"
                        colorCode: "red"
                    }

                    ListElement {
                        name: "Blue"
                        colorCode: "blue"
                    }

                    ListElement {
                        name: "Green"
                        colorCode: "green"
                    }

                    ListElement {
                        name: "yellow"
                        colorCode: "YELLOW"
                    }

                    ListElement {
                        name: "pink"
                        colorCode: "pink"
                    }
                }
                delegate: Item {
                    x: 5
                    width: 80
                    height: 40
                    Row {
                        id: row3
                        Rectangle {
                            width: 40
                            height: 40
                            color: colorCode
                        }

                        Text {
                            text: name
                            anchors.verticalCenter: parent.verticalCenter
                            font.bold: true
                        }
                        spacing: 10
                    }
                }
            }
        }

        Rectangle {
            id: rec_action
            x: 14
            y: 47
            width: 183
            height: 200
            color: "#ffffff"
            radius: 8

            ListView {
                id: list_action
                anchors.fill: parent
                boundsBehavior: Flickable.StopAtBounds
                model: ListModel {
                    ListElement {
                    }

                    ListElement {
                    }

                    ListElement {
                    }

                    ListElement {
                    }

                    ListElement {
                    }

                    ListElement {
                    }
                }
                boundsMovement: Flickable.StopAtBounds
                clip: true
                delegate: Item {
                    x: 5
                    width: 80
                    height: 40
                    Row {
                        id: row4
                        Rectangle {
                            width: 40
                            height: 40
                            color: colorCode
                        }

                        Text {
                            text: name
                            anchors.verticalCenter: parent.verticalCenter
                            font.bold: true
                        }
                        spacing: 10
                    }
                }
            }
        }

        Rectangle {
            id: rec_workstation
            x: 443
            y: 47
            width: 183
            height: 200
            color: "#ffffff"
            radius: 8
            border.color: "#00000000"

            ListView {
                id: list_workstation
                anchors.fill: parent
                boundsBehavior: Flickable.StopAtBounds
                model: ListModel {
                    ListElement {
                        name: "Grey"
                        colorCode: "grey"
                    }

                    ListElement {
                        name: "Red"
                        colorCode: "red"
                    }

                    ListElement {
                        name: "Blue"
                        colorCode: "blue"
                    }

                    ListElement {
                        name: "Green"
                        colorCode: "green"
                    }

                    ListElement {
                        name: "yellow"
                        colorCode: "YELLOW"
                    }

                    ListElement {
                        name: "pink"
                        colorCode: "pink"
                    }
                }
                boundsMovement: Flickable.StopAtBounds
                clip: true
                delegate: Item {
                    x: 5
                    width: 80
                    height: 40
                    Row {
                        id: row5
                        Rectangle {
                            width: 40
                            height: 40
                            color: colorCode
                        }

                        Text {
                            text: name
                            anchors.verticalCenter: parent.verticalCenter
                            font.bold: true
                        }
                        spacing: 10
                    }
                }
            }
        }








    }

    Rectangle {
        id: rec_header
        x: 0
        y: 0
        width: 640
        height: 48
        color: "#5253ee"
        border.color: "#00ffffff"

        Image {
            id: img_logo
            x: -377
            y: -107
            width: 929
            height: 262
            source: "../../resources/logo_hor-03.png"
            fillMode: Image.PreserveAspectFit
        }

        Rectangle {
            id: rec_upaio
            x: 49
            y: 4
            width: 200
            height: 35
            color: "#5253ee"
            border.color: "#00000000"

            Text {
                id: lbl_value_version
                x: 81
                y: 17
                width: 43
                height: 19
                color: "#ffffff"
                text: qsTr("v0.0")
                font.pixelSize: 10
                horizontalAlignment: Text.AlignLeft
                verticalAlignment: Text.AlignVCenter
                font.family: "Tahoma"
            }

            Text {
                id: lbl_upaio
                x: 2
                y: 2
                width: 106
                height: 35
                color: "#ffffff"
                text: qsTr("UpAIO")
                font.pixelSize: 26
                horizontalAlignment: Text.AlignLeft
                verticalAlignment: Text.AlignVCenter
                font.family: "Tahoma"
            }
        }
    }
}

append应该在QML文件中调用。

我通过创建一个 ListModel 解决了这个问题,将它插入到 ListView 中,在按钮 (MouseArea) 中调用 append 方法并从 python 函数中获取参数:

import sys
import os
import requests
import json

from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine
from PySide2.QtCore import QObject, Slot

class Upaio(QObject):
    def __init__(self, *args, **kwargs):
        super(Upaio, self).__init__(*args, **kwargs)
        self.build_screen()
    
    def build_screen(self):
        # instancia GUI
        self.app = QGuiApplication(sys.argv)
        
        # instancia engine
        self.engine = QQmlApplicationEngine()
        self.engine.load(os.path.join("ui", "main.qml"))

        self.engine.rootContext().setContextProperty("connection", self)

        if not self.engine.rootObjects():
            sys.exit(-1)
        sys.exit(self.app.exec_())

    @Slot(None, result=str)
    def start(self):
        req = requests.get('http://viacep.com.br/ws/78785970/json/')
        body = json.loads(req.content)
        logradouro = str(body['logradouro'])
        return logradouro

main.qml

import QtQuick 2.13
import QtQuick.Window 2.13
import QtQuick.Controls 2.15
import QtQml.Models 2.15

Window {
    id: main_window
    width: 640
    height: 560
    visible: true
    color: "#e0e0e0"
    title: "UpAIO"
    property alias img_logoSource: img_logo.source

    ListModel{
        id: list_model_action
    }
    ListModel{
        id: list_model_line
    }
    ListModel{
        id: list_model_workstation
    }
    ListModel{
        id: list_model_result
    }

    Rectangle {
        id: rec_body
        x: 0
        y: 49
        width: 639
        height: 510
        color: "#ececec"
        border.color: "#00000000"

        Rectangle {
            id: rec_start
            x: 251
            y: 458
            width: 139
            height: 41
            color: "#5253ee"
            radius: 8
            border.color: "#5253ee"
            border.width: 0

            Text {
                id: lbl_start
                x: 53
                y: 11
                color: "#ffffff"
                text: qsTr("Start")
                font.pixelSize: 16
            }

            MouseArea {
                id: msa_start
                x: 0
                y: 0
                width: 139
                height: 41

                Connections {
                    target: msa_start
                    onClicked: list_model_action.append({score: connection.start()})
                }
            }
        }

        Text {
            id: lbl_post
            x: 511
            y: 12
            width: 49
            height: 35
            color: "#000000"
            text: qsTr("Postos")
            font.pixelSize: 16
            horizontalAlignment: Text.AlignLeft
            verticalAlignment: Text.AlignVCenter
            font.family: "Tahoma"
        }

        Rectangle {
            id: rec_result
            x: 14
            y: 294
            width: 612
            height: 151
            color: "#ffffff"
            radius: 8
            border.color: "#00000000"

            ListView {
                id: list_result
                anchors.fill: parent
                boundsBehavior: Flickable.StopAtBounds
                model: list_model_result
                boundsMovement: Flickable.StopAtBounds
                clip: true
                delegate: Item {
                    x: 5
                    width: 80
                    height: 40
                    Row {
                        spacing: 15
                        Text {
                            text: score
                        }
                    }
                }
            }
        }

        Text {
            id: lbl_action
            x: 86
            y: 12
            width: 37
            height: 35
            color: "#000000"
            text: qsTr("Ação")
            font.pixelSize: 16
            horizontalAlignment: Text.AlignLeft
            verticalAlignment: Text.AlignVCenter
            font.family: "Tahoma"
        }

        Text {
            id: lbl_result
            x: 282
            y: 258
            width: 71
            height: 35
            color: "#000000"
            text: qsTr("Resultado")
            font.pixelSize: 16
            horizontalAlignment: Text.AlignLeft
            verticalAlignment: Text.AlignVCenter
            font.family: "Tahoma"
        }

        Text {
            id: lbl_line
            x: 297
            y: 12
            width: 41
            height: 35
            color: "#000000"
            text: qsTr("Linha")
            font.pixelSize: 16
            horizontalAlignment: Text.AlignLeft
            verticalAlignment: Text.AlignVCenter
            font.family: "Tahoma"
        }

        Rectangle {
            id: rec_line
            x: 227
            y: 47
            width: 183
            height: 200
            color: "#ffffff"
            radius: 8

            ListView {
                id: list_line
                anchors.fill: parent
                model: list_model_line
                boundsBehavior: Flickable.StopAtBounds
                boundsMovement: Flickable.StopAtBounds
                clip: true
                delegate: Item {
                    x: 5
                    width: 80
                    height: 40
                    Row {
                        spacing: 15
                        Text {
                            text: score
                        }
                    }
                }
            }
        }

        Rectangle {
            id: rec_action
            x: 14
            y: 47
            width: 183
            height: 200
            color: "#ffffff"
            radius: 8

            ListView {
                id: list_action
                anchors.fill: parent
                boundsBehavior: Flickable.StopAtBounds
                model: list_model_action
                boundsMovement: Flickable.StopAtBounds
                clip: true
                delegate: Item {
                    x: 5
                    width: 80
                    height: 40
                    Row {
                        spacing: 15
                        Text {
                            text: score
                        }
                    }
                }
            }
        }

        Rectangle {
            id: rec_workstation
            x: 443
            y: 47
            width: 183
            height: 200
            color: "#ffffff"
            radius: 8
            border.color: "#00000000"

            ListView {
                id: list_workstation
                anchors.fill: parent
                boundsBehavior: Flickable.StopAtBounds
                model: list_model_workstation
                boundsMovement: Flickable.StopAtBounds
                clip: true
                delegate: Item {
                    x: 5
                    width: 80
                    height: 40
                    Row {
                        spacing: 15
                        Text {
                            text: score
                        }
                    }
                }
            }
        }




    }

    Rectangle {
        id: rec_header
        x: 0
        y: 0
        width: 640
        height: 48
        color: "#5253ee"
        border.color: "#00ffffff"

        Image {
            id: img_logo
            x: -377
            y: -107
            width: 929
            height: 262
            source: "../../resources/logo_hor-03.png"
            fillMode: Image.PreserveAspectFit
        }

        Rectangle {
            id: rec_upaio
            x: 49
            y: 4
            width: 200
            height: 35
            color: "#5253ee"
            border.color: "#00000000"

            Text {
                id: lbl_value_version
                x: 81
                y: 17
                width: 43
                height: 19
                color: "#ffffff"
                text: qsTr("v0.0")
                font.pixelSize: 10
                horizontalAlignment: Text.AlignLeft
                verticalAlignment: Text.AlignVCenter
                font.family: "Tahoma"
            }

            Text {
                id: lbl_upaio
                x: 2
                y: 2
                width: 106
                height: 35
                color: "#ffffff"
                text: qsTr("UpAIO")
                font.pixelSize: 26
                horizontalAlignment: Text.AlignLeft
                verticalAlignment: Text.AlignVCenter
                font.family: "Tahoma"
            }
        }
    }
}