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
Regular Contributor
Posts: 60
Registered: ‎03-07-2011
My Device: Z10 + PlayBook
My Carrier: Telus

QMLRegisterType DataModel Problem

Hi everyone,

 

I've started to work from the Bucket List sample to create a Prayer Journal application. No matter what I try, though, I keep getting the same message in my QML file: "The PrayerModel component might be an unknown or custom component. Its properties are not validated." I may be wrong but I assume it is why every time I try to load the application to my Dev Alpha I just get a black screen with BlackBerry written on it.

 

Here's what I think are relevant code:

 

In PrayerJournal.cpp:

qmlRegisterType<PrayerModel>("com.prayerjournal.prayermodel", 1, 0, "PrayerModel");

 In main.qml:

import com.prayerjournal.prayermodel 1.0

 

ListView {
                    id: prayerList
                    
                    signal newBBMStatus(string message, string icon)
                             
                    dataModel: prayerModel

 

attachedObjects: [
                   PrayerModel {
                       id: prayerModel
                       // The path to the JSON file with initial data, this file will be moved to
                       // the data folder on the first launch of the application (in order to
                       // be able to get write access).
                       jsonAssetPath: "app/native/assets/models/prayers.json"
                       
                       parent: listContain
                                               
                       // The filtering is initially set to "today" to show items which are scheduled for today
                       filter: "today"              
                       sortingKeys: ["title"]
                   },

 In prayermodel.cpp, the class declaration, this is the constructor:

PrayerModel::PrayerModel(QObject *parent)
{
  setParent(parent);
}

One option I have considered is that the constructor wants a parameter but I don't know what that should be or how to pass it from QML.

 

 

Any help would be much appreciated; I'm still trying to get something together for the 10k deadline.

Regular Contributor
Posts: 60
Registered: ‎03-07-2011
My Device: Z10 + PlayBook
My Carrier: Telus

Re: QMLRegisterType DataModel Problem

No ideas? I've also emailed one of the Dev evangelists. At this point I can't find anything else wrong so if it isn't this then I'm not sure what it is.
Developer
Posts: 291
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: QMLRegisterType DataModel Problem

[ Edited ]

total long-shot as I've not used datamodels in this way before (I only set them from C++ using ListView::setDataModel)

 

Do you need a Q_DECLARE_METATYPE for your PrayerModel class?

 

And I think your PrayerModel constructor needs to call the DataModel constructor:

 

PrayerModel::PrayerModel(QObject *parent = 0) : DataModel(parent)

{

}

 

(don't know if that makes a difference because i don't know what the DataModel constructor does, if anything)

Regular Contributor
Posts: 60
Registered: ‎03-07-2011
My Device: Z10 + PlayBook
My Carrier: Telus

Re: QMLRegisterType DataModel Problem

The :smileyvery-happy:ataModel(parent) idea didn't work; just created compile errors.

 

Still looking at the Q_DECLARE_METATYPE idea. I hadn't encountered it before.

 

That said, for whatever reason, the warning disappeared when I loaded it this afternoon even before I tried either of those solutions. Still getting nowhere with anything showing up other than a splashscreen so my problem must be somewhere else, I guess.

Developer
Posts: 291
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: QMLRegisterType DataModel Problem

How have you declared your datamodel? You should be inheriting the DataModel class - I can only imagine this is why you have compile errors?

 

class PrayerModel : public DataModel

{

   Q_OBJECT

 

   PrayerModel(QObject *parent = 0) DataModel(0)   { }

 

   ~PrayerModel() {}

};

 

 

Regular Contributor
Posts: 60
Registered: ‎03-07-2011
My Device: Z10 + PlayBook
My Carrier: Telus

Re: QMLRegisterType DataModel Problem

[ Edited ]

Here's how it was, again based on the Bucket List sample so I don't necessarily understand why it was this way but I trusted it

typedef bb::cascades::QListDataModel<QVariantMap> PrayerListModel;

class PrayerModel: public PrayerListModel
{
Q_OBJECT

 I tried changing the class PrayerModel: public PrayerListModel to your suggestion of class PrayerModel: public DataModel as well as to class PrayerModel: public QListDataModel. Both created errors.

 

The original error after disappearing for a little bit is now back again, so I'm still guessing this is the most likely problem.

Developer
Posts: 291
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: QMLRegisterType DataModel Problem

[ Edited ]

Ok... so you didn't mention how you had defined your datamodel previously. In this instance you require:

 

class PrayerModel : public PrayerListModel

{

Q_OBJECT

 

public:

 

   PrayerModel(QObject *parent = 0) : PrayerListModel(parent)

   {

   }

 

   ~PrayerModel()

   {

   }

};

 

 

You don't need to do anything else (for the moment) - the class above will definitely work. If you are having issues still, then the problem is either in your QML, or somewhere else in your program that is unrelated to your datamodel. I would suggest that you:

 

1. Debug your program to see where it is getting stuck

2. Compile & run the bucklist sample to see how that works

3. Reduce your program down so that you only have your datamodel & listview in the qml, to really isolate the problem

 

I would suggest if you still get stuck after this, to remove the datasource from your qml and set it from C++ instead using the ListView::setDataModel function

Regular Contributor
Posts: 60
Registered: ‎03-07-2011
My Device: Z10 + PlayBook
My Carrier: Telus

Re: QMLRegisterType DataModel Problem

As suggested, I commented out everything of the DataModel except for the constructor and destructor, and commented out the setParent(parent) in the constructor. No problems there. I did get two different errors with copying your constructor to my .cpp, though: it gave an error adding the = 0 and it gave an error with the PrayerListModel(parent).

Developer
Posts: 291
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: QMLRegisterType DataModel Problem

this is a basic c++ inheritance problem - you are inherting from a base class so you must call the base class constructor with the proper parameters it is expecting.

 

In this case you inherit from PrayerListModel, which (because of the typedef statement) is actually an alias for QListDataModel<QVariantMap>. You need to find the class definition for QListDataModel to see what c++ constructors it defines.

 

Right-click that QListDataModel name,  click "open declaration" (or press F3) and that will take you to the QListDataModel class. You can see exactly how the constructor(s) are defined. In this instance there are two (one default constructor, and a second copy-constructor). The one you are interested in is the default constructor, defined as:

 

QListDataModel()

 

So make your class definition:

 

class PrayerModel : public PrayerListModel 
{

  Q_OBJECT

public: 

  PrayerModel(QObject *parent = 0) : PrayerListModel()   /* call the default constructor, with NO parameters */

  {

     setParent(parent);  /* this is your own code that overrides the default behaviour of PrayerListModel */

  }

 

  ~PrayerModel() {}

 


};

 

 

Regular Contributor
Posts: 60
Registered: ‎03-07-2011
My Device: Z10 + PlayBook
My Carrier: Telus

Everything you said made complete sense to me... but it s...

Everything you said made complete sense to me... but it still didn't change anything. I have that, including the constructor and destructor defined completely in the header instead of splitting it up as I thought was normal. It doesn't add any new errors like most of the other proposed solutions did but it didn't fix the one in the QML recognizing the PrayerModel component either.