[CLOSED] Qt Q_PROPERTY: how to pass custom C struct in QML
-
Hi to everyone,
this is my problem: I've a C struct (I cannot modify this) and I want pass it to a QML.
For example:@
typedef struct
{
int a;
int b;
} INT_CUSTOM_STRUCT;typedef struct
{
char c;
char b;
} CHAR_CUSTOM_STRUCT;typedef struct
{
INT_CUSTOM_STRUCT num;
CHAR_CUSTOM_STRUCT varChar;
} CUSTOM_C_TYPE;
@and in the header of my object (c++)
@
class MyCustomClass : public QObject
{
Q_OBJECT
Q_PROPERTY(CUSTOM_C_TYPE myStruct READ getMyStruct CONSTANT)public:
explicit MyCustomClass(QObject *parent = 0);
CUSTOM_C_TYPE getMyStruct() { return myObj;}private:
CUSTOM_C_TYPE myObj;
};
@in main.cpp, I've add this line:
@
int main(int argc, char *argv[])
{
qmlRegisterType<MyCustomClass>("com.example.myClass", 1, 0, "MyCustomClass");
QApplication app(argc, argv);MyCustomClass *myClass = new MyCustomClass(); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:///main.qml"))); engine.rootContext()->setContextProperty("myClass",myClass); return app.exec();
}@
in my Qml:
@
import QtQuick 1.2
import com.example.myClass 1.0Item {
Component.onCompleted: console.log(myClass.myStruct.num.a);
}
@It doesn't works...
-
Custom properties need to inherit QObject. So you probably need to write a wrapper class for your C struct.
Check the Q_PROPERTY documentation for more information:
http://qt-project.org/doc/qt-5/properties.html#requirements-for-declaring-properties
-
But it's very difficult create a wrapper class.... this is only an example... but I have a very big structure to pass in a qml
I've modify my files in this way:mycustomclass.h
@
typedef struct
{
int a;
int b;
} INT_CUSTOM_STRUCT;typedef struct
{
char c;
char b;
} CHAR_CUSTOM_STRUCT;typedef struct
{
int test;
INT_CUSTOM_STRUCT num;
CHAR_CUSTOM_STRUCT varChar;
} CUSTOM_C_TYPE;Q_DECLARE_METATYPE(CUSTOM_C_TYPE)
class MyCustomClass : public QObject
{
Q_OBJECT
Q_PROPERTY(CUSTOM_C_TYPE myStruct READ getMyStruct CONSTANT)
public:
explicit MyCustomClass(QObject *parent = 0);
CUSTOM_C_TYPE getMyStruct() { return myObj;}private:
CUSTOM_C_TYPE myObj;};
@mycustomclass.cpp
@
MyCustomClass::MyCustomClass(QObject *parent) :
QObject(parent)
{
myObj.num.a = 10;
myObj.test = 50;
}
@main.cpp
@
int main(int argc, char *argv[])
{
qmlRegisterType<MyCustomClass>("com.example.myClass", 1, 0, "MyCustomClass");
qRegisterMetaType<CUSTOM_C_TYPE>("CUSTOM_C_TYPE");
QApplication app(argc, argv);MyCustomClass *myClass = new MyCustomClass(); QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("myClass",myClass); engine.load(QUrl(QStringLiteral("qrc:///main.qml"))); return app.exec();
}
@main.qml
@
import QtQuick 2.2
import QtQuick.Controls 1.1
import com.example.myClass 1.0ApplicationWindow {
Component.onCompleted: console.log(myClass.myStruct)
}
@output:
@
qml: QVariant(CUSTOM_C_TYPE)
@if i change my qml file in this way:
@
import QtQuick 2.2
import QtQuick.Controls 1.1
import com.example.myClass 1.0ApplicationWindow {
Component.onCompleted: console.log(myClass.myStruct.test)
}
@my new output is:
@
qml: undefined
@ -
Please read the documentation some more. The Q_PROPERTY must be a subclass of QObject.
You are using your C-struct as the Q_PROPERTY. -
thank you for the reply
there isn't an other way to pass this c-native struct to a Qml?
-
Not without wrapping it in a QObject-derived class.