Control qml ListView from C++



  • Hi there,

    I have two qml files from a designer (not me, I am not firm with qml). The loaded qml file is called main.qml the other mylistview.qml.

    @import QtQuick 1.1

    Item {
    id: root
    anchors.fill: parent

    Item {
        id: mainViewArea
        anchors.fill: parent
    
        Background {
            id: background
        }
    
        ListView {
            id: listView@
    

    As you can think, the main.cpp loads the main.qml which includes the mylistview.qml that finally shows a ListView vertical. No Problem here.
    I use the following C++ code to load the QML. I cannot use QtQuick2 because no OpenGL on my embedded system:

    @QDeclarativeView view;
    view.setSource(QUrl::fromLocalFile(QCoreApplication::applicationDirPath() + QLatin1String("/main.qml")));@

    I want to call the "incrementCurrentIndex" funktion of my listbox, but how?
    Can I search subfunktions with this:

    @// get root object
    QObject *rootObject = dynamic_cast<QObject *>(view.rootObject());
    //Invoke
    QMetaObject::invokeMethod(
    rootObject->findChild<QObject *>("listView"), // QML item
    "incrementCurrentIndex" // name of the method to call
    // without parameters??
    );@

    How do I call this in C++ after Invoke? Will the invoke find the mylistview.qml file? Do it find the increment function of a qml object?



  • Hi,

    First, to use findChild you need set your component's property "componentName" and use this name to find, like this:

    QML File:
    @ import QtQuick 1.1

    Item {
        id: root
        anchors.fill: parent
     
        Item {
            id: mainViewArea
            anchors.fill: parent
     
            Background {
                id: background
            }
     
            ListView {
                objectName: "listView"
                id: listView
    

    @

    CPP file:
    @ // get root object
    QObject *rootObject = dynamic_cast<QObject *>(view.rootObject());
    //Invoke
    QObject *listViewObject = rootObject->findChild<QObject *>("listView");
    @

    Second, to call the function, if it needs parameter you need send it, because if you dont send it will raise a error telling no function was found. However, "incrementCurrentIndex" function doesn't need any parameter, so you don't need send parameters. Therefore you can call the way you were doing.

    @QMetaObject::invokeMethod(
    listViewObject, // QML item
    "incrementCurrentIndex" // name of the method to call
    );
    @

    Also you can set the currentIndex property like this:

    @listViewObject.setProperty("currentIndex", 1);@

    []'s



  • Ahhh, object name was the missing part ;)
    ID, model name etc. was tested, but object name is new to me.
    Is it possible to search for ID instead of object name?

    Connections are also possible, but I do not want to change the qml code so much.


Log in to reply
 

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