将 Python 对象列表传递给 qml [英] Passing a python list of objects to qml

查看:50
本文介绍了将 Python 对象列表传递给 qml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将对象列表从 python 传递到 qml.在qml方面,我将解释这些信息,并使用repeater和listmodel元素,以类似表格的方式显示这些信息.

I am trying to pass a list of objects from python to qml. on the qml side, I will interpret this information and using repeater and listmodel element, display these information in a table like manner.

如果我只是传递一个对象或整数列表,我可以读取 qml 端的信息.但否则在尝试传递对象列表时.我如何读取 qml 端的对象列表?我必须使用不同的属性吗?

if i simply pass an object or a list of integers, i could read the information on the qml side. but otherwise when trying to pass a list of objects. how can i read a list of objects on the qml side? do i have to use different properties?

以下是我目前所拥有的:

below is what i have so far:

class File(QObject):
    def __init__(self, fileName, commentsStatus, diagnosisStatus, parent=None):
        QObject.__init__(self, parent)
        self.fileName = fileName
        self.commentsStatus = commentsStatus
        self.diagnosisStatus = diagnosisStatus  

class DisplayComponent(QObject):
    def __init__(self, parent = None):
        QObject.__init__(self, parent)
        self.list = [File("file 1", True, False), File("file 2", False, True)]

    @pyqtProperty(QQmlListProperty)
    def getDicomFilesList(self):
        return QQmlListProperty(File, self, self.list)

通过以下方式暴露给 qml 端:context.setContextProperty("dicomFiles", displayComponent)

exposing to the qml side the following way: context.setContextProperty("dicomFiles", displayComponent)

这就是我在 qml 端阅读列表的方式:

and this is how i am reading the list on the qml side:

HanaContainer {

    Text {
        id: display
        text: "no signal detected yet"
    }

    Component.onCompleted: {
        console.log(dicomFiles.getDicomFilesList[1]) // prints File(0x7f8a6d454c70)
        console.log(dicomFiles.getDicomFilesList[1].fileName) // prints undefined
    }
}

ps:我是 Qml 和 Qt5 的新手.如果我在我的概念中犯了任何基本错误,请告诉我

ps: am completely new to Qml and Qt5. if i am making any fundamental errors in my concepts, please do let me know

推荐答案

要使属性在 qml 中可见,这必须是一个属性,为此您应该使用 pyqtProperty 如下图:

For an attribute to be visible in qml this must be a property, for this you should use pyqtProperty as shown below:

class File(QObject):
    def __init__(self, fileName, commentsStatus, diagnosisStatus, parent=None):
        QObject.__init__(self, parent)
        self._fileName = fileName
        self._commentsStatus = commentsStatus
        self._diagnosisStatus = diagnosisStatus

    @pyqtProperty(str)
    def fileName(self):
        return self._fileName

    @pyqtProperty(bool)
    def commentsStatus(self):
        return self._commentsStatus

    @pyqtProperty(bool)
    def diagnosisStatus(self):
        return self._diagnosisStatus

如果我们想成为可编辑的implementetar setter,以上将使属性仅可读,例如:

The above will make the attribute only readable, if we want to be editable implementetar setters, eg:

@fileName.setter
def fileName(self, value):
    self._fileName = value

这篇关于将 Python 对象列表传递给 qml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆