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: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator
Accepted Solution

Using C++ classes in QML

[ Edited ]

Hello,

 

I am trying to run an example from this page(https://developer.blackberry.com/cascades/documentation/dev/integrating_cpp_qml/index.html) about "Using C++ classes in QML". I have the following

code as stated on that page:

 

CombineCppAndQml::CombineCppAndQml(bb::cascades::Application *app)
: QObject(app)
{

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

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

    qmlRegisterType<QTimer>("my.library", 1, 0, "QTimer"); 

    app->setScene(root);
}

 and this I have in the QML file:

 

// Default empty project template
import bb.cascades 1.0
import my.library 1.0 


Page {
    Label {
        id: timerTestLabel
        text: "Hello world"
              
        attachedObjects: [
            QTimer {
                id: timer
                interval: 1000
                onTimeout :{
                    timerTestLabel.text = "Timer triggered"      
                }
            }
        ]
    }
          
    onCreationCompleted: {
        timer.start();
    }
}

 but I get a blank screen when I try to run the application. Can someone spot what is wrong here? Maybe I have called:

qmlRegisterType<QTimer>("my.library", 1, 0, "QTimer") 

at a wrong place?? Thanks.

 

Developer
Posts: 216
Registered: ‎09-18-2009
My Device: Z10
My Carrier: O2

Re: Using C++ classes in QML

That's the example.

Page {
    property alias labelText: label.text
    Container {
        Label {
            id: label
            text: "Label"
        }

        Button {
             objectName: "button"
             text: "Button"
        }
    }
}

 At a first glance I see that the container is missing.

Developer
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: Using C++ classes in QML

No no, thats the *first* example, I am referring to the example under section "Using C++ classes in QML", which is a bit below.
Developer
Posts: 216
Registered: ‎09-18-2009
My Device: Z10
My Carrier: O2

Re: Using C++ classes in QML

Ok, right. But the example works for me. I see the text "Hello world". And after a moment I see "Timer triggered".

Developer
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: Using C++ classes in QML

HM.... that's really strange. Is your code similar to what I pasted above??? ps. Maybe the problem is in SDK version that I use? Or the simulator version? Any help appreciated.
Developer
Posts: 216
Registered: ‎09-18-2009
My Device: Z10
My Carrier: O2

Re: Using C++ classes in QML

If it does not run then there should be some error message in the console of Momentics. Or in the compiler output during compilation should be an error message.

Developer
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: Using C++ classes in QML

No the problem is that it runs and compiles, I get a blank screen as I mentioned on this page: http://supportforums.blackberry.com/t5/Native-Development/Blank-screen/td-p/2129171
thanks.
Highlighted
Developer
Posts: 216
Registered: ‎09-18-2009
My Device: Z10
My Carrier: O2

Re: Using C++ classes in QML

What about removing all the QTimer code and just showing the Label. At least this should work.

 

Furthermore, you can debug the application. Set a break point and execute your code step by step to see where the error is.

Developer
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: Using C++ classes in QML

[ Edited ]

One problem I think is with the following line: If I remove this line: import my.library 1.0
from the QML file I manage to display *just* a Label. However, once I add that import line I get the blank screen I mentioned. Can I ask you where in your code do you add the: " qmlRegisterType<QTimer>("my.library", 1, 0, "QTimer");" line? Maybe that is causing the issues? thanks.

Developer
Posts: 103
Registered: ‎12-31-2012
My Device: Blackberry 9810
My Carrier: Verizon

Re: Using C++ classes in QML

Hi

Can u please move  qmlRegisterType<QTimer>("my.library", 1, 0, "QTimer"); to the very first line of the constructor

CombineCppAndQml::CombineCppAndQml(bb::cascades::Application *app)
: QObject(app)
{
	 qmlRegisterType<QTimer>("my.library", 1, 0, "QTimer");
    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

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



    app->setScene(root);
}

 _ Dishooom

 

Hope this helps