QML : How to read a QList from C++

孤街醉人 提交于 2019-12-14 01:51:37

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/6438157/qml-how-to-read-a-qlist-from-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!