02-04-2013 05:10 AM
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/document
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(th is); 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.
Solved! Go to Solution.
02-04-2013 06:12 AM
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(th is); qml->setContextProperty("injection", this); AbstractPane *root = qml->createRootObject<AbstractPane>(); app->setScene(root); mRootContainer = root->findChild<Container*>("rootContainer"); }
-Dishooom
--------------------------------------------------
Hope this helps ![]()
02-04-2013 06:22 AM
02-04-2013 06:31 AM
I believe that's solved as well ... ![]()