June 22, 2011

kathy kathy
Lab Rat
50 posts

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

 

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:

  1. VisualItemModel {
  2.     property int itemwidth: 0
  3.     property int itemheight: 0
  4.     page1 {
  5.         id: p1
  6.         width: itemwidth
  7.         height: itemheight
  8.     }
  9.     page2 {
  10.         id: 2
  11.         width: itemwidth
  12.         height: itemheight
  13.     }
  14.     page3 {
  15.         id: p3
  16.         width: itemwidth
  17.         height: itemheight
  18.     }
  19. }

In the C++ code:

  1. QObject *rootObject = dynamic_cast<QObject*>(viewer.rootObject());
  2. 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

June 22, 2011

aalpert aalpert
Lab Rat
44 posts

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?

 
  ‹‹ List View using two elements      Image with radius in QML ››

You must log in to post a reply. Not a member yet? Register here!