Read c++ struct from QML
-
hi,
I need to read struct from qml but I dont know how.
Here some code:@struct ThumbStruct {
int ID;
QString description_row1;
QString description_row2;
QString image_tburl;
QString image_url;
} ;
Q_DECLARE_METATYPE(ThumbStruct)class ThumbItem : public QDeclarativeItem
{
Q_OBJECTQ_PROPERTY(ThumbStruct properties READ properties WRITE setProperties NOTIFY propertiesChanged)
public:
ThumbItem(QDeclarativeItem *parent=0);ThumbStruct properties() const; void setProperties(const ThumbStruct &properties); ThumbStruct m_properties;
signals:
void propertiesChanged();
}@Reading from QML for example in a Text component like above, give me "Unable to assign [undefined] to QString text"
@Text {
id: thumbText
text: properties.description_row1
}@if I add other Q_PROPERTY, they work
I am new to QML, so maybe the syntax is wrong ?Thanks in advance
-
bq. Maybe ThumbStruct should derive from QObject
So I have to declare ThumbStruct as a class instead of a struct ? Or how I can derive a struct type from QObject ?
bq. description_row1 and other fields should be declared as Q_PROPERTY
If I do this I can read description_row1 because of a known type, but the problem is that seems ThumbStruct isn't a known type within QML even if I declared it with Q_DECLARE_METATYPE(ThumbStruct)
Since ThumbStruct has a lot more fields ( and also other structs ), I would like to read it directly instead of making many Q_PROPERTY with get, write and notify functions.
Thanks for your time -
I don't know how it is about Q_OBJECT macro so it will be better to make ThumbStruct a class instead of struct.
I would change properties property in your ThumbItem to be of type QObject*!
Then after adding Q_PROPERTY items to ThumbStruct it should work, I guess :) -
Yep, to access object properties in QML class should be derived from QObject, contain Q_OBJECT macro, have Q_PROPERTY (or Q_INVOKABLE/slot in case of methods) and if it is returned by other method (not exposed as context property) you should register it with qmlRegisterType (or qmlRegisterUncreatableType)
-
The above discussion was helpful however it does not explicitly say that it is impossible to read/write a C/C++ struct that does not inherit from QObject or have Q_PROPERTYs in QML. Is there any way to register a non-QObject data type so that it can be used in a QML context?
It would be very helpful for our project if this were possible because there is a lot of code already written that has data which the UI needs to access but is not in a class that derives from QObject. This was possible with the widget based UI we currently have by using the Q_DECLARE_METATYPE() macro and the qRegisterMetaType<>() method. We would very much like to change over to QML for the UI but the amount of work may be prohibitive.
Thank you!
-
Denis was quite clear, I think:
[quote author="Denis Kormalev" date="1312616496"]Yep, to access object properties in QML class should be derived from QObject, contain Q_OBJECT macro, have Q_PROPERTY (or Q_INVOKABLE/slot in case of methods) and if it is returned by other method (not exposed as context property) you should register it with qmlRegisterType (or qmlRegisterUncreatableType)[/quote]
So, the answer is no, you cannot export structs to a QML context.
edit:
OK, structs as you would have then in C: POD's. -
bq. So I have to declare ThumbStruct as a class instead of a struct ? Or how I can derive a struct type from QObject ?
bq. it will be better to make ThumbStruct a class instead of struct.
There is no difference between class and structure in C++ except the default "access restriction"(public or private). So don't be so panic about making class from your structure as well as feel free to inherit your structure from the QObject. Just choose the way you like more
-
But remember that "Q_OBJECT":http://developer.qt.nokia.com/doc/qt-4.8/qobject.html#Q_OBJECT macro must appear in private section of your class.
You mat try to use "QVariant":http://developer.qt.nokia.com/doc/qt-4.8/qvariant.html to export data from C++ to QML.