Note that there are some explanatory texts on larger screens.

plurals
  1. POQt QML C++ QList of structs as custom ListView model
    primarykey
    data
    text
    <p>What I have in my project is the binary file that contains list of structure elements:</p> <pre><code>typedef struct { unsigned int id; char name[SIZE]; } Entry; </code></pre> <p>After reading the data from file I have all the read values stored in the following field of my class:</p> <pre><code>QVector&lt;Entry&gt; entires; </code></pre> <p>I do expose this list to QML with the following declaration:</p> <pre><code>Q_PROPERTY(QVector&lt;Entry&gt; FileContents READ getEntries NOTIFY TmpFileUpdated) </code></pre> <p>followed by getter and setter methods.</p> <pre><code>inline QVector&lt;Entry&gt; getEntries () { return this-&gt;entires; } inline void setEntries(QVector&lt;entires&gt; entries) { this-&gt;entires= entries; emit TmpFileUpdated(); } </code></pre> <p>Each time the file is read "setEntries" method is used to set the vector and to emit the signal.</p> <p>The listview in QML has the the Q_PROPERTY FileContents attached to the model:</p> <pre><code>ListView { id: myListView width: 200 height: 400 model: myInjectedObject.FileContents delegate: Row { spacing: 10 Text { text: model.entry_type // (1) font.pixelSize: 12 horizontalAlignment: "AlignHCenter" verticalAlignment: "AlignVCenter" height: 20 } } } </code></pre> <p>How to access the data that is kept on the list of structures and display it in QML?</p> <p><strong>UPDATE:</strong> After your suggestions I changed the code slightly and it compiles fine now. A following class was created:</p> <pre><code>class EntryClass: QObject { Q_OBJECT Q_PROPERTY(QString entry_name READ getEntryName) public: inline EntryClass(Entry entrystruct) { this-&gt;entry = entrystruct; } private: Entry entry; inline QString getEntryName() const { return this-&gt;entry-&gt;entry_name; } }; ListView { id: myListView width: 200 height: 400 model: myInjectedObject.FileContents delegate: Row { spacing: 10 Text { text: modelData.entry_name // (1) font.pixelSize: 12 horizontalAlignment: "AlignHCenter" verticalAlignment: "AlignVCenter" height: 20 } } } </code></pre> <p><strong>UPDATE 2</strong> OK, after some more analysis I've managed to find the solution that works. Regarding the ListView declaration above it was updated to the current state (passing struct by reference didn't work, I had to use copy by value).</p> <p>Both answers were helpful in some way, but since only one can be accepted I'll accept the first one written by Radon. Thank you both for guidance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload