【发布时间】:2015-12-14 22:45:46
【问题描述】:
我有以下代码:
QString* data = new QString("data to QML");
engine.rootContext()->setContextProperty(QStringLiteral("consoleText"), QVariant::fromValue(data));
这个不起作用,QTCreator 中的错误信息如下:
...\qglobal.h:693: 错误:静态断言失败:类型未注册,请使用 Q_DECLARE_METATYPE 宏使其知道 到 Qt 的元对象系统 #define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)
我不认为我应该使用Q_DECLARE_METATYPE 代替QString,因为如果我这样做:
engine.rootContext()->setContextProperty(QStringLiteral("consoleText"), QVariant::fromValue(QString("data to QML")));
效果很好。
我对如何将 QVariant::fromValue() 与预先声明的 QString 一起使用感兴趣。
【问题讨论】:
-
不要在堆上分配
QString,完全没有意义。 -
我只是在测试如何连接QML和c++端,没有关注其他的东西。