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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Contributor
Posts: 27
Registered: ‎09-03-2012
My Device: Blackberry Playbook
My Carrier: None

ListView and GroupDataModel...I'm struggling :(

hi

I've tried to go through all the examples I can see, I don't think I am far aware but not getting the correct data displayed.

 

I create a ListView in QML, define the datasource as an attachedObject. I add data to the  GroupDataModel in QML and the data is displayed. But if I try and add from c++, I see nothing. Anyone know what I'm doing wrong?

 

The QML:

import myLibrary 1.0

TabbedPane {
    id: tabpane
    showTabsOnActionBar: true
    Tab 
    {
        title: qsTr("Tab One")
        Page 
        {
            content: Container 
            {              
               attachedObjects: 
               [
                    GroupDataModel 
                    {
                        id: groupDataModel
                        objectName: "groupDataModel"
                    }
                ]
                        
                ListView 
                {
                    id: myListView
                    dataModel: groupDataModel 
                                     
                    onCreationCompleted: 
                    {
                                    groupDataModel.insert( {"Charlie" : "Charles"} );
                                    groupDataModel.insert( {"Dickie" : "Bob"} );
                                    groupDataModel.insert( {"Karen" : "Kath"} );
                                    groupDataModel.insert( {"Sammy" : "Sims"} );
                    }                                    

		    function itemType(data, indexPath) 
		    {
			if (indexPath.length == 1) 
			{
			return "header";
			}
		        return "listItem";
		    }                    
                }
            }
        }
    }    
}

 And the main app C++ (where I am trying to add data to the datasource:

#include "Listitem.hpp"
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include "WebImageView.h"
#include "Employee.hpp"

using namespace bb::cascades;

Listitem::Listitem(bb::cascades::Application *app)
: QObject(app)
{
	// register my Employee class
		qmlRegisterType<Employee>("myLibrary", 1, 0, "Employee");
		// standard create code
  	QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
  	// get the top level tabbed pane and find the GroupDataModel within it
    mTabbedPane = qml->createRootObject<TabbedPane>();
    GroupDataModel *pModel = mTabbedPane->findChild<GroupDataModel*>("groupDataModel");
    // insert a new Employee object
    pModel->insert(new Employee("Peter", "Piper"));

// again, standard stuff 
    AbstractPane *root = qml->createRootObject<AbstractPane>();
    app->setScene(root);
}

 If I check the length of the GroupDataModel, it has increased from 4 to 5. But I think my problem is the Employee class...just not sure what! Smiley Happy

Employee.hpp:

#include <QObject>

//using namespace bb::cascades;

class Employee : public QObject {
    Q_OBJECT

    Q_PROPERTY(QString firstName READ firstName WRITE setFirstName  FINAL)
    Q_PROPERTY(QString lastName READ lastName WRITE setLastName FINAL)


public:
    Employee(QObject *parent = 0);
    Employee(QString argLastName, QString argFirstName);

    QString firstName();
    QString lastName();

    Q_SLOT void setFirstName(QString newName);
    Q_SLOT void setLastName(QString newName);

    QString mFirstName;
    QString mLastName;
};

 and Employee.cpp:

#include "Employee.hpp"

Employee::Employee(QObject *parent) {}

Employee::Employee(QString argLastName, QString argFirstName)
{
	mFirstName = argFirstName;
	mLastName = argLastName;
}

QString Employee::firstName()
{
	return mFirstName;
}

QString Employee::lastName()
{
	return mLastName;
}

void Employee::setFirstName(QString newName)
{
	mFirstName = newName;
}

void Employee::setLastName(QString newName)
{
	mLastName = newName;
}

 I know its going to be something stupid, I apologise. But I just can;t see it Smiley Sad

 

many thanks

 

Highlighted
Developer
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: ListView and GroupDataModel...I'm struggling :(

What is the QML that displays your list items? From that code you are never setting any listitemcomponents.
Like all of my posts
Contributor
Posts: 27
Registered: ‎09-03-2012
My Device: Blackberry Playbook
My Carrier: None

Re: ListView and GroupDataModel...I'm struggling :(

hi KyleFowler

i thought by creating the listview (myListView in the above QML) that was enough? doesn't the ListView handle displaying each item in the list automatically?

thanks

Contributor
Posts: 27
Registered: ‎09-03-2012
My Device: Blackberry Playbook
My Carrier: None

Re: ListView and GroupDataModel...I'm struggling :(

I added section for 

 

listItemComponents: [
     ListItemComponent
     {
         type: "listItem"

etc

But it still only displays 4 items in the list. In my latest code I add 20 items to the groupdatamodel in the c++ code, none of them show up Smiley Sad

 

Developer
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: ListView and GroupDataModel...I'm struggling :(

I would expect that all of the c++ ones would work and none of the ones in qml. The qml data structure has no way of knowing what key to access? Post your whole listitemcomponent code, its impossible to know if it should actually be displaying anything.

I would also not do a findchild on the data model. I would do a findchild to get the listview and then from the listview get the datamodel out of it.
Like all of my posts
Contributor
Posts: 27
Registered: ‎09-03-2012
My Device: Blackberry Playbook
My Carrier: None

Re: ListView and GroupDataModel...I'm struggling :(

hi KyleFowler

yeah, its the opposite! The ones in QML are displayed, the c++ is not.

The listitemcomponent code is:

	            listItemComponents: [ 
	                    // The second ListItemComponent defines how "listItem" items
	                    // should appear. These items use a Container that includes a
	                    // CheckBox and a Label.
	                ListItemComponent 
	                {
	                    type: "listItem"
	                    Container 
	                    {
	                        Label 
	                        {
	                            text: "help!!!" 
	                            textStyle 
	                            {
	                                base: SystemDefaults.TextStyles.TitleText
	                                fontWeight: FontWeight.Normal
	                            }
	                        }
	                    } // end of Container
	                } // end of second ListItemComponent
	                    
	            ] // end of listItemComponents list

 So you can see, I'm not displaying the data yet, if I try listitemdata.firstname I get "undefined".

 

I really appreciate the support, I'd dead in the water right now.

If you could spare a few minutes, I uploaded the whole project to:

https://www.dropbox.com/s/eckejm7ihnzajs0/listitem.zip

 

many thanks

Developer
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: ListView and GroupDataModel...I'm struggling :(

Did you try changing the way you access the datamodel in c++?

Its also always possible that depending on the order that things get done that the c++ code finishes before the qml loads and the qml loading wipes out the c++ items?
Like all of my posts
Contributor
Posts: 27
Registered: ‎09-03-2012
My Device: Blackberry Playbook
My Carrier: None

I'm not able to get a pointer to the ListView so I can't...

I'm not able to get a pointer to the ListView so I can't then get the datasource from it.

 

In my C++ I have:

QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
// Expose this class to QML so that we can call its functions from there
qml->setContextProperty("app", this);

AbstractPane *root = qml->createRootObject<AbstractPane>();
GroupDataModel *pModel = root->findChild<GroupDataModel*>("groupDataModel");
Page *pPage2 = root->findChild<Page*>("zonepage");
Container *pCont = root->findChild<Container*>("myContainer");
ListView *pList = root->findChild<ListView*>("myListView");

 GroupDataModel returns with a valid pointer but pPage2, pCont and pList are all null, I cannot get a pointer back.

 

Also strange, if I change the page from being a tabbedPane to just a Page (no tabs) the c++ code successfully adds the rows to the listview.

 

Is this a bug?

 

thanks

Contributor
Posts: 27
Registered: ‎09-03-2012
My Device: Blackberry Playbook
My Carrier: None

I think this thread has got a bit too messy, sorry. I'll...

I think this thread has got a bit too messy, sorry. I'll try and isolate each issue and create a clear, concise question for each Smiley Happy

Developer
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: I think this thread has got a bit too messy, sorry. I'll...

You need to use objectName to get things from QML in c++, not the ID property
Like all of my posts