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. Is there a way to mark a slot or invokable to not give ownership to the qml engine?
Forum Updated to NodeBB v4.3 + New Features

Is there a way to mark a slot or invokable to not give ownership to the qml engine?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 2 Posters 211 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.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by
    #1

    I have a slot that returns a QObject*. I forgot that QML takes ownership of the object unless you tell the engine that you don't want that ie:

    if(m_engine){
            m_engine->setObjectOwnership(m_buttonList[index], QQmlEngine::CppOwnership);
    }
    

    This is a pain as I have to pollute my objects to get the engine. What I want is this:

    public slots:
        Q_CPPOWNED QObject* getObject();
    

    Some macro or something to mark the slot or invokable. I suppose this is probably not possible as it would need to find the info about the current engine.

    C++ is a perfectly valid school of magic.

    J.HilkJ 1 Reply Last reply
    0
    • fcarneyF fcarney

      I have a slot that returns a QObject*. I forgot that QML takes ownership of the object unless you tell the engine that you don't want that ie:

      if(m_engine){
              m_engine->setObjectOwnership(m_buttonList[index], QQmlEngine::CppOwnership);
      }
      

      This is a pain as I have to pollute my objects to get the engine. What I want is this:

      public slots:
          Q_CPPOWNED QObject* getObject();
      

      Some macro or something to mark the slot or invokable. I suppose this is probably not possible as it would need to find the info about the current engine.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @fcarney the qml engine only takes ownership on orphaned QObjects.

      make your getObject() function have a check for parent == nullptr, and if thats the case, call setParent to something valid


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • fcarneyF fcarney has marked this topic as solved on

      • Login

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