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. C++: how to call QML ListView selected item object?
Forum Updated to NodeBB v4.3 + New Features

C++: how to call QML ListView selected item object?

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 6.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.
  • K Offline
    K Offline
    kathy
    wrote on last edited by
    #1

    My QML file has a listview which use VisualItemModel to display each item (each item is a qml page). In my C++, how to get the selected object? In another word, how to get the selected item in C++?

    My list model:

    @VisualItemModel {
    property int itemwidth: 0
    property int itemheight: 0
    page1 {
    id: p1
    width: itemwidth
    height: itemheight
    }
    page2 {
    id: 2
    width: itemwidth
    height: itemheight
    }
    page3 {
    id: p3
    width: itemwidth
    height: itemheight
    }
    }@
    In the C++ code:

    @QObject rootObject = dynamic_cast<QObject>(viewer.rootObject());
    QObject lv = rootObject->findChild<QObject>("objectMyListView");@
    If I selected p1, then how to get p1 object? The page1 / page2 / page3 are 3 qml files in the same directory.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      aalpert
      wrote on last edited by
      #2

      lv->property("currentItem");

      But normally you want QML to be calling things from C++, not the other way around. Perhaps you could expose the C++ functionality as a function, and call that function from QML with the currently selected item as an argument?

      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