On 23/04/2020 10:50, michael fuchs wrote: > Am 23.04.2020 08:59 schrieb David Llewellyn-Jones: >> On 23/04/2020 09:51, Michael Fuchs wrote: >>> 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. >> >> This doesn't look like a bad approach to me (although passing the >> controller itself might be cleaner). >> >> An alternative might be to make Controller a singleton, that way your >> AlbumList class could access it from the C++ without needing to be >> explicitly passed a handle to it. >> >> But whether you can do that depends on the how Controller is used. > > Thanks a lot. > I'm not familiar with singletons, but this sounds promising. > I found this simple example: > > https://gist.github.com/pazdera/1098119
Yeah, this looks like a decent example: clear and complete. As you can see from this, you essentially end up with a global object you can access from anywhere. The downside is that you only ever have one of them, so if you need more than one Controller, or (as Julian mentions) think it might be needed in future, then this probably isn't the right approach. > I hope this also works for Q_OBJECT s. > Maybe I should rather make netAccess and imgDB itself singletons? I don't recall any issues with Q_OBJECTS being singletons, however if you plan to use it in your QML you may also need to do some work registering it as a singleton there too. There's an example here: https://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterSingletonType But I wouldn't worry about this unless you're already registering your Controller as a QML type and need to use it from QML. As Julien also pointed out, mixing singletons in C++ and QML adds complexity that can lead to errors. David -- Website: https://www.flypig.co.uk _______________________________________________ SailfishOS.org Devel mailing list To unsubscribe, please send a mail to devel-unsubscr...@lists.sailfishos.org