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. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Native Development

Reply
Trusted Contributor
gdev001
Posts: 163
Registered: ‎01-30-2013
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.

 

Please use plain text.
Developer
oliver_kranz
Posts: 175
Registered: ‎09-18-2009
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.

Please use plain text.
Trusted Contributor
gdev001
Posts: 163
Registered: ‎01-30-2013

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.
Please use plain text.
Developer
oliver_kranz
Posts: 175
Registered: ‎09-18-2009
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".

Please use plain text.
Trusted Contributor
gdev001
Posts: 163
Registered: ‎01-30-2013

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.
Please use plain text.
Developer
oliver_kranz
Posts: 175
Registered: ‎09-18-2009
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.

Please use plain text.
Trusted Contributor
gdev001
Posts: 163
Registered: ‎01-30-2013

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.
Please use plain text.
Developer
oliver_kranz
Posts: 175
Registered: ‎09-18-2009
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.

Please use plain text.
Trusted Contributor
gdev001
Posts: 163
Registered: ‎01-30-2013

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.

Please use plain text.
Developer
dishooom
Posts: 103
Registered: ‎12-31-2012
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 :Angel:

Please use plain text.