[solved] Flickable: Items added from C++ not moving



  • Hi,

    I am trying to add items from a c++ method to a Flickable component. Everything is working so far, but the newly added items don't move when flicking. Items added via qml behave as expected.
    The items from c++ are added statically at the position defined in the code.
    When debugging the c++ items appear as children of the qml Flickable.

    An excerpt from the code:

    @
    QQmlComponent event_component(qmlContext(ui_timeline)->engine(), QUrl("qrc:/Event.qml"));

    int event_count = model_events->rowCount(QModelIndex());
    int timeline_width = (event_count - 1) * window_width + window_width;
    ui_timeline->setProperty("contentWidth", timeline_width);
    
    for(int i=0; i<event_count; i++) {
        QQuickItem *event_view = qobject_cast<QQuickItem*>(event_component.create(qmlContext(ui_timeline)));
        event_view->setParentItem(ui_timeline);
        event_view->setX(130*i);
        event_view->setY(window_height/2 - event_view->height()/2 - 16);
    }
    

    @

    ui_timeline represents the Flickable
    event_view the added items from a qml file

    Is there anything wrong in the way I'm adding the items to Flickable?

    best regards


  • Moderators

    Hi,

    Since you are adding the Items dynamically you should reparent them to Flickable's contentItem.
    You can find the Flickable object in C++ using findChild(), then its first child would be the contentItem.
    Also contentWidth and contentHeight too should be set.



  • Hi,

    that worked perfectly, thanks!


  • Moderators

    Glad that it worked :)
    You can mark the post as solved by editing the post title and prepend [solved].


Log in to reply
 

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