Pregunta

What I have in my project is the binary file that contains list of structure elements:

typedef struct {
    unsigned int   id;
    char           name[SIZE];
} Entry;

After reading the data from file I have all the read values stored in the following field of my class:

QVector<Entry> entires;

I do expose this list to QML with the following declaration:

Q_PROPERTY(QVector<Entry> FileContents READ getEntries NOTIFY TmpFileUpdated)

followed by getter and setter methods.

inline QVector<Entry> getEntries () 
{ 
    return this->entires; 
}

inline void setEntries(QVector<entires> entries) 
{ 
    this->entires= entries; 
    emit TmpFileUpdated(); 
}

Each time the file is read "setEntries" method is used to set the vector and to emit the signal.

The listview in QML has the the Q_PROPERTY FileContents attached to the model:

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
        }
    }
}

How to access the data that is kept on the list of structures and display it in QML?

UPDATE: After your suggestions I changed the code slightly and it compiles fine now. A following class was created:

class EntryClass: QObject
{
    Q_OBJECT
    Q_PROPERTY(QString entry_name READ getEntryName)
public:
    inline EntryClass(Entry entrystruct)
    {
        this->entry = entrystruct;
    }
private:
    Entry entry;

    inline QString getEntryName() const
    {
        return this->entry->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
        }
    }
}

UPDATE 2 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).

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!

¿Fue útil?

Solución

QML cannot access "low-level" struct types.

But you can create a class EntryClass that inherits QObject and add id and name as Qt properties (internally EntryClass can use the data of the corresponding Entry struct instances, e.g. by using a pointer to it). Then you should be able to access these properties in QML.

(But I didn't test it)

Otros consejos

Radon is right, your objects exported to QML need properties (or Q_INVOKABLE functions).

Additionally, QVector won't work either. You need to use QDeclarativeListProperty or QVariantList as the property type.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top