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
Posts: 407
Registered: ‎04-04-2012
My Device: BlackBerry Z30/Z10/Z10LE/Q10/PlayBook

DataModel and GroupDataModel C++ and QML

When you create a new Blackberry Project using the List with context menu template, it creates a myListModel class that reads a JSON file. It integrates the class into cascades by using

 

    qmlRegisterType<MyListModel>("com.rim.example.custom", 1, 0, "MyListModel");

 

Then in the QML, it generates

 

        ListView {
            id: listView
            objectName: "listView"
            horizontalAlignment: HorizontalAlignment.Center
            dataModel: MyListModel {
                id: myListModel
            }

 

The declaration for the class is as follows:

 

.hpp

class MyListModel : public bb::cascades::QVariantListDataModel
{
    Q_OBJECT
public:



.cpp MyListModel::MyListModel(QObject* parent) : bb::cascades::QVariantListDataModel() { qDebug() << "Creating MyListModel object:" << this; setParent(parent); }

 

I am trying to accomplish the exact same thing except reading a SQL file.

 

I have looked at the weatherguesser example app and it uses a class (city model) to read a SQL db file. Except that they use

 

void WeatherGuesserApp::createCitiesModel()
{
    // Create and bind the cityModel, so that it can be accessed in the ContinentCitiesPage.QML
    // via the _cityModel context property
    CityModel *cityModel = new CityModel(QStringList() << "name", "continents_connection", this);
    mQmlDocument->setContextProperty("_cityModel", cityModel);
}

 

to tie the C++ to the QML. I rather use the qmlRegisterType but I can't get the parameters for the class right. Using the same method as the weatherguesser app, here is what I tried.

 

.hpp

#include <bb/cascades/GroupDataModel>

class MyListModel: public bb::cascades::GroupDataModel
{
    Q_OBJECT

public:

    MyListModel(const QStringList &keys, QObject *parent = 0);
    virtual ~MyListModel();

    Q_INVOKABLE void load();
};

.cpp

#include "mylistmodel.hpp"

using namespace bb::cascades;

MyListModel::MyListModel(const QStringList &keys, QObject *parent)
: GroupDataModel(keys, parent)
{
}

MyListModel::~MyListModel()
{
}

void MyListModel::load()
{

}

 But when I try to compile it, it is giving me the follow errors.

 

c:/bbndk/target_10_0_10_263/qnx6/usr/include/qt4/QtDeclarative/qdeclarativeprivate.h:82:11: error: no matching function for call to 'MyListModel::MyListModel()'
c:/bbndk/target_10_0_10_263/qnx6/usr/include/qt4/QtDeclarative/qdeclarativeprivate.h:82:11: note: candidates are:
../src/mylistmodel.hpp:13:5: note: MyListModel::MyListModel(const QStringList&, QObject*)
../src/mylistmodel.hpp:13:5: note:   candidate expects 2 arguments, 0 provided
../src/mylistmodel.hpp:7:7: note: MyListModel::MyListModel(const MyListModel&)
../src/mylistmodel.hpp:7:7: note:   candidate expects 1 argument, 0 provided

 I know that the QML call is probably wrong, but I am not sure how I pass the key list. I tried

 

            dataModel: MyListModel {
                id: myListModel
                sortingKeys: ["name"]
            }
 

 thinking that might work, but with no luck.

 

  Does anyone have a way that I can get this to work? Thanks in advance.

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: DataModel and GroupDataModel C++ and QML

[ Edited ]

You can't use parameters in the class constructor to pass information between QML and C++ you need to use Q_PROPERTY,

 

http://developer.blackberry.com/cascades/documentation/dev/integrating_cpp_qml/

 

sortingKeys is already defined as a Q_PROPERTY by the GroupDataModel base class anyway so you do not need to redefine it.

 

Have you looked at this,

 

http://developer.blackberry.com/cascades/reference/bb__data__sqldataaccess.html

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 407
Registered: ‎04-04-2012
My Device: BlackBerry Z30/Z10/Z10LE/Q10/PlayBook

Re: DataModel and GroupDataModel C++ and QML

I have been searching through the samples, this forum and the docs on an example of what I am trying to accomplish with no success. I have other classes that I am using with QmlRegisterType with great success. It is just the List with SQL C++ data model that I am currently strugging with. Does anyone have a simple example?

Developer
Posts: 407
Registered: ‎04-04-2012
My Device: BlackBerry Z30/Z10/Z10LE/Q10/PlayBook

Re: DataModel and GroupDataModel C++ and QML

I did take a look at the Quotes example but was hoping so something similar to the list with context template generated example.