Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Contributor
nn_2012
Posts: 5
Registered: ‎10-29-2012
My Device: developer

How to pass listview selecteditem from qml to c++

Hi,
I am developing an app based on weatherguesser app. I have 2 datamodels for 2 listviews. The listviews display data from sqlite database. I need to pass the 1st listview selectedItem to c++ inorder to display the second listview. How to pass the listview selectedItem from qml to c++.  Please suggest me a solution.

Developer
zezke
Posts: 1,004
Registered: ‎12-12-2010
My Device: Z10 (red Limited Edition)

Re: How to pass listview selecteditem from qml to c++

Use the triggered signal to fetch the correct data from your data model.

 

ListView* lView = rootPane->findChild<ListView*>("coverListView");
connect(lView, SIGNAL(triggered(QVariantList)), this, SLOT(handleGridTriggered(QVariantList)));
lView->setDataModel(dataModel);


 

void App::handleGridTriggered(QVariantList indexPath)
{
	Data data = dataModel->data(indexPath);
	//Use data here
}

 

-------------------------------------------
BlackBerry Certified Builder for Native Application Development -- Proud member of the Belgian BlackBerry Developer group
Samples: Park in Ghent
Feeling generous? Nominate me for BB Elite member!
New Contributor
nn_2012
Posts: 5
Registered: ‎10-29-2012
My Device: developer

Re: How to pass listview selecteditem from qml to c++

Hi,
I am new to programming, so i am having difficulty in implementing the solution in my code. Here is my code:

 

app.cpp

QmlDocument *mQmlDocument= QmlDocument::create("asset:///main.qml");
mQmlDocument->setParent(this);

LinesModel *lineModel = new LinesModel(QStringList() << "fld_line" ,"lines_connection", this);
mQmlDocument->setContextProperty("_lineModel", lineModel);
lineModel->loadlines();


LinesModel *stationModel = new LinesModel(QStringList() <<
"fld_stations" ,"lines_connection", this);
mQmlDocument->setContextProperty("_stationModel", stationModel);
stationModel->loadstations();

if (!mQmlDocument->hasErrors()) {

NavigationPane *nav = mQmlDocument->createRootObject<NavigationPane>();

if (nav) {
// Set the main application scene to NavigationPane.
Application::instance()->setScene(nav);
}
}


LinesModel.cpp

 

void LinesModel::loadlines()
{
QString query = "SELECT fld_line FROM tbl_tram ";

DataAccessReply reply = mSqlConnector->executeAndWait(query, INITIAL_LOAD_ID);

if (reply.hasError()) {qWarning() << "onLoadLines load error " << reply;
} else {
onLoadAsyncResultData(reply);
}
}

void LinesModel::loadstations()
{

QString query = "SELECT fld_stations FROM tbl_tram" ;

DataAccessReply reply = mSqlConnector->executeAndWait(query, INITIAL_LOAD_ID);

if (reply.hasError()) {
qWarning() << "onLoadStations load error " << reply;
} else {
onLoadAsyncResultData(reply);
}
}

 

main.qml

 

ListView {
id: linesList

layout: StackListLayout {
headerMode: ListHeaderMode.Sticky
}
dataModel: _lineModel

listItemComponents: [
ListItemComponent {
type:"item"

StandardListItem {
id: linesItem
imageSpaceReserved: false
title: ListItemData.fld_line

}
}
]

onTriggered: {

var selectedItem = dataModel.data(indexPath);
var stationsPage = stationsPageDefinition.createObject();
nav.push(stationsPage);
}
}//listview


i have to pass the selecteditem in the _lineModel to linesModel::loadstations inorder to query the db with the
selectedItem.