Accessing QML ListModel-Items with C++



  • Hey Guys,

    After finishing my program with a regular Qt Gui I have to update it for tablets. One main feature is the possibility to rearrange the UI by Drag&Drop. There are 7 elements with each 3 pictures. These pictures shall be updated by c++ code.

    My problem is that I don't know how to access the different elements to change the pictures. This is what I've written:

    main.cpp
    @#include <QApplication>
    #include <QtCore>
    #include <QQuickItem>
    #include <QQuickView>
    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>

    #include "qtquick2applicationviewer.h"
    #include "gui.h"

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

    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile&#40;QStringLiteral("qml/QML-MRGalleyServer/main.qml"&#41;&#41;;
    viewer.show(&#41;;
    QQuickItem* item = viewer.rootObject();
    item->setProperty("galleyColor","#a6cea9"); // Works without problems
    
    QList<QObject*> objectList = viewer.findChildren<QObject*>("galley1"); // does not work
    for(int i = 0; i&lt;objectList.size();i++){
        if(objectList.at(i))
            objectList.at(i)-&gt;setProperty("nextpicture","Images/widget2.png");
    }
    
    
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    return app.exec&#40;&#41;;
    

    }@

    WidgetModel.qml
    @import QtQuick 2.0

    ListModel {
    ListElement {
    objectName: "galley1"
    hoverColor: "#5040e020"
    color: "transparent"
    galleyName: "Galley 1"
    nextpicture: "Images/widget1.png"
    currentpicture: "Images/widget1.png"
    lastpicture: "Images/widget1.png"
    }
    ListElement {
    objectName: "galley2"
    hoverColor: "#5040e020"
    color: "transparent"
    galleyName: "Galley 2"
    nextpicture: "Images/widget2.png"
    currentpicture: "Images/widget2.png"
    lastpicture: "Images/widget2.png"
    }
    }@

    main.qml
    @Rectangle{
    id: window

        property color galleyColor
    
        width: 1366
        height: 768
        color: "#00ff15"
    
        ListView {
            id: galleylist
            x: 0
            y: 0
            width: 1366
            height: 768
            boundsBehavior: Flickable.StopAtBounds
            z: 1
            anchors.horizontalCenterOffset: 50
            anchors.topMargin: 100
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.verticalCenter
            anchors.top: parent.top
            spacing: 10
            orientation: ListView.Horizontal
    
            displaced: Transition {
                NumberAnimation { properties: "x,y"; easing.type: Easing.OutQuad }
            }
    
            model: VisualDataModel {
                id: visualModel
                model: WidgetModel {
                    id: colormodel}
    
                delegate: MouseArea {
                    id: delegateRoot
    
                    property int visualIndex: VisualDataModel.itemsIndex
    
                    width: 170; height: 400
                    drag.target: icon
    
                    GalleyViewer {
                        objectName: model.objectName
                        id: icon
                        width: 170; height: 400
                        anchors {
                            horizontalCenter: parent.horizontalCenter;
                            verticalCenter: parent.verticalCenter
                        }
                        color: galleyColor
                        mouseOverColor: model.hoverColor
                        galleyName: model.galleyName
                        nextpicture: model.nextpicture
                        lastpicture: model.lastpicture
                        currentpicture: model.currentpicture
                        radius: 3
    
                        Drag.active: delegateRoot.drag.active
                        Drag.source: delegateRoot
                        Drag.hotSpot.x: 36
                        Drag.hotSpot.y: 36
    
                        states: [
                            State {
                                when: icon.Drag.active
                                ParentChange {
                                    target: icon
                                    parent: galleylist
                                }
    
                                AnchorChanges {
                                    target: icon;
                                    anchors.horizontalCenter: undefined;
                                    anchors.verticalCenter: undefined
                                }
                            }
                        ]
                    }
    
                    DropArea {
                        anchors { fill: parent; margins: 15 }
                        onEntered: visualModel.items.move(drag.source.visualIndex, delegateRoot.visualIndex&#41;
                    }
                }
            }
    }
    

    @

    Does anyone know where my fault is? Or is there a simpler solution? If anyone knows good examples for c++ programs with qml guis let me know =)

    Thank you!


Log in to reply
 

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