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. Object created from C++, display on QML side and delete from C++

Object created from C++, display on QML side and delete from C++

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 963 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.
  • E Offline
    E Offline
    Erakis
    wrote on last edited by Erakis
    #1

    Hi,

    Once my QML page finished to load I need to call a C++ function that will produce a model (list of item).
    Once the QML page is destroy, (ex : changing page), I want to free the object that has been allocated on the C++ side.

    The C++ side need to keep the ownership of the model has an engine can alter the list at any time. If the ownership is going to the QML side I would need a mechanism to detect when the list is going to be destroyed and stop updating it but I found this way more complicated.

    Here is an exemple of what I have yet :

    C++ Side :

    void CUIManager::createList(QQuickItem* pQuickItem)
    {
        CMyList* p = new CMyList(this);
        p->addItem(...);
        p->addItem(...);
        pQuickItem->setProperty("model", QVariant::fromValue(p));
    }
     
    void CUIManager::removeList(QQuickItem* pQuickItem)
    {
        CMyList* p = (CMyList *)pQuickItem->property("model");
        delete p; // I'm getting this compilation error : cannot convert from 'QVariant' to 'CMyList *'
    } 
    

    QML side :

        GridView {
            id : sectionMainMenuGrid
            Component.onCompleted: {
                uiManager.createList(sectionMainMenuGrid);
            }
    
            Component.onDestruction: {
                uiManager.removeList(sectionMainMenuGrid);
            }
        }
    

    By the way, is my reasoning is correct ? I'm open to any suggestions.

    Best regards,

    1 Reply Last reply
    0
    • E Offline
      E Offline
      Erakis
      wrote on last edited by Erakis
      #2

      Nevermind, I got it :)

      void CUIManager::removeList(QQuickItem* pQuickItem)
      {
           QVariant q = pQuickItem->property("model");
           CMyList* pMyList = (CMyList*)q.value<void *>();
           delete pMyList;
      } 
      

      Hope it will help someone else

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vladstelmahovsky
        wrote on last edited by
        #3

        Hi

        Why not just save pointer of allocated model?
        Also, consider to register the pointer at quick engine as CPP, other wise it might be destroed by JS GC in an inappropriate time

        br

        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