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
raju504
Posts: 86
Registered: ‎05-21-2012
My Device: Blackberry 10 Dev Alpha
My Carrier: IDEA
Accepted Solution

Unable to get the cascades classes into qml

Hi!

 

 I wrote the following statements in CPP file:

 

qmlRegisterType<bb::system::SystemDialog("my.systemDialogs",1,0,"SystemDialog");
qmlRegisterType<bb::system::SystemToast>("my.systemToasts",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

- Raju
Please use plain text.
Developer
greenback
Posts: 496
Registered: ‎10-17-2010
My Device: BlackBerry Z10, DAC

Re: Unable to get the cascades classes into qml

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(this);
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::Type(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

 

 

  1. Attach code before last } curly bracket in  your main.qml

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. 

:Rockon:

 

Please use plain text.
Developer
raju504
Posts: 86
Registered: ‎05-21-2012
My Device: Blackberry 10 Dev Alpha
My Carrier: IDEA

Re: Unable to get the cascades classes into qml

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/documentation/ui/dialogs_toasts/sysdialogs.html (refer - Creating system dialog in QML)

 

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.qml: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

- Raju
Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Unable to get the cascades classes into qml

You're not showing enough context, I think. The qmlRegisterType calls look correct, yet they're not working. That suggests they are not actually being executed. Are you absolutely certain that you've got those statements in a place where they are executing? If so, are you certain you're actually installing and running what you think you are? Maybe you've got older code executing. The issue is not with your QML...

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Unable to get the cascades classes into qml

By the way, once you've spent some time considering what I said, and possibly fix your problem, note that the documentation is out of date and you don't have to do any of that anyway. Just do "import bb.system 1.0" and you can directly access SystemToast and the others, without any of the qmlRegisterType calls and related stuff...

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
raju504
Posts: 86
Registered: ‎05-21-2012
My Device: Blackberry 10 Dev Alpha
My Carrier: IDEA

Re: Unable to get the cascades classes into qml

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

- Raju
Please use plain text.
Developer
raju504
Posts: 86
Registered: ‎05-21-2012
My Device: Blackberry 10 Dev Alpha
My Carrier: IDEA

Re: Unable to get the cascades classes into qml

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!!!:smileyhappy:

 

- Raju

- Raju
Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Unable to get the cascades classes into qml

raju_winit, yes they've changed from strings to integers, with the "enum" that you found being the correct way to handle things now. There are numerous such issues in the documentation at the moment, but an updated set of docs has been promised for the very near future, so I'm personally holding off reporting such issues until I can see how many are covered by the updates.

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.