Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Reload QML Singleton instances
Forum Updated to NodeBB v4.3 + New Features

Reload QML Singleton instances

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 703 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sebastienc
    wrote on last edited by
    #1

    Hello,

    Is it possible to reload a past singleton instance with qmlRegisterSingletonType and use the new instance in QML?

    Exemple :

    // allocate example before the engine to ensure that it outlives it
    QScopedPointer<SingletonTypeExample> example(new SingletonTypeExample);
    QQmlEngine engine;
    
    // Third, register the singleton type provider with QML by calling this
    // function in an initialization function.
    qmlRegisterSingletonInstance("Qt.example.qobjectSingleton", 1, 0, "MyApi", example.get());
    
    engine.load("qrc:/MyApp.qml");
    
    example.reset(new SingletonTypeExample);
    

    I've already tried it and the object becomes undefined once it has been reset.

    Thank you

    1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      No it is not trivially possible.

      Even it if was your solution wouldn't work, you are resetting the scoped pointer but the qml engine is not aware of it, it only knows the raw pointer you passed to it and which you destroyed later when you resetted the scoped pointer.

      What you could do is expose a singleton with a myApi property, and when you want to change the myApi, do it and emits its notify signal.

      Alternatively you could reevaluate your needs. Why do you need to reload a singleton? That seems weird on a first look.

      1 Reply Last reply
      1
      • S Offline
        S Offline
        sebastienc
        wrote on last edited by
        #3

        Thank you for your response.

        I need to pass an instance of an object in QML and this instance can be replaced by another one in a particular situation.

        I found a way to do this by passing my instance through the QML context.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved