QML:如何从 C++ 读取 QList
我有一个简单的需求:我定义了一个 C++ 类
class MyClass: public QDeclarativeItem
{
Q_OBJECT
public:
MyClass(QDeclarativeItem * parent=0);
...
private:
QList<QString> mList
}
当然,我已经注册了它: qmlRegisterType
我想在 QML 代码中访问我的 QList
。我该怎么做呢?
这让我很恼火,因为它看起来像一个简单的问题,但我找不到任何关于此的信息。 (我可以创建一个 Q_INVOKABLE 插槽,但我无法读取结果等...)
编辑: QML 支持的数据类型
I have a simple need : I have defined a C++ class
class MyClass: public QDeclarativeItem
{
Q_OBJECT
public:
MyClass(QDeclarativeItem * parent=0);
...
private:
QList<QString> mList
}
And of course, I've registered it : qmlRegisterType<MyClass>(...)
I want to access in the QML code to my QList<QString> mList
. How can I do it?
It annoys me as it looks like a simple problem, but I can't find anything about this. (I can create a Q_INVOKABLE slot, but I can't read the results, etc...)
Edit : QML supported Data Types
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为 QList 是 Qt 的 QML 绑定支持的数据类型。我在使用 QtWebkit Bridge 在 C++ 和 JavaScript 之间进行接口时也遇到过类似的问题。
如果可能,请尝试使用的 typedef,但我认为它应该可以工作。
QVariantList
而不是QList
。尽管这在技术上是 QListI don't think that
QList
is a supported data type for Qt's QML binding. I've had similar problems interfacing between C++ and JavaScript using the QtWebkit Bridge.If possible, try using a
QVariantList
instead of aQList
. Although this is technically a typedef forQList<QVariant>
I think it should work.如果您的列表中需要自定义类型(不仅仅是字符串或其他基本类型),则 QDeclarativeListProperty 适合这种情况。
但它更复杂,请参见 http://doc.qt .nokia.com/4.7/declarative-tutorials-extending-chapter5-listproperties.html
If you need a custom type in your list (not just strings or other basic types) there's QDeclarativeListProperty for that case.
But it's more complicated, see http://doc.qt.nokia.com/4.7/declarative-tutorials-extending-chapter5-listproperties.html