0

简短的

是否可以在 a的值readonly property var内创建与数据的绑定?ListElementListModel

(或者是否有一些等效的途径来获取readonly包含此数据的结构化对象,而无需写入 areadonly property ListModelget(...)函数?)


背景/完整版

我正在使用qml. 这个已安装的项目大量使用了pragma Singleton前端/QtObject后端模式,其中前端的数据是,后端通过条件分配的w/readonly有条件地加载,而合作伙伴运行时项目则使用调用命令后端更改数据(必要时)。这样做是为了帮助混淆后端是从硬件读取的真实数据,还是合作伙伴项目使用运行时代码设置的模拟数据。另一个好处是能够在运行时从硬件输入动态切换到软件模型。Loadersourceswitchfunction

基本模式是:

SomeFeatureFrontEnd.qml

pragma Singleton
import QtQuick 2.0

QtObject {
   id: someFeatureFrontEnd

   readonly property string _backend: "fake"
   readonly property double fraction: _backend.item.fraction
   readonly property int counter: _backend.item.counter

   //... etc ...

   function setFraction(value) { _backend.item.setFraction(value) }

   function incrementCounter() { _backend.item.incrementCounter() }

   function decrementCounter() { _backend.item.decrementCounter() }

   //... etc ...

    _backend: Loader {
        id: _backend
        source: {
            switch(backend) {
                case "fake":
                default:
                   return "backends/SomeFeatureBackendFake.qml"
            }
        }
    }  
}

后端/SomeFeatureBackendApi.qml

import QtQuick 2.0

//This is just an interface for the derived backend children 
//  not actually instantiation.
QtObject {
   property double fraction
   property int counter

   //... etc ...

   function setFraction(value) { }

   function incrementCounter() { }

   function decrementCounter() { }

   //... etc ...
}

后端/SomeFeatureBackendFake.qml

import QtQuick 2.0

SomeFeatureBackendApi {
   fraction: 1.0
   counter: 0

   //... etc ...

   function setFraction(value) { fraction = value }

   function incrementCounter() { counter++ }

   function decrementCounter() { counter-- }

   //... etc ...
}

现在,在该一般策略中,我遇到了一种困境,即我想以counterfraction上述相同的方式添加一些动态分配的结构化数据。一个看似直观的拟合似乎是ListModel因为它支持结构化的、可修改的数据。

关于 ListModel 的一些细节:

  • 整数索引。
  • ListElement数据是所有基本类型real/double

或者如果更方便,这里有一个例子:

事物列表.qml

import QtQuick 2.0

ListModel {
   //Initializer code to dynamically initialize w/ an
   //arbitrary number of ThingEntry instances...
}

事物入口.qml

import QtQuick 2.0

ListElement {
   property double profileValue
   property int profileScore
}

如果您对上面的混淆代码片段有任何疑问,请告诉我。

现在根据简介重述问题/困境,关于在ListModel我的后端/前端模式中使用它:

根据我的理解readonly property ListModel structuredStuff,仍然可以通过structuredStuff.get()调用修改,对吧?我不希望那样...readonly出于上述原因,我希望前端的任何内容都是如此。

基于后端的非只读,我如何方便有效地获得类似于前端的readonly表示?varproperty ListModel

4

1 回答 1

0

我已经基于 qml 针对这个问题开发了一个框架。基本问题是listmodel必须包含直接数据,而不是对象,在我的框架中,listmodel可以包含任何qt对象,它支持数据绑定,即listmodel数据可以绑定到其他qt data/var。

于 2018-08-17T00:00:27.097 回答