Passing a python list of objects to qml(将对象的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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将对象的python列表传递给qml


基础教程推荐
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 筛选NumPy数组 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01