[Interest] copying the object in the delegate

Giuseppe D'Angelo giuseppe.dangelo at kdab.com
Mon Oct 21 00:43:06 CEST 2019


Il 19/10/19 23:48, Dirk Hohndel ha scritto:
> That is of course ridiculously inefficient. Even ignoring the issue on my
> side that forces me to run the constructor every time the data() function
> is called, it seems silly to not just be able to have a local copy of that
> object for the delegate QML object which then accesses the members of that
> object without calling back to the underlying model.
> Is there a way to do that and I just didn't find it when looking?

Note that, in general, a model's data() function is expected to be 
seriously hammered by a view. In Qt's model/view architecture, views and 
delegates don't cache anything. (This is brought quite to the extreme: 
it's not uncommon to see hundreds if not thousands of calls to data() 
from a widgets view simply by moving the mouse cursor over it.)

If there's need of caching because accessing the data is "slow", for any 
measure of slow, then this caching belongs to the model (or to the 
user's own delegates/views).

Thus, the way I see it, you have two options:

1) Add some caching to your model's data(), e.g. a very simple LRU cache 
of a reasonably small size, as proposed in the earlier reply.

2) Given that in QML you write the delegates yourself, maybe instead of 
using something like:

    delegate: MyDelegate {
       propA: model.role.fieldA
       propB: model.role.fieldB
       // etc

which will likely cause multiple calls to data() for the rolename "role" 
for the same delegate instance, did you try something like this?

    delegate: MyDelegate {
       readonly property QtObject myObject: model.role // or, "var"
       propA: myObject.fieldA
       propB: myObject.fieldB
       // etc.

Giuseppe D'Angelo | giuseppe.dangelo at kdab.com | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - The Qt, C++ and OpenGL Experts

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4329 bytes
Desc: Firma crittografica S/MIME
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20191021/6c741b3c/attachment.bin>

More information about the Interest mailing list