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. Dynamically add animation to dynamically created QML component from C++
Forum Updated to NodeBB v4.3 + New Features

Dynamically add animation to dynamically created QML component from C++

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 3 Posters 1.2k Views 2 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.
  • C Offline
    C Offline
    ChrisTof
    wrote on last edited by ChrisTof
    #1

    I have a rectangle in rect.qml

    import QtQuick 2.0
    Rectangle{
        id: myRect;
       width: 100
       height: 100
       color: "red"
    }
    

    and animation in anim.qml:

    import QtQuick 2.12
    ScaleAnimator {
            id: rotationAnim
            target: myRect
            from: 0
            to: 1
            duration: 2000
    }
    

    I load the rectangle from c++:

    QQmlComponent component(qmlEngine, QUrl::fromLocalFile("rect.qml"));
    QQmlContext* rootItemContext = QQmlEngine::contextForObject(rootWindow);
    QQuickItem *object = qobject_cast<QQuickItem*>(component.beginCreate(rootItemContext));
    if(object != nullptr) {
        QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership);
        object->setParentItem(rootWindow->contentItem());
        object->setParent(qmlEngine);
        }
    }
    
    component.completeCreate();
    

    I can load the animation the same way, but obviously, I get the error:

    ReferenceError: myRect is not defined
    

    I want to keep a rectangle and animation in different files. I have a set of animations and depending on my need I want to append selected animation to a rectangle.
    Is there any way to refer to this rectangle from the animation or any way to append an animation to an existing object?

    1 Reply Last reply
    0
    • IntruderExcluderI Offline
      IntruderExcluderI Offline
      IntruderExcluder
      wrote on last edited by
      #2

      You can define ScaleAnimator inside your rectacgle, like in documentation.

      1 Reply Last reply
      1
      • Shrinidhi UpadhyayaS Offline
        Shrinidhi UpadhyayaS Offline
        Shrinidhi Upadhyaya
        wrote on last edited by Shrinidhi Upadhyaya
        #3

        Hi @ChrisTof , you can do like this:-

        Rectangle {
            id: myRect
            width: 100
            height: 100
            color: "red"
        
            Anim
            {
                target: myRect
            }
        }
        

        Note:- Remove target inside ScaleAnimator in anim.qml

        Shrinidhi Upadhyaya.
        Upvote the answer(s) that helped you to solve the issue.

        1 Reply Last reply
        1
        • C Offline
          C Offline
          ChrisTof
          wrote on last edited by ChrisTof
          #4

          Of course, I can define ScaleAnimator inside the rectangle. I did not express myself clearly. I have animation and rectangle in separate files, because:

          • I have a rectangle.
          • I have a set of animations.
            I create a rectangle and sometimes I want to append animation and sometimes I don't. Sometimes I append animation 1, and other time animation 2. I want to this dynamically.
          1 Reply Last reply
          0
          • IntruderExcluderI Offline
            IntruderExcluderI Offline
            IntruderExcluder
            wrote on last edited by
            #5

            Remove target property then and set running to false for your ScaleAnimator. Create Animator at C++ like Rectangle and do a bit magic like in example below:

                QQmlComponent rect(&engine, QUrl(QStringLiteral("qrc:/MyRect.qml")));
                QQuickItem* rectangle = qobject_cast<QQuickItem*>(rect.beginCreate(context));
                if (rectangle != nullptr) {
                    auto rootItem = engine.rootObjects()[0]->property("contentItem").value<QQuickItem*>();
                    rectangle->setParentItem(rootItem);
                    QQmlComponent a1(&engine, QUrl(QStringLiteral("qrc:/Anim1.qml")));
                    auto animation = a1.beginCreate(context);
                    rect.completeCreate();
                    animation->setParent(rectangle);
                    animation->setProperty("target", QVariant::fromValue(rectangle));
                    a1.completeCreate();
                    animation->setProperty("running", true);
                }
            
            C 1 Reply Last reply
            3
            • IntruderExcluderI IntruderExcluder

              Remove target property then and set running to false for your ScaleAnimator. Create Animator at C++ like Rectangle and do a bit magic like in example below:

                  QQmlComponent rect(&engine, QUrl(QStringLiteral("qrc:/MyRect.qml")));
                  QQuickItem* rectangle = qobject_cast<QQuickItem*>(rect.beginCreate(context));
                  if (rectangle != nullptr) {
                      auto rootItem = engine.rootObjects()[0]->property("contentItem").value<QQuickItem*>();
                      rectangle->setParentItem(rootItem);
                      QQmlComponent a1(&engine, QUrl(QStringLiteral("qrc:/Anim1.qml")));
                      auto animation = a1.beginCreate(context);
                      rect.completeCreate();
                      animation->setParent(rectangle);
                      animation->setProperty("target", QVariant::fromValue(rectangle));
                      a1.completeCreate();
                      animation->setProperty("running", true);
                  }
              
              C Offline
              C Offline
              ChrisTof
              wrote on last edited by
              #6

              @IntruderExcluder exactly what I needed. Thank you very much.

              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