QQmlListProperty - writable QList violates QML's memory management rules?

拜拜、爱过 提交于 2019-12-11 07:28:22

问题


I've been using QQmlListProperty in a kind of a "master class" that is both in the same time a model and a qml list property, allowing the easy declarative markup of object backbones, which may or may not be modifiable as well as the runtime generation / consolidation of code from dynamic structure. I've shared something approximate in this answer.

I have zero idea of the actual implementation details, but what triggers my "spider sense" is the following bits of documentation:

QQmlListProperty::QQmlListProperty(QObject *object, QList<T *> &list) - the one I am currently using states that:

Generally this constructor should not be used in production code, as a writable QList violates QML's memory management rules.

The reason I implement QQmlListProperty is that I want to use the qtquick parser/factory or whatever to be able to create my model data from regular QML code. Naturally, the absence of insert and remove methods from the QQmlListProperty API on its own is not any cause of concern, as such functionality is not needed for the intended purpose, which is parse code and create objects as you go, for that appending suffices. The cause of my concern is the "writable QList violates QML's memory management rules" as the end result will later be modified. Not that appending doesn't qualify as writing too but still, I am worried about possible negative effects of "violating QML's memory management rules". Especially after the problems with objects being destroyed while still in use I stumbled upon.

In the general case, it seems that it is possible and not problematic to make runtime changes to the structure, created by parsing QML files, even if it has ids, for example:

  Rectangle {
    width: 50
    height: 50
    Rectangle {
      id: c
      width: 10
      height: 10
      color: "red"
    }
    Rectangle {
      y: 20
      width: 10
      height: 10
      color: "red"
    }
    Component.onCompleted: c.destroy()
  }

however in some cases it does say that the object can't be destroyed dynamically or something like that.

So am I safe to create object structures from QML files by implementing QQmlListProperty and making changes to those structures later on in light of the vague "writable QList violates QML's memory management rules" warning?

来源:https://stackoverflow.com/questions/39580659/qqmllistproperty-writable-qlist-violates-qmls-memory-management-rules

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