How to access C++ struct elements in QML
-
wrote on 4 Feb 2024, 16:40 last edited by Praveen.Illa 2 Apr 2024, 16:41
Hi Team,
How can I access struct elements declared in Qt-C++ from QML.
Please find sample piece of code and pls let me know what I am doing wrong
Thank you**Sample.h** struct Details : public QObject { QString deviceName; } class Sample : public QObject { Q_PROPERTY(QList<Details *> list READ getList CONSTANT) } **Sample.cpp** Sample::Sample() { Details *dd1 = new Details ; dd1->setName("cc1"); m_list.append(dd1); } QList<DeviceDetails *> Sample::getList() { return m_list; } **main.cpp** Sample sampleObj; engine.rootContext()->setContextProperty("qmlSample", &sampleObj); **main.qml** Column { anchors.centerIn: parent spacing: 10 Repeater { model: qmlSample.list delegate: Row { spacing: 10 Text { text: modelData.deviceName font.pixelSize: 15 } } } }
-
Hi Team,
How can I access struct elements declared in Qt-C++ from QML.
Please find sample piece of code and pls let me know what I am doing wrong
Thank you**Sample.h** struct Details : public QObject { QString deviceName; } class Sample : public QObject { Q_PROPERTY(QList<Details *> list READ getList CONSTANT) } **Sample.cpp** Sample::Sample() { Details *dd1 = new Details ; dd1->setName("cc1"); m_list.append(dd1); } QList<DeviceDetails *> Sample::getList() { return m_list; } **main.cpp** Sample sampleObj; engine.rootContext()->setContextProperty("qmlSample", &sampleObj); **main.qml** Column { anchors.centerIn: parent spacing: 10 Repeater { model: qmlSample.list delegate: Row { spacing: 10 Text { text: modelData.deviceName font.pixelSize: 15 } } } }
@Praveen-Illa said in How to access C++ struct elements in QML:
modelData.deviceName
Just
deviceName
should be enough.I hope you have used
Q_OBJECT
macro in all these QObject subclasses? And you have initialized parent class properly in constructor?Are you getting any error from QML? If not, the problem might be much simpler: your
Text
,Row
,Column
elements have no dimensions (width
andheight
). -
@Praveen-Illa said in How to access C++ struct elements in QML:
modelData.deviceName
Just
deviceName
should be enough.I hope you have used
Q_OBJECT
macro in all these QObject subclasses? And you have initialized parent class properly in constructor?Are you getting any error from QML? If not, the problem might be much simpler: your
Text
,Row
,Column
elements have no dimensions (width
andheight
).wrote on 6 Feb 2024, 05:29 last edited by Praveen.Illa 2 Jun 2024, 13:22@sierdzio Hi, Thank you for your response.
Yes, I have define Q_OBJECT macro in all subclasses.
Even though, I am not able to access the struct elements.
Since, the above sample code provided is pseudo code, so I haven't mentioned all the details. sorry for that.
I came across this below link from stack overflow but I am not sure wether Qt supports or not support exposing struct elements to QML
https://stackoverflow.com/questions/7980900/qt-qml-c-qlist-of-structs-as-custom-listview-model#:~:text=QML cannot access "low-level,access these properties in QML. -
@sierdzio Hi, Thank you for your response.
Yes, I have define Q_OBJECT macro in all subclasses.
Even though, I am not able to access the struct elements.
Since, the above sample code provided is pseudo code, so I haven't mentioned all the details. sorry for that.
I came across this below link from stack overflow but I am not sure wether Qt supports or not support exposing struct elements to QML
https://stackoverflow.com/questions/7980900/qt-qml-c-qlist-of-structs-as-custom-listview-model#:~:text=QML cannot access "low-level,access these properties in QML.wrote on 7 Feb 2024, 13:53 last edited by@Praveen-Illa in the code you provided in your initial message, you did not show the
Q_OBJECT
macros. If you are defining them, that means we are not seeing the same code that you are using so it is difficult to see what you might be doing wrong.One thing I notice from your code is that you don't define
deviceName
as aQ_PROPERTY
(see the answers in the StackOverflow link you mentioned). Is it the same in your real code?Another thing to mention is that if you are just exposing a simple struct, it might be worth looking into using
Q_GADGET
instead.
1/4