I have the following code:
QString* data = new QString("data to QML");
engine.rootContext()->setContextProperty(QStringLiteral("consoleText"), QVariant::fromValue(data));
and this one does not work, the error message in QTCreator is the following:
...\qglobal.h:693: error: static assertion failed: Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system #define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)
I do no think that I should use Q_DECLARE_METATYPE
for QString
because if I do something like this:
engine.rootContext()->setContextProperty(QStringLiteral("consoleText"), QVariant::fromValue(QString("data to QML")));
it works fine.
I am interested in that how can I use the QVariant::fromValue()
with a pre-declared QString.