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. Creating QML elements at runtime
QtWS25 Last Chance

Creating QML elements at runtime

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 4 Posters 3.7k 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.
  • K Offline
    K Offline
    kuschky
    wrote on last edited by
    #1

    Hello, I have some already existing QObject based classes representing some data I want display as graphic in QML.

    For that I change my class defintion so that my classes now derived from QDeclarativeItem and providing a implementation for the paint method and make them accessible by qmlRegisterType.
    @
    class Car : public QDeclarativeItem
    {
    Q_OBJECT

    Q_PROPERTY(int wheels READ wheels WRITE setWheels NOTIFY wheelsChanged)
    

    ....

    }
    @
    I'm now able to embed these objects into QML:
    @
    Rectangle {
    car {
    id: car1
    wheels: 1
    }

    car {
    id: car2
    wheels: 1
    }
    }
    @
    That's fine and works.

    But is there a way to create those kinds of objects on the C++ side and add them on a dynamic way at runtime to a QML context or to replace an already existing QML element with them? What want to do is to add another Car Object / Element with id car3 or replace an existing Car element with a one created in C++?

    Thanks & best regards Michael

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dmcr
      wrote on last edited by
      #2

      If the context that you associated to your QML contains an object car

      @ Q_PROPERTY(Car car READ getCarte WRITE setCar NOTIFY carChanged)@

      then when you update the object, the QML will be noticed
      with the event carChanged

      If you want many of them then you can use QDeclarativeListeProperty.

      dmcr

      1 Reply Last reply
      0
      • D Offline
        D Offline
        drixs
        wrote on last edited by
        #3

        if i have QML declarations lets say Book.qml and Paper.qml, and i have set Book.qml as QDeclarativeView source.

        how i can create and store some "paper" as child of "Book" on the C++ side at runtime?
        thanks in advance :D

        1 Reply Last reply
        0
        • D Offline
          D Offline
          diro
          wrote on last edited by
          #4

          Two solutions for add object to QML context:

          1. QDeclarativeContext::setContextProperty()
            "http://doc.qt.nokia.com/stable/qdeclarativecontext.html":http://doc.qt.nokia.com/stable/qdeclarativecontext.html

          2. QDeclarativePropertyMap class
            "http://doc.qt.nokia.com/4.7-snapshot/qdeclarativepropertymap.html":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativepropertymap.html

          The method 1 is good for few objects, and method 2 is good for object array.

          For your case, if you want to the car(s) display in the Rectangle dynamically, I think "DataModel":http://doc.qt.nokia.com/latest/qdeclarativemodels.html#exposing-c-data-models-to-qml + "ListView":http://doc.qt.nokia.com/4.7-snapshot/qml-listview.html element would be the best solution.

          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