简体   繁体   English

将 Q_GADGET 存储在 ListModel 中,它会“忘记”它的方法

[英]Storing a Q_GADGET in a ListModel, it “forgets” about its methods

I have a error queue (as a C++ class available as a context property) that can regularly give me a list of Error values, which is a Q_GADGET.我有一个错误队列(作为 C++ class 可用作上下文属性),它可以定期给我一个Error值列表,它是一个 Q_GADGET。 The gadget has a set of properties, among them extTimeStamp which is also a custom Q_GADGET of type JSDate64 .该小工具具有一组属性,其中extTimeStamp也是 JSDate64 类型的自定义JSDate64

So when I get errors, I push the timestamp and messages into a ListModel like this因此,当我遇到错误时,我会将时间戳和消息推送到这样的ListModel

function collectErrors() {
   if( errorQueueController.drainable ) {
      for(const error of errorQueue.popErrors()) {
         console.log("Time: " + error.extTimeStamp.asDateTime())
         errorsModel.append({extTimeStamp: error.extTimeStamp, extMessage: error.extMessage})
      }
   }
}

As can be seen, I log the timestamp before the append and it is also shown in the Text delegate, which I connected to the ListModel可以看出,我在append之前记录了时间戳,它也显示在我连接到ListModelText委托中

delegate: Text {
   color: "white"
   text: model.extTimeStamp.asDateTime() + ": " + model.extMessage
}

But as it turns out, I only see this in the debug output:但事实证明,我只在调试 output 中看到这个:

qml: Time: Fri Nov 4 00:43:01 2016 GMT+0100 qml:时间:2016 年 11 月 4 日星期五 00:43:01 GMT+0100

qrc:/views/DummyView2.qml:352: TypeError: Property 'asDateTime' of object [object Object] is not a function qrc:/views/DummyView2.qml:352: TypeError: object [object Object] 的属性“asDateTime”不是 ZC1C425268E68385D1AB5074C17A94

Even though it is the same value, somehow the properties are gone when queried from within the delegate, I also registered a string converter for my Q_GADGET, and it works at the time of append , but when I convert extTimeStamp to a string in the delegate, I just get [object Object] .即使它是相同的值,当从委托中查询时,不知何故属性消失了,我还为我的 Q_GADGET 注册了一个字符串转换器,它在append时工作,但是当我将extTimeStamp转换为委托中的字符串时,我只是得到[object Object]

Further, I figured that if I enable dynamicRoles for the ListModel , everything works as expected.此外,我认为如果我为ListModel启用dynamicRoles ,一切都会按预期工作。 Since the performance is much worse with dynamicRoles , the Qt manual discourages from its use, unless it's absolutely required.由于dynamicRoles的性能要差得多,因此 Qt 手册不鼓励使用它,除非绝对需要。 But I don't understand what it has to do with that, since I'm not changing the data type of roles at all.但我不明白这与它有什么关系,因为我根本没有改变角色的数据类型。 The extTimeStamp role will always be my JSDate64 -Gadget! extTimeStamp角色将永远是我的JSDate64 -Gadget!

// Why does it work with "dynamicRoles: true"?
ListModel { id: errorsModel; dynamicRoles: true }

You're assigning your Q_GADGET to a ListElement .您将 Q_GADGET 分配给ListElement ListElement s have the annoying feature of looking like they hold properties like any other QML object, but they're not properties. ListElement有一个烦人的特性,看起来它们像任何其他 QML object 一样拥有属性,但它们不是属性。 They're "roles", and they can only store constant data, like strings.它们是“角色”,它们只能存储常量数据,比如字符串。 (See docs ). (见文档)。 So it must be automatically converting your Q_GADGET to something that it accepts, but is not really useful to you anymore.所以它必须自动将你的 Q_GADGET 转换为它接受的东西,但对你来说不再有用了。 I'm not exactly sure why dynamicRoles would make it work.我不确定为什么dynamicRoles会使它起作用。 Maybe that allows it to be more loose with the type of objects that it stores.也许这允许它对它存储的对象类型更加松散。

It looks to me like a possible solution for you could be to store the date as a string in your model, instead of as an object.在我看来,一个可能的解决方案是将日期存储为 model 中的字符串,而不是 object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM