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

Cascades Development

Reply
New Contributor
indigodream
Posts: 2
Registered: ‎11-14-2012
My Carrier: VF
Accepted Solution

Can't Access C++ Object in QML

[ Edited ]

I have a NavigationPane defined in QML. In my C++ class I create the NavigationPane and use setContextProperty in order to have access to the C++ class. However, when I try to use the context in QML inside the onCreationCompleted signal, the console outputs a "ReferenceError: Can't find variable: splashSample" error.

 

Code is as follows:

QML:

import bb.cascades 1.0

NavigationPane
{
    id: navigationPane
    Page
    {
        onCreationCompleted:
        {
            splashSample.splashScreenDone.connect(navigationPane.push(getSecondPage()))
        }
        Container
        {
            background: Color.Black
            layout: DockLayout
            {
            }
            ImageView 
            {
                horizontalAlignment: HorizontalAlignment.Center
                verticalAlignment: VerticalAlignment.Center
                imageSource: "asset:///images/splash.png"
                onTouch:
                {
                    // show detail page when the button is clicked
                    var page = getSecondPage();
                    console.debug("pushing detail " + page)
                    navigationPane.push(page);
                }
                property Page secondPage
                function getSecondPage()
                {
                    if (! secondPage)
                    {
                        secondPage = secondPageDefinition.createObject();
                    }
                    return secondPage;
                }
                attachedObjects:
                [
                    ComponentDefinition
                    {
                        id: secondPageDefinition
                        source: "main.qml"
                    }
                ]
            }
        }
    }
}

 

cpp file:

SplashScreenSample::SplashScreenSample(bb::cascades::Application *app)
: QObject(app)
{
    QmlDocument *qml = QmlDocument::create("asset:///splash.qml").parent(this);

    AbstractPane *root = qml->createRootObject<AbstractPane>();
	qml->setContextProperty("splashSample", this);

    app->setScene(root);

//	QTimer::singleShot(1000, this, SLOT(loadMainScreen()));
}

void SplashScreenSample::loadMainScreen()
{
	emit splashScreenDone();
	qDebug("******************************************************************");
}

 

hpp file:

#ifndef SplashScreenSample_HPP_
#define SplashScreenSample_HPP_

#include <QObject>

namespace bb { namespace cascades { class Application; }}

class SplashScreenSample : public QObject
{
    Q_OBJECT
public:
    SplashScreenSample(bb::cascades::Application *app);
    virtual ~SplashScreenSample() {}

public Q_SLOTS:
	void loadMainScreen();

    Q_SIGNALS:
    	void splashScreenDone();

};

#endif /* SplashScreenSample_HPP_ */

 It seems that the alias "splashSample" is not accessible in the onCreationCompleted signal. Is this the case? Is there a way to connect to a signal when the QML document is created?

 

Many thanks.

Please use plain text.
Developer
simon_hain
Posts: 13,830
Registered: ‎07-29-2008
My Carrier: O2 Germany

Re: Can't Access C++ Object in QML

try to connect the slot with a function where you call navigationPane.push(getSecondPage())
----------------------------------------------------------
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
Please use plain text.
New Contributor
indigodream
Posts: 2
Registered: ‎11-14-2012
My Carrier: VF

Re: Can't Access C++ Object in QML

Thanks, but I still get the "ReferenceError: Can't find variable: splashSample" error
Please use plain text.
Developer
kylefowler
Posts: 479
Registered: ‎05-17-2009
My Carrier: ATT

Re: Can't Access C++ Object in QML

You need to call setContextProperty before you call createRootObject
Like all of my posts
Please use plain text.