QML i C++ - komunikacja

0

Witam, mam kod qml wyświetlający qui programu, tylko nie wiem jak w kodzie C++ pobrać np. który element ListView jest zaznaczony :

Oto kod QML :

import QtQuick 2.5
import QtQuick.Window 2.2

Window {
    width: 1920
    height: 1080
    visible: true
    visibility: "FullScreen"

    MouseArea {
        anchors.rightMargin: 0
        anchors.bottomMargin: 0
        anchors.leftMargin: 0
        anchors.topMargin: 0
        anchors.fill: parent
        onClicked: {
            Qt.quit();
        }

        Component{
            id: highlightBar
            Rectangle {
                id: zaz
                width: 160
                height: 40
                y: listView1.currentItem.y
                radius: 10
                color: 'lightgrey'
            }
        }

        ListView {
            id: listView1
            x: -5
            y: 0
            width: 110
            height: 512
            antialiasing: false
            transformOrigin: Item.Center

            highlight: highlightBar
            highlightFollowsCurrentItem: false

            model: ListModel {
                id: lvItems
                ListElement {
                    name: "System"
                    imagesSource: "qrc:/images/linux.png"
                }

                ListElement {
                    name: "Procesor"
                    imagesSource: "qrc:/images/cpu.png"
                }

                ListElement {
                    name: "Pamięć RAM"
                    imagesSource: "qrc:/images/ram.png"
                }

                ListElement {
                    name: "Sieć LAN"
                    imagesSource: "qrc:/images/lan.png"
                }
            }

            delegate: Item {
                x: 5
                width: 80
                height: 40
                Row {
                    id: row1

                    Rectangle {
                        width: 40
                        height: 40

                        Image {
                            anchors.fill: parent
                            fillMode: Image.PreserveAspectFit
                            source: imagesSource
                        }

                        MouseArea {
                            anchors.fill: parent
                            onClicked: {
                                console.log("clicked")
                                listView1.currentIndex = index

                            }
                        }
                    }

                    Text {
                        text: name
                        font.bold: true
                        anchors.verticalCenter: parent.verticalCenter

                        MouseArea {
                            anchors.fill: parent
                            onClicked: {
                                console.log("clicked")
                                listView1.currentIndex = index
                            }
                        }
                    }
                    spacing: 10
                }
            }

            onCurrentIndexChanged: {
                console.log(lvItems.get(currentIndex).name)
            }
        }
    }
}
0

Próbowałem tak, ale nie działa :

#include <QGuiApplication>
#include <QQmlApplicationEngine>

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

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    QObject rootObject = dynamic_cast<QObject>(engine.rootObjects());
    QObject lv = rootObject.findChild<QObject>("listView1");

    lv.property("currentItem");

    return app.exec();
}

0

??

1 użytkowników online, w tym zalogowanych: 0, gości: 1