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. qml and creating multiple c++ qml objects from one template object
Forum Updated to NodeBB v4.3 + New Features

qml and creating multiple c++ qml objects from one template object

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 3 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.
  • WalterWhiteW Offline
    WalterWhiteW Offline
    WalterWhite
    wrote on last edited by
    #1

    If I have a qml file that describes the look of an on/off button,
    how can I findObject with c++ and use it to create multiple QQuickItem objects?

    I know I can do it using this...

        m_pQmlComponent = new QQmlComponent(pEngine,QUrl(pszQmlFilename));
        if (m_pQmlComponent)
        {
            //create a dynamic instance
            QObject *pObject = m_pQmlComponent->create();
    

    but what I need is a way to create the component using only the object found using findChild off the root object.

    For example: If I had a On/Off menu item call on_off_item
    and I had a menu with
    item
    on_off_item
    item
    on_off_item

    and another menu with more on_off_items
    item
    on_off_item
    on_off_item

    so now my c++ code has multple objects that use on_off_item as a template.
    but on_off_item will be listed just once in the qml file.

    1 Reply Last reply
    0
    • 6thC6 Offline
      6thC6 Offline
      6thC
      wrote on last edited by
      #2

      I can't understand your use case - I tried twice - that's ok.

      I'd direct you to look at dynamic qml createComponent and maybe with incubate
      http://doc.qt.io/qt-5/qml-qtqml-component.html#incubateObject-method

      And http://doc.qt.io/qt-5/qtqml-cppintegration-definetypes.html

      I don't personally have c++ instances spawned from QML but it's certainly possible.

      If you just wish to display differently for what's on and off etc, have you seen states and transitions?
      http://doc.qt.io/qt-5/qml-tutorial3.html and
      https://qmlbook.github.io/en/ch05/index.html#states-and-transitions

      You might find you're making it more complicated (but again, I already know I don't understand what you want exactly).

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rweickelt
        wrote on last edited by
        #3

        Do you want to clone an existing object in the object tree of a QML document? Is it that what you want? To my best knowledge, this is only possible under the following circumstances:

        1. your object does not have any relationships with other objects, e.g. there are no bindings pointing to your object.
        2. your object does not have any signal/slot connections. Or at least, you will have to reproduce them somehow after cloning.

        When you clone the object, make sure that you keep the same parent as the original one and you are using exactly the same type. Use obj->metaObject() to find out its type and use QMetaObject::newInstance() to create a clone of the obj.

        I don't know if that works for QQuickItem though. Good luck!

        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