10-06-2012 03:56 AM
Hi!
I wrote the following statements in CPP file:
qmlRegisterType<bb::system::SystemDialog("my.syste mDialogs",1,0,"SystemDialog");
qmlRegisterType<bb::system::SystemToast>("my.syste mToasts",1,0,"SystemToast");and included the required .h files
#include <bb/system/SystemToast> #include <bb/system/SystemDialog> using namespace bb::system;
and also specified LIBS += -lbbsystem in .pro file and imported the specified uri into the qml. The simulator doesn't open the application
in console it printed like this:
qt-msg 0 bb::cascades::QmlDocument:createRootObject document is not loaded or has errors, can't create root
And it is working fine with cpp code.. but not working with qml code.....!
Any help!!!
- Raju
Solved! Go to Solution.
10-06-2012 05:28 AM
I have created a re-usable class method which you are free to implement. It takes one parameter (QString) from the message.
Basically, you will be able to call a toast from QML as long as the setContextProperty from your cpp file.
app.cpp
headers
#include <bb/system/SystemToast> #include <bb/system/SystemUiPosition> #include <bb/Application> #include <QObject>
within your constructor set context property
QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(th is);
qml->setContextProperty("app", this);
create the class method
void App::showToast(QString message) {
SystemToast *toast = new SystemToast(this);
toast->setBody(message);
// optional position MiddleCenter = 0, TopCenter = 1, BottomCenter = 2
// toast->setPosition(bb::system::SystemUiPosition::T ype(2));
toast->show();
}
now the app.hpp file
// Tabbed pane project template
#ifndef App_HPP_
#define App_HPP_
#include <QObject>
#include <bb/cascades/Application>
namespace bb { namespace cascades { class Application; }}
/*!
* @brief Application pane object
*
*Use this object to create and init app UI, to create context objects, to register the new meta types etc.
*/
class App : public QObject
{
Q_OBJECT
public:
App(bb::cascades::Application *app);
virtual ~App() {}
Q_INVOKABLE void showToast(QString message);
};
#endif /* App_HPP_ */
Take particular note of Q_INVOKABLE if you don't specify that this to the class method, your code just won't work in your .qml file.
Now Let's look at how you would call the toast from any .qml file.
app.showToast('Message to display inside your toast");
Use cases and scenarios when displaying toasts in QML
Display a toast on startup
onCreationCompleted: {
app.showToast("Welcome to my awesome app");
}
Respond to user input with a toast
1. Generate input field TextField{} or TextArea{}
2. Capture text property.
3. Press button, display user input inside Toast.
TextField{
id: nameInput
hintText: "Enter your name"
}
Button{
id: toastUserButton
text: "Surprise me!"
onClicked: {
if(nameInput.text !== ""){
app.showToast("Hi" + nameInput.text + ". Surprise!");
}
}
}
Toasts gracefully fade out after 3 seconds.
Hope this helps. ![]()
10-06-2012 06:23 AM
Thanks greenback!
It worked with c++ code which you have sent. But it is not working with QML code....!
There is problem with the following code which is in documentation of dialogs and toasts:
Url: https://developer.blackberry.com/cascades/document
import bb.cascades 1.0
import my.systemToasts 1.0
import my.systemDialogs 1.0
Page {
content: Container {
layout: DockLayout {
}
Container {
horizontalAlignment: HorizontalAlignment.Center
verticalAlignment: VerticalAlignment.Center
Button {
text: "First time?"
attachedObjects: [
SystemToast {
id: myQmlToast
body: "So long! Thanks for coming, see you next time!"
onFinished: {
Application.quit();
}
},
SystemDialog {
id: myQmlDialog
title: "Friendly Warning"
body: "Kakel can be habit-forming... "
onFinished: {
if (myQmlDialog.result == "CancelButtonSelection") myQmlToast.show();
}
}
]
onClicked: {
myQmlDialog.show()
}
} // button
} // end Container
} // end Container
}// end Page
The register type statements written:
qmlRegisterType<SystemDialog>("my.systemDialogs",1 ,0,"SystemDialog");
qmlRegisterType<SystemToast>("my.systemToasts",1,0 ,"SystemToast");
But the application is not starting. And the log messages are
qt-msg 0 bb::cascades::QmlDocument: error when loading QML from: QUrl( "file:///apps/com.example.ToastsAndDialogs.testDev_sAndDialogs5ea08e91/native/assets//main.qml" ) qt-msg 0 --- errors:(file:///apps/com.example.ToastsAndDialogs. testDev_sAndDialogs5ea08e91/native/assets//main.qm l:2:1: module "my.systemToasts" is not installed import my.systemToasts 1.0 ^) qt-msg 0 bb::cascades::QmlDocument:createRootObject document is not loaded or has errors, can't create root
I am unable to implement SystemToast and SystemDialog in QML. And this is the problem with classes which I want to use in QML by setting qmlRegisterType() method...!
Thanks in advance!
- Raju
10-06-2012 09:50 AM
10-06-2012 09:51 AM
10-06-2012 10:46 AM
Thanks Peter!
By writing "import bb.system 1.0", it worked good but how can I receive the info of which button is clicked on the previously displyed dialog. The sample code snippet for checking the result argument given in the documentation is not working...!
this was the code: (OR you can refer the previous post)
onFinished: {
if (myQmlDialog.result == "CancelButtonSelection") myQmlToast.show();
}Thanks in advance!
- Raju
10-06-2012 10:59 AM
I just got the way how we can check the clicked button type, from the github sample(dialogs) and the way is:
onFinished: {
if (result == SystemUiResult.CancelButtonSelection) {
myQmlToast.show();
}
}But the sample code given for creating SystemToast and SystemDialog using qmlRegisterType calls in the documentation is not working. I request those people to correct it!!!
Thanks for your help!!!![]()
- Raju
10-06-2012 12:30 PM