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 object dynamically

Creating QML object dynamically

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 4 Posters 4.2k 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.
  • Q Offline
    Q Offline
    qtNiks
    wrote on 13 Jul 2012, 08:56 last edited by
    #1

    Is there anyway for creating a QML object dynamically from the Qt code, without using the .qml file or JavaScripts?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AlterX
      wrote on 13 Jul 2012, 09:37 last edited by
      #2

      yes it is!

      @
      QDeclarativeItem *item = NULL;
      QDeclarativeComponent component(this->rootContext->engine(), QUrl("qrc:/my.qml"));

      item = qobject_cast<QDeclarativeItem *>(component.create());
      item->setProperty("property", "Hello");
      item->setParentItem(qobject_cast<QDeclarativeItem *>(parent));
      @

      You can safely pass a QML object (parent in the example) from within a QML file as parameter

      Qt Ambassador
      Real-time cooperative teams: http://www.softairrealfight.net
      Free Real-time network platform sdk: https://github.com/AlterX76/Solomon

      https://codereview.qt-project.org/...

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Skylar
        wrote on 13 Jul 2012, 09:40 last edited by
        #3

        There is a possibility to build QQmlExtensionPlugin to define your qmlfiles and refenrece them.

        Is it the pint you think about ?

        http://doc-snapshot.qt-project.org/5.0/qqmlextensionplugin.html

        There is the an example in the TimeExample in the QT5 sources.

        In most cases, the bug is mine. But of the remaining lines are correct.

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          qtNiks
          wrote on 19 Jul 2012, 07:05 last edited by
          #4

          I already have a qml file and i want to add this declarative item on that QML file and be able to change the property for that declarative item. How to do that?
          Below is the code snippet

          int main(int argc, char *argv[])
          {

          QApplication app(argc, argv);
          
          QmlApplicationViewer viewer;
          viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
          viewer.setMainQmlFile&#40;QLatin1String("qml/example_2/main.qml"&#41;);
          QObject *rootObjectA = viewer.rootObject();
          rootObjectA->setProperty("text1Text",QVariant("Change you text here..."));
          viewer.showExpanded();
          
          return app.exec();
          

          }

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AlterX
            wrote on 19 Jul 2012, 09:35 last edited by
            #5

            [quote author="qtNiks" date="1342681553"]I already have a qml file and i want to add this declarative item on that QML file and be able to change the property for that declarative item. How to do that?
            Below is the code snippet

            int main(int argc, char *argv[])
            {

            QApplication app(argc, argv);
            
            QmlApplicationViewer viewer;
            viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
            viewer.setMainQmlFile&#40;QLatin1String("qml/example_2/main.qml"&#41;&#41;;
            QObject *rootObjectA = viewer.rootObject();
            rootObjectA->setProperty("text1Text",QVariant("Change you text here..."));
            viewer.showExpanded();
            
            return app.exec();
            

            }

            [/quote]

            Maybe I didn't understand what you want to do...if you just want to change a property on rootObjectA, the code above is correct...

            Qt Ambassador
            Real-time cooperative teams: http://www.softairrealfight.net
            Free Real-time network platform sdk: https://github.com/AlterX76/Solomon

            https://codereview.qt-project.org/...

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MartinJ
              wrote on 20 Jul 2012, 03:03 last edited by
              #6

              I'm not sure I understand exactly what you're looking for either. Perhaps you want to create an object from QML defined entirely in C++. QQmlComponent::setData() does this, e.g.

              @
              QDeclarativeComponent component(engine);
              component.setData("import QtQuick 1.0; Rectangle { color: 'red'; width: 20; height: 20 }", QUrl());
              QObject *obj = component.create();
              @

              1 Reply Last reply
              0

              1/6

              13 Jul 2012, 08:56

              • Login

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