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. Alternative method for qmlRegisterSingletonInstance method in qt 5.12.9.
QtWS25 Last Chance

Alternative method for qmlRegisterSingletonInstance method in qt 5.12.9.

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlregistertype5.12.95.15.0backward compat
2 Posts 1 Posters 933 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.
  • A Offline
    A Offline
    Ashvit
    wrote on last edited by Ashvit
    #1

    I have a project which I need to compile in both 5.15.0 for an embedded system and 5.12.9 for android (due to api level constraints used in the device).
    I started the development using 5.15.0 and have used the neat function qmlRegisterSingletonInstance to pass the data to the qml only to realise that it is not supported in 5.12.9.
    The data I am passing has a Q_PROPERTY(QList<QList<CustomClass>>) which is being used as a model in the repeater and it works in case of qmlRegisterSingletonInstance.
    For 5.12.9 I have tried using qmlRegisterSingletonType and qmlRegisterUncreatableType along with rootContext->setContextProperty. Using these methods the repeater shows count as 0.
    Is there any way I can copy the functionality provided by qmlRegisterSingletonInstance in 5.12.9?

    Minimal example:

    // In case of 5.15
    QScopedPointer dataHolderPtr<DataHolder>(new DataHolder);
    qmlRegisterSingletonInstance<DataHolder>("DataHolder", 1, 0, "DataHolder",dataHolderPtr->get());
    
    //In case of 5.12.9
    static QScopedPointer<DataHolder> *dataHolderPtr;
    int main(int argc, char *argv[]) {
    ...//Default code
    
    dataModelPtr= new QScopedPointer<DataHolder>(new DataHolder);
    qmlRegisterSingletonType<DataHolder>("DataHolder", 1, 0, "DataHolder",getDataHolder);
    QQmlContext *rootContext = engine.rootContext();
    rootContext->setContextProperty("DataHolder", dataHolderPtr->get());
    
    ...//Other stuff
    }
    static QObject* getDataModel(QQmlEngine *engine, QJSEngine *scriptEngine)
    {
        Q_UNUSED(engine)
        Q_UNUSED(scriptEngine)
        qDebug()<< Q_FUNC_INFO<<dataHolderPtr->get()->count()<<endl; //This is printing correct count here but the same property returns 0 in qml.
        return dataHolderPtr->get();
    }
    
    1 Reply Last reply
    0
    • A Offline
      A Offline
      Ashvit
      wrote on last edited by
      #2

      For someone facing a similar issue, I did not find a direct solution,

      According to the table
      https://doc.qt.io/qt-5/qtqml-cppintegration-data.html#sequence-type-to-javascript-array
      QList<CustomClass> is not yet supported in QML. Due to this reason I had to convert the QList<QList<CustomClass>> to QVariantMap<string,QVariantList<QVariantList<QVariantMap>>>

      And while using the values in qml use the bracket notation instead of the dot notation.

      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