Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Control qml ListView from C++
Forum Updated to NodeBB v4.3 + New Features

Control qml ListView from C++

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sirnoname
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      leonardo.ramosantos
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sirnoname
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved