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
Developer
nareshkodumuri
Posts: 125
Registered: ‎04-28-2012
My Carrier: BlackBerry
Accepted Solution

how to get the contents of the selected list item from the listview using cpp?

[ Edited ]

Hi,

 

I am doing one simple  application getting the contents of the selected list item  from the ListView using cpp, In main.qml file i have created a listview using XMLDataModel and i have retrieved the selected contents of  the listview using dataModel.data(indexPath).attributeNameInXmlfile  in qml. like in the same way i  want  to get the contents of the selected listitem from the listview using cpp.please help me. 

 

Here is my code

main.qml

import bb.cascades 1.0

Page {
    content: Container {
        id: mainContainer
        objectName: "container"
        // property alias selectedText: selectedTextField.text
        //property alias deselectedText: deselectedTextField.text
        ListView {
            objectName: "listView"
            dataModel: XmlDataModel {
                source: "models/items.xml"
                id: model
            }
            listItemComponents: [
                ListItemComponent {
                    type: "item"
                    StandardListItem {
                        title: ListItemData.title
                    }
                }
            ]
        }
        TextField {
            id: selectedTextField
            objectName: "selectedTextField"
            text: ""
        }
        TextField {
            id: deselectedTextField
            objectName: "deselectedTextField"
            text: ""
        }
    }
}

 app.cpp

#include "app.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <QDebug>
#include <bb/cascades/XMLDataModel>
#include <iostream.h>
using namespace std;

using namespace bb::cascades;
QObject *textFieldSelected ;
QObject *textFieldNotSelected;
QObject *list;
App::App() {
	QmlDocument *qml = QmlDocument::create("main.qml");
	AbstractPane *root = qml->createRootNode<AbstractPane>();
	list = root->findChild<QObject*>("listView");
	textFieldNotSelected = root->findChild<QObject*>("deselectedTextField");
	textFieldSelected = root->findChild<QObject*>("selectedTextField");
	QObject::connect(list,SIGNAL(selectionChanged (QVariantList, bool)),this,SLOT(changeText(QVariantList, bool)));
	Application::setScene(root);
}

void App::changeText(QVariantList indexPath,bool selected) {
	

}

 app.hpp

#ifndef APP_H
#define APP_H

#include <QObject>
#include <bb/cascades/DataModel>
#include <bb/cascades/ListView>

/*!
 * @brief Application GUI object
 */
class App : public QObject
{
    Q_OBJECT
public:
    App();
public slots:
void changeText(QVariantList ,bool );

};

#endif // ifndef APP_H

 

Please use plain text.
Contributor
iamjay
Posts: 18
Registered: ‎04-28-2012
My Carrier: AIS

Re: how to get the contents of the selected list item from the listview using cpp?

Try this.

 

void App::changeText(QVariantList indexPath,bool selected) {
    XmlDataModel *model = (XmlDataModel *)list->dataModel();
    QVariantMap map = model->data(indexPath).toMap();
    // map is your data in QVariant
    qDebug() << map["title"].toString();
}

 

Please use plain text.
Developer
nareshkodumuri
Posts: 125
Registered: ‎04-28-2012
My Carrier: BlackBerry

Re: how to get the contents of the selected list item from the listview using cpp?

hi,

 

It is working fine.Thank u verymuch.

Please use plain text.
Developer
Pallam_Madhukar
Posts: 21
Registered: ‎04-26-2012
My Carrier: BlackBerry

Re: how to get the contents of the selected list item from the listview using cpp?

you can also try this,

 

listview->dataModel().data(indexPath).toMap()["title"].toString();

 

Please use plain text.
Developer
nareshkodumuri
Posts: 125
Registered: ‎04-28-2012
My Carrier: BlackBerry

Re: how to get the contents of the selected list item from the listview using cpp?

Thank you,I got it.

Please use plain text.
Developer
varghesekiran
Posts: 38
Registered: ‎09-27-2012
My Carrier: airtel

Re: how to get the contents of the selected list item from the listview using cpp?

Hi all..,

How can i do this in QML..???
Please use plain text.