7

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.

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)

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

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: am completely new to Qml and Qt5. if i am making any fundamental errors in my concepts, please do let me know

3
  • 1
    link Commented Sep 24, 2017 at 11:08
  • You can show how you pass DisplayComponent to qml and qml please. Commented Sep 24, 2017 at 20:29
  • thanks @folibis for the link.i have made some changes based on the link you gave. eyllanesc i have edited my question accordingly. i can have the list passed to qml but cant seem to read the object. any ideas? Commented Sep 25, 2017 at 2:25

1 Answer 1

3

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

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
Sign up to request clarification or add additional context in comments.

4 Comments

that worked! thanks! may i clarify with you if my approach is right for updating this list? there will be some interaction between the user and the qml interface where the statuses in this list will change. user will make comments and these comments will be saved as a txt file on python. on successful file save, a boolean value will be returned to qml where the ListElement values will be updated and the UI will be updated according to the boolean value returned. is this workflow right?
also, anything i should take note of if i would like to edit the attributes?
@eugeneoei For me it seems correct, but how to modify the file, I do not notice that it has a function that saves in the file. you must create a setter for that attribute to be editable from qml, I've put an example at the end of my answer. Please do not forget to mark my answer as correct please.
the function that saved in the file has yet to be implemented. probably the next step i will try. thanks for the tips! yupp. i did mark it as correct, but only votes cast by 15 reputation and above will be recorded. if anyone sees this, the above worked! thanks @eyllanesc!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.