将对象的python列表传递给qml

编程入门 行业动态 更新时间:2024-10-24 19:18:57
本文介绍了将对象的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 selfmentsStatus = 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

本文发布于:2023-10-07 17:33:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1470041.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   列表   python   qml

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!