Unsolved Q_DECLARE_METATYPE(); issue loading QList<struct> in qml
-
Hello, I need to get a list of structs into a qml list or table, but I do not understand what I am doing wrong trying to get the list output to qml.
I have declared the list and struct in a namespace, so that i can declare the metatype for the struct. This gives no errors, and I have no issues appending OurFiles structs to filesList or reading from the list itself in cpp.
And there is no error from the declaration of the type or anything else, but it does not load, so I wonder, what have I missed or done wrong?
namespace { struct OurFiles { QString name; QString dateChanged; QString size; }; } Q_DECLARE_METATYPE(OurFiles);
in header class i have:
Q_PROPERTY(QList<OurFiles> getFileList READ getFileListCpp)
public:
QList<OurFiles> filesList; QList<OurFiles> getFileListCpp() {return filesList;}
and in source
OurFiles fileItem; fileItem.name = name; fileItem.size = size; fileItem.dateChanged = date; filesList.append(fileItem);
In qml i try to read like this
console.log(backendEnd.getFileList) console.log(backendEnd.getFileList[1].name)
But only get the result
qrc:/WebServer.qml:19: TypeError: Cannot read property 'name' of undefined qml: []
After change as described in post below, i have this output
qml: [QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, )] qml: undefined
Appreciate any help :)
(Edit: backendEnd is also registred so that works fine..)
-
Seems I misplaced the QList<OurFiles> filesList; declaration, moved it to inside the header class, but still some issues going on with loading,
I have 8 files that's added, so number here adds up, but seems that I have done something wrong in the setup of the QList/Struct as now my output is:console.log(backendEnd.getFileList) console.log(backendEnd.getFileList[1].name)
qml: [QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, )] qml: undefined
-
@MEMekaniske Please take time to read documentation, this will avoid you many frustrations!
Custom data type exchange with QML is only possible with QObject sub-classes. This is mandatory.
==> https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html -
@KroMignon said in Q_DECLARE_METATYPE(); issue loading QList<struct> in qml:
Custom data type exchange with QML is only possible with QObject sub-classes. This is mandatory.==> https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html
Ok, so it cannot be done.
-
@MEMekaniske said in Q_DECLARE_METATYPE(); issue loading QList<struct> in qml:
Ok, so it cannot be done.
No the way you try to do it. QML needs QObject to work, that's the deal ;)
-
I understand.
But the struct needs to be above all else to to regisred, and to use Q_OBJECT or GADGET and set struct properties that will not be private I need to be under where I can registrer the type, and then that wont work. So I don't really get it. My real issue is probably just finding out how to register the types "anywhere"..
But it took 4-5 minutes rewriting it all to use QList<QStringList> and that works just fine.. Just very confusing not being able to use struct the same "normal" way..
Thanks though :)
-
@MEMekaniske said in Q_DECLARE_METATYPE(); issue loading QList<struct> in qml:
But it took 4-5 minutes rewriting it all to use QList<QStringList> and that works just fine.. Just very confusing not being able to use struct the same "normal" way..
QStringList is a known native type in QML, your struct not.
You can register struct to work with signals/slots mechanisms, but not with QML. To add new type to QML, the type must be a QObject to enable QML to work with.
There are plenty of examples how to do it. -
@KroMignon Yeah I understood :)
I did not find any suitable examples for my case. But I will figure it out..
-
you must define struct above and outside the class definition as follows
struct OurFiles{
Q_GADGETpublic:
QString name;
QString dateChanged;
QString size;Q_PROPERTY(QString name MEMBER name) Q_PROPERTY(QString dateChanged MEMBER dateChanged) Q_PROPERTY(QString size MEMBER size)
};
Q_DECLARE_METATYPE(OurFiles)
class Test : public QObject {
Q_OBJECT
// your code
}
and you must add Q_OBJECT macro to your class.