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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


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

Developer
Posts: 17,026
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
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
New Contributor
Posts: 2
Registered: ‎11-14-2012
My Device: BB10 Alpha
My Carrier: VF

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

Thanks, but I still get the "ReferenceError: Can't find variable: splashSample" error
Highlighted
Developer
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
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