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
New Contributor
indigodream
Posts: 2
Registered: ‎11-14-2012
My Device: BB10 Alpha
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.

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

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
New Contributor
indigodream
Posts: 2
Registered: ‎11-14-2012
My Device: BB10 Alpha

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

Thanks, but I still get the "ReferenceError: Can't find variable: splashSample" error
Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900

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

You need to call setContextProperty before you call createRootObject
Like all of my posts