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
Trusted Contributor
Posts: 134
Registered: ‎05-31-2010
My Device: 8300
My Carrier: vodqafone
Accepted Solution

Unable to Trigger list items

[ Edited ]

Hi,

 

 

    I am using Listview with GroupDataModel. I used StandardListItem to display rows.My Xml looks like this.

 

import bb.cascades 1.0

Page {
    Container {
        layout: StackLayout {
        }
        ListView {
            objectName: "photoslist"
            topPadding: 5
            leftPadding: 5
            bottomPadding: 5
            rightPadding: 5
            dataModel: GroupDataModel {
                id: groupDataModel
            }
            listItemComponents: [
                ListItemComponent {
                    type: "header"
                    Header {
                        title: "Photos"
                    }
                },
                ListItemComponent {
                    type: "item"
                    StandardListItem {
                        title: ListItemData.firstName
                        imageSource: ListItemData.imagesource
                    }
                }
            ] //    LsitComponents end
            
			onTriggered: {
              cppObj.onListTapped();  
            } 
			
            //    onCreationCompleted of Listview
            onCreationCompleted: {
                groupDataModel.insert({
                        "firstName": "Personal Activites",
                        "imagesource": "asset:///images/image1.jpg"
                    });
                groupDataModel.insert({
                        "firstName": "Social Activities",
                        "imagesource": "asset:///images/image13.jpg"
                    });
                groupDataModel.insert({
                        "firstName": "Honoring",
                        "imagesource": "asset:///images/image209.jpg"
                    });
            } //    oncreationCompleted end
        } //    listview end
    } //    container end
}//    page end

 

In OnTriggerd method i'm calling a method in Cpp class. My Cpp class looks like this

 

#include "PhotosListScreen.h"
#include "GridListView.h"

PhotosListScreen::PhotosListScreen(NavigationPane *mpane) :
		QObject(mpane) {
	pane = mpane;
	// create scene document from main.qml asset
	// set parent to created document to ensure it exists for the whole application lifetime
	QmlDocument *qml = QmlDocument::create("asset:///photoslist.qml").parent(this);

	// create root object for the UI
	Page *root = qml->createRootObject<Page>();
	if(!qml->hasErrors())
	{
		pane->push(root);
		qml->setContextProperty("cppObj", this);
	}

}

PhotosListScreen::~PhotosListScreen() {

}

void PhotosListScreen::onListTapped()
{
	new GridListView(pane);
}

 

So, when on tiggered in qml is called I am calling a method in cpp which will raise show Page on to the screen.

 

Issue is, onTriggered is not being called.

I made number of trails like implemented PageHandler, OnTouch, OnTouch for StandardListItem etc.
 But noting helped me out.

 

I have added onListTapped() as Q_INVOKABLE in .hpp file.

Please help me with the issue.

 

Thanks & Regards,

Sha.

Developer
Posts: 16,985
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Unable to Trigger list items

listitems reside in a different context, they are not in the same as the qml root. this means your setContextProperty is not accessible for them.
there are several workarounds for that, you can declare your context globally on the defaultDelarativeEngine or connect the list items with the main qml using the listview.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Highlighted
Developer
Posts: 107
Registered: ‎05-11-2011
My Device: BlackBerry Z10 LE
My Carrier: AirTel

Re: Unable to Trigger list items

Trusted Contributor
Posts: 134
Registered: ‎05-31-2010
My Device: 8300
My Carrier: vodqafone

Re: Unable to Trigger list items

I didnt get you exactly. From my understanding, I have to catch the tiggered method from cpp object . Is that right?

 

I have changed the datamodel to static xml data now.

Developer
Posts: 16,985
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Unable to Trigger list items

the list item does not have access to the c++ object.

you can define a function in the listview (which has access to it) and use the reference of the listview you have in the list item to call this function, similar to Rajs link.
Or you can define the context property as a global element as i stated before.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Trusted Contributor
Posts: 134
Registered: ‎05-31-2010
My Device: 8300
My Carrier: vodqafone

Re: Unable to Trigger list items

@simon_hain

 

I found out the real problem is not with the listview. Even if I replace the complete Qml with button and calling the method upon button clik, this method is not being called.

import bb.cascades 1.0

Page {
    Container {
        Button {
            text: "Click me"
            objectName: "button"
            onClicked: {
                cpp.onListTapped();
            }
        }
    }
}

 and my hpp file looks like.

 

/*
 * PhotosListScreen.h
 *
 *  Created on: 29-Apr-2013
 *      Author: shpolavarapu
 */

#ifndef PHOTOSLISTSCREEN_H_
#define PHOTOSLISTSCREEN_H_
#include <QObject>
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/NavigationPane>
#include <bb/cascades/ListView>
#include <bb/cascades/Button>


using namespace bb::cascades;

class PhotosListScreen: public QObject {

public:
	PhotosListScreen(bb::cascades::NavigationPane *app);
	virtual ~PhotosListScreen();
	NavigationPane *pane;
public slots:
	Q_INVOKABLE
	void onListTapped();

private:
	void setModelData(ListView * view);

};

#endif /* PHOTOSLISTSCREEN_H_ */

 

I am now into more deep trouble.

Please help me out.

I havn't changed the cpp file

 

Regards,

Sha

Developer
Posts: 16,985
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Unable to Trigger list items

The class is missing the Q_OBJECT macro.

Also check the name of your context property. in your first code snippet you register it as "cppObj", in this one you call it as "cpp".
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Trusted Contributor
Posts: 134
Registered: ‎05-31-2010
My Device: 8300
My Carrier: vodqafone

Re: Unable to Trigger list items

Thank you very much.

 

Now I got to know the real use of  Q_OBJECT, which handles Qt's C++ extensions.

 

Smiley Happy