QML:如何从 C++ 读取 QList

发布于 2024-11-16 18:03:08 字数 563 浏览 1 评论 0原文

我有一个简单的需求:我定义了一个 C++ 类

class MyClass: public QDeclarativeItem
{
  Q_OBJECT
  public:
    MyClass(QDeclarativeItem * parent=0);
    ...

  private:
    QList<QString> mList
}

当然,我已经注册了它: qmlRegisterType(...)

我想在 QML 代码中访问我的 QList; mList。我该怎么做呢?

这让我很恼火,因为它看起来像一个简单的问题,但我找不到任何关于此的信息。 (我可以创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执手闯天涯 2024-11-23 18:03:08

我不认为 QList 是 Qt 的 QML 绑定支持的数据类型。我在使用 QtWebkit Bridge 在 C++ 和 JavaScript 之间进行接口时也遇到过类似的问题。

如果可能,请尝试使用 QVariantList 而不是 QList。尽管这在技术上是 QList的 typedef,但我认为它应该可以工作。

I 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 a QList. Although this is technically a typedef for QList<QVariant> I think it should work.

坠似风落 2024-11-23 18:03:08

如果您的列表中需要自定义类型(不仅仅是字符串或其他基本类型),则 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文