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. Singleton object which was provided as a model for a table view in qml destroyed during closing qml panel
Forum Updated to NodeBB v4.3 + New Features

Singleton object which was provided as a model for a table view in qml destroyed during closing qml panel

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 328 Views 1 Watching
  • 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.
  • darthanaD Offline
    darthanaD Offline
    darthana
    wrote on last edited by
    #1

    Hi,

    I have a class named FilterData inherited from QSortFilterProxyModel in C++ for filtering and sorting of a table.

    Created a singleton from FilterData for accessing it in qml.
    qmlRegisterSingletonType<FilterData >(uri, 1, 0, ensureUpperCaseLetter.toLatin1().data(), FilterData::getInstance);

    In MyView.qml, I created a table view and provided this singleton as a model to the view

        TableView {
            anchors.fill: idTableRect
    
            model: TableViewSingleton
    
            delegate: Rectangle {...}
        ...
        ...
        }
    

    I have a list of qmls and loading them using panelLoader. When I load MyView.qml for the first time, the view loads perfectly. All sorting and filtering options works as expected.

    When I close the view. i.e., removing it from the panel loader, the destructor of FilterData is being called and the singleton object is destroyed.

    Can't able to figure out the reason for singleton destruction as no explicit call was made to destroy. For other panels which I unload, I see no destructor is being called. Issue occurs only with this qml.

    When I load MyView.qml for the next time, the program crashes as no singleton object is associated to "TableViewSingleton"

    Can anyone help me on this?

    Note: I see this issue occurs only with 5.14.2 and not with 5.12.12

    sierdzioS 1 Reply Last reply
    0
    • darthanaD darthana

      Hi,

      I have a class named FilterData inherited from QSortFilterProxyModel in C++ for filtering and sorting of a table.

      Created a singleton from FilterData for accessing it in qml.
      qmlRegisterSingletonType<FilterData >(uri, 1, 0, ensureUpperCaseLetter.toLatin1().data(), FilterData::getInstance);

      In MyView.qml, I created a table view and provided this singleton as a model to the view

          TableView {
              anchors.fill: idTableRect
      
              model: TableViewSingleton
      
              delegate: Rectangle {...}
          ...
          ...
          }
      

      I have a list of qmls and loading them using panelLoader. When I load MyView.qml for the first time, the view loads perfectly. All sorting and filtering options works as expected.

      When I close the view. i.e., removing it from the panel loader, the destructor of FilterData is being called and the singleton object is destroyed.

      Can't able to figure out the reason for singleton destruction as no explicit call was made to destroy. For other panels which I unload, I see no destructor is being called. Issue occurs only with this qml.

      When I load MyView.qml for the next time, the program crashes as no singleton object is associated to "TableViewSingleton"

      Can anyone help me on this?

      Note: I see this issue occurs only with 5.14.2 and not with 5.12.12

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @darthana you need to set the object to Cpp ownership:

      NOTE: A QObject singleton type instance returned from a singleton type provider is owned by the QML engine unless the object has explicit QQmlEngine::CppOwnership flag set.

      For example:

      QJSEngine::setObjectOwnership(instance, QJSEngine::CppOwnership);
      

      (Z(:^

      darthanaD 1 Reply Last reply
      1
      • sierdzioS sierdzio

        @darthana you need to set the object to Cpp ownership:

        NOTE: A QObject singleton type instance returned from a singleton type provider is owned by the QML engine unless the object has explicit QQmlEngine::CppOwnership flag set.

        For example:

        QJSEngine::setObjectOwnership(instance, QJSEngine::CppOwnership);
        
        darthanaD Offline
        darthanaD Offline
        darthana
        wrote on last edited by
        #3

        @sierdzio But if the object is created in C++ and shared to JavaScript, by default the memory management of the QObject remains in C++. Do we need to explicitly set object ownership?

        Also I dont find setObjectOwnership() under QJSEngine.

        sierdzioS 1 Reply Last reply
        0
        • darthanaD darthana

          @sierdzio But if the object is created in C++ and shared to JavaScript, by default the memory management of the QObject remains in C++. Do we need to explicitly set object ownership?

          Also I dont find setObjectOwnership() under QJSEngine.

          sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          @darthana said in Singleton object which was provided as a model for a table view in qml destroyed during closing qml panel:

          @sierdzio But if the object is created in C++ and shared to JavaScript, by default the memory management of the QObject remains in C++. Do we need to explicitly set object ownership?

          Yes you need to set it explicitly. The documentation clearly states: a singleton type provider is owned by the QML engine.

          Also I dont find setObjectOwnership() under QJSEngine.

          In Qt 5 it is under QQmlEngine

          (Z(:^

          1 Reply Last reply
          1

          • Login

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