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. [SOLVED] QQuickItem change property with animation
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QQuickItem change property with animation

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 1 Posters 1.3k Views 1 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.
  • Y Offline
    Y Offline
    Yrchgrchh
    wrote on last edited by
    #1

    Hi All,

    I'm trying to create and manipulate QML components from within C++. In test app I simply want to change Rectangle's coordinates and make it animated.
    Although I see animation when set y in List's constructor for the first time it crashes in List::move in setProperty("y") if I have 'Behavior on y' in my qml. Furthermore if I comment it out I can't see onYChanged output from Qml. What's wrong with it?

    list.h
    @#ifndef LIST_H
    #define LIST_H

    #include <QQuickItem>
    #include <QList>

    class List : public QQuickItem
    {
    Q_OBJECT

    QList<QQuickItem*> m_items;
    

    public:
    explicit List(QQuickItem *parent = 0);

    void move(int x, int y);
    
    void keyPressEvent(QKeyEvent *event);
    

    };

    #endif // LIST_H@

    list.cpp
    @#include "list.h"
    #include <QQmlEngine>

    List::List(QQuickItem *parent) :
    QQuickItem(parent)
    {
    QQmlEngine engine;
    QQmlComponent component(&engine, QUrl::fromLocalFile("qml/epg/Cell.qml"));

    m_items.append( dynamic_cast<QQuickItem*>(component.create()) );
    m_items.back()->setParentItem(this);
    
    m_items.append( dynamic_cast<QQuickItem*>(component.create()) );
    m_items.back()->setParentItem(this);
    m_items.back()->setProperty("y",100);
    
    m_items.append( dynamic_cast<QQuickItem*>(component.create()) );
    m_items.back()->setParentItem(this);
    m_items.back()->setProperty("y",200);
    

    }

    void List::move(int x, int y)
    {
    foreach (QQuickItem* item, m_items) {
    item->setProperty("x", item->x() + x);
    item->setProperty("y", item->y() + y);
    }
    }

    void List::keyPressEvent(QKeyEvent *event)
    {
    qDebug() << "keyPressed";
    switch (event->key())
    {
    case Qt::Key_Up:
    move(0,-50);
    break;
    case Qt::Key_Down:
    move(0,50);
    break;
    }
    }@

    main.cpp
    @#include <QtGui/QGuiApplication>
    #include "qtquick2applicationviewer.h"
    #include "list.h"

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

    qmlRegisterType<List>("epg.list", 1, 0, "List");
    
    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile&#40;QStringLiteral("qml/epg/main.qml"&#41;&#41;;
    viewer.showExpanded();
    
    return app.exec&#40;&#41;;
    

    }@

    main.qml
    @import QtQuick 2.0
    import epg.list 1.0

    Rectangle {
    width: 360
    height: 360
    color:"blue"

    List{
        anchors.fill: parent
        focus:true
    }
    

    }@

    Cell.qml
    @import QtQuick 2.0

    Rectangle {
    width: 100
    height: 50

    color: "green"
    
    Behavior on y { NumberAnimation{duration: 400}}
    
    onYChanged: {
        console.log("y changed")
    }
    
    onXChanged: {
        console.log("x changed")
    }
    

    }@

    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      Yrchgrchh
      wrote on last edited by
      #2

      I solved it by adding initial value m_items.back()->setProperty("y",0); for the first element of my list.
      Not sure what type of magic is that but it works just fine now

      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