ListView not displaying KotoArtistModel items #32

Closed
opened 2024-10-27 19:53:01 +01:00 by g-fb · 1 comment
g-fb commented 2024-10-27 19:53:01 +01:00 (Migrated from github.com)

The problem is with the Cartographer singleton, which is created twice (resulting in 2 models too), once in cpp and once when used in qml as model for the ListView.

This happens because:

  1. the create function was commented out
  2. the create function only works with non default constructor

A default constructor is Cartographer(QObject* parent = nullptr).

Since you were using a default constructor, the create function was ignored and the default constructor was used when trying to access the singleton in qml, therefore the instance() function was not called and a new object is created. Changing the constructor to Cartographer(QObject* parent) is enough to force the use of the create function.

diff --git a/desktop/datalake/cartographer.cpp b/desktop/datalake/cartographer.cpp
index a5ae4b0..7f47b5e 100644
--- a/desktop/datalake/cartographer.cpp
+++ b/desktop/datalake/cartographer.cpp
@@ -10,7 +10,7 @@ Cartographer::Cartographer(QObject* parent)
       i_tracks(QHash<QUuid, KotoTrack*>()) {}
 
 Cartographer& Cartographer::instance() {
-  static Cartographer _instance;
+  static Cartographer _instance(nullptr);
   return _instance;
 }
 
diff --git a/desktop/datalake/cartographer.hpp b/desktop/datalake/cartographer.hpp
index baad892..2e93a27 100644
--- a/desktop/datalake/cartographer.hpp
+++ b/desktop/datalake/cartographer.hpp
@@ -20,12 +20,12 @@ class Cartographer : public QObject {
     // Q_PROPERTY(QQmlListProperty<KotoTrack*> tracks READ getTracksQml)
 
   public:
-    Cartographer(QObject* parent = nullptr);
+    Cartographer(QObject* parent);
     static Cartographer& instance();
-    //    static Cartographer* create(QQmlEngine* engine, QJSEngine*) {
-    //      engine->setObjectOwnership(&instance(), QQmlEngine::CppOwnership);
-    //      return &instance();
-    //    }
+    static Cartographer* create(QQmlEngine*, QJSEngine*) {
+        QQmlEngine::setObjectOwnership(&instance(), QQmlEngine::CppOwnership);
+        return &instance();
+    }
 
     void addAlbum(KotoAlbum* album);
     void addArtist(KotoArtist* artist);

The problem is with the Cartographer singleton, which is created twice (resulting in 2 models too), once in cpp and once when used in qml as model for the ListView. This happens because: 1. the create function was commented out 2. the create function only works with non default constructor A default constructor is `Cartographer(QObject* parent = nullptr)`. Since you were using a default constructor, the create function was ignored and the default constructor was used when trying to access the singleton in qml, therefore the instance() function was not called and a new object is created. Changing the constructor to `Cartographer(QObject* parent)` is enough to force the use of the create function. ```diff diff --git a/desktop/datalake/cartographer.cpp b/desktop/datalake/cartographer.cpp index a5ae4b0..7f47b5e 100644 --- a/desktop/datalake/cartographer.cpp +++ b/desktop/datalake/cartographer.cpp @@ -10,7 +10,7 @@ Cartographer::Cartographer(QObject* parent) i_tracks(QHash<QUuid, KotoTrack*>()) {} Cartographer& Cartographer::instance() { - static Cartographer _instance; + static Cartographer _instance(nullptr); return _instance; } diff --git a/desktop/datalake/cartographer.hpp b/desktop/datalake/cartographer.hpp index baad892..2e93a27 100644 --- a/desktop/datalake/cartographer.hpp +++ b/desktop/datalake/cartographer.hpp @@ -20,12 +20,12 @@ class Cartographer : public QObject { // Q_PROPERTY(QQmlListProperty<KotoTrack*> tracks READ getTracksQml) public: - Cartographer(QObject* parent = nullptr); + Cartographer(QObject* parent); static Cartographer& instance(); - // static Cartographer* create(QQmlEngine* engine, QJSEngine*) { - // engine->setObjectOwnership(&instance(), QQmlEngine::CppOwnership); - // return &instance(); - // } + static Cartographer* create(QQmlEngine*, QJSEngine*) { + QQmlEngine::setObjectOwnership(&instance(), QQmlEngine::CppOwnership); + return &instance(); + } void addAlbum(KotoAlbum* album); void addArtist(KotoArtist* artist); ```
JoshStrobl commented 2024-10-28 18:21:28 +01:00 (Migrated from github.com)

Thank you! Pushed new commit to branch that indeed fixes it 🎉

Thank you! Pushed new commit to branch that indeed fixes it :tada:
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: ModernDesktopInitiative/koto#32
No description provided.