Is there any way to create QML component recursively? I am implementing a chat where user can resend messages to another user, so as a result I want to see message component containing another message. Like this:
>my message
>>forwarded message1
>>>forwarded message1.2
>>forwarded message2
But I don't know maximum depth of such forward messages, so I cannot just create prototypes for each situation.
For now I use Loader
component to load component recursively, but there is 2 bugs. Firstly, it is very slow, so if I had 100 messages in chat with a lot of forwarded messages, it took up to 2 seconds to load page.
Secondly, scrollbar behaviour is strange - Loader
component (as I understood) unload my component when it is hidden, so when I scroll overall height
changes.
Maybe someone know a good way to create items recursively, or some ideas on how to make that part of code work faster, and without scroll bugs?
By now, I have only one idea - move that component to C++ code, where I would be able to create item without Loader
component.
Even though a snippet would have helped, if I've understood right you are trying to dynamically create objects on demand, for you don't know how many of them you require.
Here a link to the documentation. You can do that by means of the methods
Qt.createComponent
andComponent.createObject
.It follows an example copied from the documentation above linked:
Do not rely on the code above anyway. I strongly suggest to read the documentation, because the example is really a simple one.
Anyway, if I had to do something similar I wouldn't spawn repeatedly objects by means of the functions above mentioned. Both the performance and the memory occupancy could suffer from that. Instead, try to get the best from more suitable and well known data structures like circular buffers: you can preallocate them, keep your code easy to understand and maintain as well as reduce risks at runtime.