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

Sample Not working from the BlackBerry web site

Hi, again I am trying to run  a sample from BlackBerry web site. This time this one:

"Injecting C++ objects into QML" (the last section on the bottom) - from page:

https://developer.blackberry.com/cascades/documentation/dev/integrating_cpp_qml/index.html,

 


This is my code:

 

cpp file

 

#include "CombineCppAndQml.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>

#include <bb/cascades/Color>
#include <bb/cascades/Container>
#include <bb/cascades/DockLayout>
#include <bb/cascades/ImageView>
#include <bb/cascades/Page>
#include <bb/cascades/Slider>
#include <bb/cascades/Stacklayout>
#include <bb/cascades/StackLayoutProperties>

using namespace bb::cascades;

CombineCppAndQml::CombineCppAndQml(bb::cascades::Application *app)
: QObject(app)
{
    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
    qml->setContextProperty("injection", this);
    AbstractPane *root = qml->createRootObject<AbstractPane>();
    app->setScene(root);
}

void CombineCppAndQml::injectContainer()
{
    // Creates the container and adds it to the root
    // container in qml
    mRootContainer->add(Container::create()
        .background(Color::Red)
        .preferredSize(200,200)
        .bottomMargin(20)
        .horizontal(HorizontalAlignment::Center));
}

 hpp file

 

#ifndef CombineCppAndQml_HPP_
#define CombineCppAndQml_HPP_

#include <QObject>

 #include <bb/cascades/Container>
#include <bb/cascades/Page>

namespace bb { namespace cascades { class Application; }}

using namespace bb::cascades;


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

    // By using Q_INVOKABLE we can call it from qml
    Q_INVOKABLE void injectContainer();

private:
    Page *appPage;
    Container *mRootContainer;
};

#endif /* Test_Hpp_ */

 and the qml file as listed on the Web Site.

 

import bb.cascades 1.0
  
Page {
    // Allows the user to scroll vertically
    ScrollView {
        scrollViewProperties {
            scrollMode: ScrollMode.Vertical
        }
        // Root container that containers from C++ are added to
        Container {
            objectName: "rootContainer"      
            layout: StackLayout {}
            // Button that calls the C++ function to add a
            // new container. The selectedIndex from the drop down
            // is passed to C++.
            Button {
                text: "Add container"
                onClicked: {
                    injection.injectContainer();       
                }
            }
        }
    }
}

 When I run it, I actually see the button, but when I click it in order to open up the second container, my application gets minimized instead of showing the second container.

 

pps. I also have a separate main.cpp class which calls the CombineCppAndQml class.

 

Any help greatly appreciated.

 

Please use plain text.
Developer
dishooom
Posts: 103
Registered: ‎12-31-2012
My Device: Blackberry 9810

Re: Sample Not working from the BlackBerry web site

Please add the following line to your CombineCppAndQml constructor

 

 mRootContainer = root->findChild<Container*>("rootContainer");

 

CombineCppAndQml::CombineCppAndQml(bb::cascades::Application *app)
: QObject(app)
{
    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
    qml->setContextProperty("injection", this);
    AbstractPane *root = qml->createRootObject<AbstractPane>();
    app->setScene(root);

    mRootContainer = root->findChild<Container*>("rootContainer");
}

 -Dishooom

 

--------------------------------------------------------------------------------------------------------------------------------

 

Hope this helps :Angel:

 

Please use plain text.
Developer
gdev001
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: Sample Not working from the BlackBerry web site

Thanks. good catch!! I was also thinking smth was wrong with the mRootContainer. Thanks. It is weird how these samples are arranged on the web site, I just copied the code from the site, and that line was not there (it was separately mentioned somewhere else though). ps. Maybe you could also have a look at my other issue? http://supportforums.blackberry.com/t5/Native-Development/Using-C-classes-in-QML/td-p/2133641 thanks again
Please use plain text.
Developer
dishooom
Posts: 103
Registered: ‎12-31-2012
My Device: Blackberry 9810

Re: Sample Not working from the BlackBerry web site

I believe that's solved as well ...  :Angel:

 

Please use plain text.