Your needs are very similar to mine in kontroller app( this is to be
expected, needs are very similar).

 

At first glance, i tried an approach using singleton objects, and qml
objects were internally just proxy over the c++ singletons. This proved to
be a total mess, not reliable (including several crashes due to c++ objects
not correctly exposed to qml) so i strongly recommend not going this way.

 

What does work, and is much more clean from a design point of view, is
exposing a « client » object (that would be your Controller if i
understand correctly) as a context property of the root page. This is what
you proposed :

====
AlbumList {
imgDB: controller.imgDB
netAccess: controller.netAccess
}
====

 

There is no way to make qml call a specific constructor when creating an
object (Qt 5.14 introduced required properties, so it may change in the
future, but i wouln't count on it in the near future. Maybe for Qt6).
However, the setProperty / refresh() in component.onCompleted event pattern
works pretty well. It may be considered a bit redundant, but if/when you
implement multiple servers and server switching, you'll be glad you went
that way.

 

On a different topic, i would make two additional remarks based on my
experience:

* i would't use AlbumList as the model for the list. More precisely, i
would use an AlbumService, which expose an albumList as a property (this is
what i do in Kontroller). I found that exposing a QVariantList of Q_GADGETS
gives pretty good result regarding performance (when qt gets upgraded, we
will be able to expose directly a QVector<Album>, but not yet), ie even
with more than 10k elements in list it works fine on a jolla 1.

* i'm not sure what your ImageDatabase is, but did you look at
QQuickImageProvider ?
(https://doc.qt.io/archives/qt-5.6/qquickimageprovider.html ). It could be
a better alternative (as it benefits a lot from internal qt caching /
optimizations).

 

Best regards,

 

Julien

 

"Michael Fuchs" mic...@gmx.at – 23 avril 2020 08:51
 

> Hi,
>
> ...I don't even know how to phrase the subject of this mail...
>
> I'm hacking on SMPC and I want to reorganize the spaghettis of the code
> and
> I'm stuck at a problem now, where I don't even know what to search for:
>
> I created a c++ class called AlbumList, and i registered it in qml, ...
>
> ====
> qmlRegisterType<AlbumList>("smpc", 1, 0, "AlbumList");
> ====
>
> ...so it can be instanciated there like this:
> ====
> ListModel {
> model: AlbumList { }
> }
> ====
>
> This works fine. Now that the object is instantiated it needs to fetch
> information from mpd about the albums and get the coverimages from a
> database.
> Both are instantiated in the "controller".
>
> ====
> Controller::Controller(....) {
> mImgDB = new ImageDatabase();
> mNetAccess = new NetworkAccess();
> }
> ====
>
> The controller gets created in main.cpp:
>
> ====
> Controller *control = new Controller(view,0);
> ====
>
> How can AlbumList access mImgDB and mNetAccess?
>
> One solution could be to register the controller itself in qml and then
> write
> sth like this in qml:
>
> ====
> AlbumList {
> imgDB: controller.imgDB
> netAccess: controller.netAccess
> }
> ====
>
> but it's redundant.
> Isn't there a way I can solve this inside c++?
> Do I have to register AlbumList differently in qml? Can I influence,
which
> constructor of AlbumModel is used by qml, so I can hand over netAccess
and
> imgDB as parameters.
>
> I hope I could make myself clear about this.
>
> Greetings, fooxl.
>
> _______________________________________________
> SailfishOS.org[1] Devel mailing list
> To unsubscribe, please send a mail to
> devel-unsubscr...@lists.sailfishos.org
>
>  



Links:
------
[1] http://SailfishOS.org
_______________________________________________
SailfishOS.org Devel mailing list
To unsubscribe, please send a mail to devel-unsubscr...@lists.sailfishos.org

Reply via email to