[英]Using std::string in QML
I have a STL codebase that need to be shared between a QtQuick 2.0 application (the graphical interface) and a purely STL application (the server). 我有一个STL代码库,需要在QtQuick 2.0应用程序(图形界面)和纯STL应用程序(服务器)之间共享。 The interface can derive its classes from the shared STL codebase, so it can have
Q_PROPERTY
s, signals, slots, etc. but the shared data structures need to remain STL-only. 接口可以从共享的STL代码库派生其类,因此它可以具有
Q_PROPERTY
,信号,插槽等,但是共享的数据结构需要保持仅STL。
I'd like to avoid data duplication ( std::string
-> QString
, etc.) so I tried to use std::string
directly within the Q_PROPERTY
system. 我想避免数据重复(
std::string
> QString
等),因此我尝试直接在Q_PROPERTY
系统中使用std::string
。 Using Q_DECLARE_METATYPE(std::string)
and qRegisterMetaType<std::string>();
使用
Q_DECLARE_METATYPE(std::string)
和qRegisterMetaType<std::string>();
and declaring properties like: 并声明以下属性:
Q_PROPERTY(QString stldata READ stldata WRITE setSTLData NOTIFY stldataChanged)
makes my code compile, but QML still doesn't like std::string
s. 使我的代码可以编译,但是QML仍然不喜欢
std::string
。
Writing a text field with: 使用以下命令编写文本字段:
Text
{
text: myserviceinterface.stldata
}
produces a warning saying Unable to assign std::string to QString
, while appending an existing QML string: 产生警告,说在追加现有QML字符串时
Unable to assign std::string to QString
:
Text
{
text: "text: " + myserviceinterface.stldata
}
makes the Text
control display a weird QVariant(std::string)
. 使
Text
控件显示一个奇怪的QVariant(std::string)
。
What am I doing wrong? 我究竟做错了什么?
I am afraid you can't use std::string
directly within QML, you have to convert to QString
first. 恐怕您不能在QML中直接使用
std::string
,您必须先转换为QString
。
In general, you can only export two kinds of C++ types to QML: QObject-derived classes and some build-in value types, see this documentation page . 通常,您只能将两种C ++类型导出到QML:QObject派生的类和某些内置值类型,请参阅此文档页面 。 So using a
QString
as the type of a Q_PROPERTY only works because it has special built-in support in the QML engine. 因此,将
QString
用作Q_PROPERTY的类型仅能起作用,因为它在QML引擎中具有特殊的内置支持。 There is no way to add support for more of these value types like std::string
(*). 无法添加对更多此类值类型(如
std::string
(*))的支持。
Using qRegisterMetaType
also doesn't help: It merely registers std::string
as a meta type, which among others means it can be stored in a QVariant
. 使用
qRegisterMetaType
也无济于事:它仅将std::string
注册为元类型,这意味着可以将其存储在QVariant
。 It is (mostly) not relevant for QML integration. (大多数)与QML集成无关。
So have your getter return a QString
: 因此,让您的getter返回一个
QString
:
Q_PROPERTY(QString stldata READ stldata WRITE setSTLData NOTIFY stldataChanged)
QString stldata() const {
return QString::fromStdString(myStlString);
}
(*) Actually there is a way to add support for new value types, but that is private API in QML that needs to work directly with the underlying Javascript engine. (*)实际上,有一种方法可以添加对新值类型的支持,但这是QML中的专用API,需要直接与基础Javascript引擎一起使用。 The QtQuick module uses that private API to add support for eg the
QMatrix4x4
value type. QtQuick模块使用该专用API来添加对
QMatrix4x4
值类型的支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.