1
votes

In my application ( QML, QtQuck 1.1, C++ ) I want to reuse some items in my screen.

Generally there are two options ( please let me know if there are more)

1.Use a separate .qml file and put the basic block inside and import in new screen
2.Create a component and use loader to load it.

But I cannot understand which option to use.

Whats the difference between these two options.

Or

which will consume less CPU load?

Thanks!!

3
It doesn't really matter. Both work the same way. Using a loader might use a few hundred bytes more memory.dtech

3 Answers

3
votes

Use the first approach unless:

  1. You need to conditionally load QML based on some runtime condition. Note that file selectors are more efficient than Loaders for this purpose, but it might not be possible to use them, depending on your requirements.
  2. You want to avoid loading expensive QML until absolutely necessary (lazy loading).

You should especially be wary of using Loader to load components that are created in large numbers (such as those used as delegates in views), as it doesn't come for free.

1
votes

To clarify on a part of the answer provided by Mitch:

You should especially be wary of using Loader to load components that are created in large numbers (such as those used as delegates in views), as it doesn't come for free.

Using a Loaderas a delegate just to create some other object doesn't make sense except for a very narrow case. You can directly and most definitely should use YourItem {} as a delegate instead. Delaying instantiation in a view delegate in particular is not a very good idea, since that will mess up the view layout, absent the actual item's dimensions which can't be determined without loading it. Even in the case where the Loader is not the top element but nested, it won't be problematic, as QML views only instantiate delegate objects for objects that are in view, if your model has 10k objects it is not like you will have 10k loaders unless they are all in view, which ... won't me much of a view.

Also, if you really need dynamic instantiation, then you will need at least an Item to use as a parent anyway, so the overhead of the Loader becomes negligible and a very good trade-off for the extra flexibility and functionality such as use bindings for the item or to set a dynamic source component. You could do that with an Item but you will have to implement it yourself, and in the end it will end up with larger overhead than Loader.

The CPU time for either of the approaches will not make a difference. Now if you are dynamically creating lots of objects manually, you definitely neither need nor should be using loaders. And not because of the CPU time, but because of memory usage - QML objects are memory hogs, a few thousand objects of medium complexity will likely run you into serious trouble on a 32bit build, especially on a mobile device. Even an empty, non visible QtObject is like 160 bytes. On top of that JS garbage collection is pretty much a joke when it comes to releasing resources, it will do fine on reusing. If you for example create 1 GB of visual items, then delete them and force garbage collection, you will only get a tiny fraction of that memory back, but if you create the objects again, memory usage will be pretty much the same +/- a few megabytes.

0
votes

In short, in separate .qml file put global component wich you will include and use in different files. Create component in same file if you will use it only in one file.

Also it depends on the size and complexity of the component. Big and complex component preferably place to separate file.

Compiling faster when all code in one file, but development faster when everything in their place.