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. Are there gotchas with creating an instance of a QQuickItem subclass which instance I don't intend to render or add to a QML tree?
QtWS25 Last Chance

Are there gotchas with creating an instance of a QQuickItem subclass which instance I don't intend to render or add to a QML tree?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
c++qquickitem
2 Posts 2 Posters 1.1k 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.
  • Stefan Monov76S Offline
    Stefan Monov76S Offline
    Stefan Monov76
    wrote on last edited by Stefan Monov76
    #1

    As you know, a QQuickFramebufferObject::Renderer subclass shouldn't access its parent item's properties directly, and instead should copy them to itself in its synchronize() method. So the code tends to look like this (using the AUTO_PROPERTY macro from here):

    class MyItem : public QQuickFramebufferObject {
        AUTO_PROPERTY(int, foo)
        AUTO_PROPERTY(float, bar)
        // ...
    };
    
    class MyItemRenderer : public QQuickFramebufferObject::Renderer {
    public:
        void synchronize(QQuickFrameBufferObject* qqfbo) {
            auto src = (MyItem*)qqfbo;
            foo = src->foo();
            bar = src->bar();
        }
    private:
        int foo;
        float bar;
        // ...
    };
    

    I wanted to avoid duplicating the declaration of properties between the two classes, so right now I'm implementing this alternative solution:

    class MyItem : public QQuickFramebufferObject {
        AUTO_PROPERTY(int, foo)
        AUTO_PROPERTY(float, bar)
        // ...
    };
    
    class MyItemRenderer : public QQuickFramebufferObject::Renderer {
    public:
        MyItemRenderer() {
            copiedData = new MyItem();
        }
        void synchronize(QQuickFrameBufferObject* qqfbo) {
            auto src = (MyItem*)qqfbo;
            copiedData.setFoo(src->foo());
            copiedData.setBar(src->bar());
        }
    private:
        MyItem* copiedData;
    };
    

    I still need to write out and maintain the copying code as you see, but it's better than the other way.

    Are there gotchas with doing that? (creating an instance of a QQuickItem subclass which instance I don't intend to render or add to a QML tree).

    Quote from the docs:

    This is useful for creating QML objects from C++ code, whether to display a QML object that can be visually rendered, or to integrate non-visual QML object data into a C++ application.

    This seems to imply that such nonvisual usage is an intended and supported use case. But I'm not sure I'm reading it right.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      It's not intended, but should work. Just remember that you get a lot of overhead from QQuickItem and QObject with this approach, these classes are not tiny.

      (Z(:^

      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