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
unbreakable
Posts: 74
Registered: ‎12-17-2009
My Device: Z30

Calling c++ in qml for sql

I am not familiar with c++ but I need to use some for my sqldatabase. I have looked at the Quotes, CRUD samples available but I need to tweak some of the bits for my app but, as I am not familiar with it, I am not sure how to.

 

Basically, I have a C++ class (db_class.cpp, .h) to help me write/edit my sqlite database. The examples given seems to use this to create the main.qml file directly. However, I do not want to do that as I need that main.qml to be created by another class. So the forms dealing with the database class directly would be a few different qml (like add.qml, update.qml), as well as components inside a ListView.

 

*For example, one of the things I need to do is to create a checkbox for each component in my Listview such that when the user presses a button, it will interact with the database and mark it true/false.

 

My questions are:

1) So should I use the db_class.cpp to create those multiple qml? If so how do I do that? I know the basic is:

 QmlDocument *qml = QmlDocument::create("asset:///add.qml").parent(this);

AbstractPane *root = qml->createRootObject<AbstractPane>();

app->setScene(root);

 

But if I have a few files that will need to use the class, do I have to repeat all 3 lines? can I still call each *qml?

 

2) Is there a better way?

I am not sure which of the methods described in https://developer.blackberry.com/native/documentation/cascades/dev/integrating_cpp_qml/ that I should use.

 

Given that all I would need to do is interact with my class to modify the database via submit buttons, what is the best way that I can call on the c++ in qml?

 

Thanks!

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Calling c++ in qml for sql

i would suggest to create qml in qml, except the initial main.qml of course.

for a database method i would suggest to mark it as Q_INVOKABLE (you have to extend QObject) to make it callable from QML.
Depening on the execution time you can return the result from the method or execute it async and emit a signal that you connect from QML.
----------------------------------------------------------
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
Developer
unbreakable
Posts: 74
Registered: ‎12-17-2009
My Device: Z30

Re: Calling c++ in qml for sql

I am not sure what you meant by create QML in QML. Do you mean to use the c++ to create a .qml file, and then to basically just include it inside another qml file as a Custom qml?

 

I have tried to do this in the mean time but it is actually causing the entire project to not built at all. It had also marked the line dbHelper *sqldatabase = new dbHelper(); as undefined reference to `dbHelper::dbHelper()'.

 

applicationui.cpp

#include "applicationui.hpp"
#include "dbHelper.h"
#include <bb/cascades/AbstractPane>
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/data/SqlDataAccess>
#include <bb/data/DataAccessError>
#include <bb/system/SystemDialog>

#include <QtSql/QtSql>
#include <QDebug>
ApplicationUI::ApplicationUI(bb::cascades::Application *app) :
        QObject(app)
{
    dbHelper *sqldatabase = new dbHelper();

    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
    qml->setContextProperty("_sqlDatabase", sqldatabase);
// ... default set root code
}

 I did not add anything to the applicationui.hpp

 

I am not sure what it means...

 

 

Developer
Benecore
Posts: 540
Registered: ‎08-31-2013
My Device: Q10, Z10LE, Nokia N950, DAB....

Re: Calling c++ in qml for sql

Clean the project

 

In many case if you add new Class it required to clean project and build it again



If helped give a like. If your problem was solved mark the post as "Solution"
Portfolio: BlackBerry Nokia
Social: Twitter | Facebook
About Me / Blog DevPDA | Blog
Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Calling c++ in qml for sql

I usually create QML pages dynamically by using ComponentDefinition.
See https://developer.blackberry.com/native/reference/cascades/bb__cascades__qmlcomponentdefinition.html
----------------------------------------------------------
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
Developer
unbreakable
Posts: 74
Registered: ‎12-17-2009
My Device: Z30

Re: Calling c++ in qml for sql

[ Edited ]

After I cleaned and rebuilt it, it now causes more error including declaring that bb::cascades is not a recognised namespace :smileysurprised: I have tried rebuilding it twice now, still the same problem!

 

(although the actual problem: my custom class, is now not throwing up an error by itself)

Developer
unbreakable
Posts: 74
Registered: ‎12-17-2009
My Device: Z30

Re: Calling c++ in qml for sql

Yes, I have been using ComponentDefinition for the qml files.

 

I guess my question is, as I have seen from the samples, the 'sub' - qml files usually call on the c++ classes by _classname.someFunction();

 

For example, I will call on my 'edit.qml' file from my main.qml's ListView. On that page, it will then have a button that, onClicked, will probably trigger '_app.editRecord' or something.

 

For me, I am confused because in the examples, the 'app' class immediately creates the main.qml, and everything seems to flow from there. I do not want to create the 'main.qml' directly. So I want to INCLUDE the class so that say my 'edit.qml' (and maybe other qml files) can use the same class!

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Calling c++ in qml for sql

You have to start with some UI, so i don't understand why you don't want the main.qml to be your starting point.
----------------------------------------------------------
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
Developer
unbreakable
Posts: 74
Registered: ‎12-17-2009
My Device: Z30

Re: Calling c++ in qml for sql

I guess it is because every single sample app does that, I am not sure what to do when I want to use more than one of these examples. For example, the BBM integration examples seem to want to do that too. I am not familiar enough with C++ to know how to modify at least one of them!

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Calling c++ in qml for sql

take a look at the build for blackberry boilerplate samples, they include a lot of stuff.

otherwise there seems no option that to learn the bit of c++ you need to modify the stuff.
----------------------------------------------------------
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