Passing data structures from QML to C++
-
Hi all -
I'm posting this in Brainstorming because I'm not sure whether it's a C++ question or a QML question, or somewhere in between.
I have a C++ class that maintains a QList of a struct I've defined. I need to add a (populated) element to this list from QML. How is this typically accomplished? Should I try to make my QML "struct aware" (something I don't know how to do, or am I better off updating the C++ class one element at a time?
I hope this makes sense. Here's what my struct looks like (currently defined outside of any class):
struct Activity { Q_GADGET Q_PROPERTY(QString name MEMBER m_name) Q_PROPERTY(QList<QString> equipment MEMBER m_equipment) Q_PROPERTY(bool recommended MEMBER m_recommended) public: QString m_name; QList<QString> m_equipment; bool m_recommended; }; Q_DECLARE_METATYPE(Activity)
-
@mzimmers said in Passing data structures from QML to C++:
Here's my attempt to create an Activity instance:
Activity {}
Use a helper C++ function that returns a default Activity object: https://stackoverflow.com/questions/42797359/how-to-create-new-instance-of-a-q-gadget-struct-in-qml
-
When I try to access this struct in QML, I get an error. Here's my attempt to create an Activity instance:
Activity {}
At run-time, I get this error: Activity is not a type.
I also have a method in my C++ class:
Q_INVOKABLE void appendItem(Activity item) { m_list->appendItem(item); }
which I try to use like so:
activityModel.appendItem("")
which causes the run-time error:
Could not convert argument 0 at expression for onClicked: TypeError: Passing incompatible arguments to C++ functions from JavaScript is not allowed.
I tried this and got the same error as above:activityModel.appendItem({name: "from", equipment: ["QML", "YAY"], recommended: false})
So...I'm not sure where to go from here. I suppose I could declare an Activity object in my class, that would be used only for the QML interface, but that sounds like a hack. Any other ideas on this?
Thanks...
-
Struct is a class in C++ and treat it as a class.
https://stackoverflow.com/questions/45650277/best-way-to-access-a-cpp-structure-in-qml -
I tried that:
class Activity { //: public QObject { // Q_GADGET // Q_OBJECT // Q_PROPERTY(QString name MEMBER m_name) // Q_PROPERTY(QList<QString> equipment MEMBER m_equipment) // Q_PROPERTY(bool recommended MEMBER m_recommended) public: QString m_name; QList<QString> m_equipment; bool m_recommended; };
and I've registered Activity in main.cpp:
qRegisterMetaType<Activity>("Activity");
but when I try to use it in my QML file:
import ActivityList // where Activity is defined Activity {}
I get an error: "Activity is not a type."
-
-
@mzimmers said in Passing data structures from QML to C++:
Here's my attempt to create an Activity instance:
Activity {}
Use a helper C++ function that returns a default Activity object: https://stackoverflow.com/questions/42797359/how-to-create-new-instance-of-a-q-gadget-struct-in-qml
-
(Further questions forked to https://forum.qt.io/topic/142227/update-c-list-model-from-qml )