从python在QML中设置属性

2024-06-30 13:14:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从python函数中获取一个字符串返回QML。{{cd2>似乎是从python的ListElement的属性时,它似乎不起作用。

第一个主.qml下面的工作很好。

第二次主.qml附加是一个不起作用的。如果您想尝试其他方法是否正常,只需注释掉“property string value:myDB.GET收入()'并赋予它一些值

在主.py

import sys
from PyQt5.QtCore import QUrl, QObject, pyqtSlot, pyqtSignal, pyqtProperty, QVariant
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQuick import QQuickView



class MyDB(QObject):
        def __init__(self):
                super().__init__()

        @pyqtSlot(result=str)
        def getIncome(self):
                return "testing income"


## Main Function
if __name__ == '__main__':
        myApp = QApplication(sys.argv)

        appLabel = QQuickView()
        mydb = MyDB()
        appLabel.rootContext().setContextProperty("myDB", mydb)
        appLabel.setSource(QUrl('main.qml'))

        appLabel.show()

        myApp.exec_()
        sys.exit()

在主.qml(工作正常)

^{pr2}$

在主.qml(不工作)

import QtQuick 2.0

Rectangle {
    id: root
    width: 250; height: 450
    color: "white"

    ListModel {
        id: mainModel

        ListElement {
            name: "Money"

            //property string value: 3
            property string value: myDB.getIncome()

        }

        ListElement {
            name: "Total Expenses"
            property string value: 4
        }

        ListElement {
            name: "Total Income"
            property string value: 56
        }
    }

    Component {
        id: mainDelegate
        Item {
            width: 100; height: 100
            scale: PathView.iconScale

            Image {
                id: myIcon
                y: 20; anchors.horizontalCenter: parent.horizontalCenter
                source: icon
            }
            Text {
                anchors { top: myIcon.bottom; horizontalCenter: parent.horizontalCenter }
                font.pixelSize: 18
                text: name + ": " + value
            }

            MouseArea {
                anchors.fill: parent
            }
        }
    }

    Component {
        id: appHighlight
        Rectangle { width: 130; height: 10; color: "lightsteelblue" }
    }

    PathView {
        id: view
        anchors.fill: parent
        highlight: appHighlight
        preferredHighlightBegin: 0.5
        preferredHighlightEnd: 0.5
        focus: true
        model: mainModel
        delegate: mainDelegate
        path: Path {
            startX: root.width/2
            startY: 0
            PathAttribute { name: "iconScale"; value: 0.5 }
            PathLine { x: root.width/2; y: root.height*0.4; }
            PathPercent { value: 0.48; }
            PathLine { x: root.width/2; y: root.height*0.5; }
            PathAttribute { name: "iconScale"; value: 1.0 }
            PathLine { x: root.width/2; y: root.height*0.6; }
            PathPercent { value: 0.52; }
            PathLine { x: root.width/2; y: root.height; }
            PathAttribute { name: "iconScale"; value: 0.5 }
        }
    }
}

Tags: nameimportidstringvaluepropertyrootqml