C++ 将QString的可写数组作为属性的自定义QML对象
我正在尝试创建一个自定义QML对象的属性,该对象将是一个QString数组。这些文件路径将从QML传递到C++代码。 当我尝试写入对象时,会出现以下错误:C++ 将QString的可写数组作为属性的自定义QML对象,c++,qml,qt5,C++,Qml,Qt5,我正在尝试创建一个自定义QML对象的属性,该对象将是一个QString数组。这些文件路径将从QML传递到C++代码。 当我尝试写入对象时,会出现以下错误: portal.qml:56: Unable to assign QVariantList to [unknown property type] OgreResourceGroup { locations: [ "C:\Client\resources" ] } 引发错误的qml: portal.qml:56: Unable to
portal.qml:56: Unable to assign QVariantList to [unknown property type]
OgreResourceGroup
{
locations: [ "C:\Client\resources" ]
}
引发错误的qml:
portal.qml:56: Unable to assign QVariantList to [unknown property type]
OgreResourceGroup
{
locations: [ "C:\Client\resources" ]
}
位置的属性声明:
Q_PROPERTY( QQmlListProperty<QString> locations READ getLocations WRITE setLocations NOTIFY locationsChanged )
Q_属性(QQmlListProperty位置读取getLocations写入setLocations通知位置更改)
自定义对象中的C++编写方法:
void setLocations( const QQmlListProperty<QString>& list );
void设置位置(const qqmlistproperty&list);
javascript是在创建变量数组而不是qString数组吗?
如果是这样,您如何实例化一个QString
我看过的Qt示例中没有一个实现了可写列表
如果您有任何建议,我们将不胜感激。Qt文档中有一句话表明无法做到这一点:
Generally this constructor should not be used in production code, as a writable QList violates QML's memory management rules.
:(首先,您不能让“qqmlistproperty”可写。它只是qmlingine写入的一个特殊对象,应该被视为引用 其次,您只能使用从QObject派生的类型,而QString不能 Javascript引擎在内部保留所有JSON对象,当存在assign to属性时,会将其转换为QVariant(数组的类型为QMetaType::QVariantList,对象的类型为QMetaType::QVariantMap)
您应该只使用QVariant、QVariantList或QStringList。QString已在QML引擎中注册。他们说它在javascript中可用,但我从未见过实例化的能力QString@JayQString被转换为JavaScript本机字符串。