[SOLVED] QQuickItem change property with animation



  • 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")
    }
    

    }@



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.