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
bmorr
Posts: 318
Registered: ‎04-13-2013
My Device: Z10
My Carrier: Telus
Accepted Solution

pass childCardDone data to a textfield in QML?

I have a camera card invoke function that replies with the image filepath once an image is saved using the childCardDone function.  I have no clue how to pass data from c++ to qml.  I know that when passing data from QML to C++ you define a QString &something.  How do I do the reverse?

 

Here's my cpp code:

 

void App::childCardDone(const bb::system::CardDoneMessage &message)
{
	if (message.reason() == "done")

//      Need the message.data() info in my QML
	 message.data();

	qDebug() << message.reason() << "\n";
	qDebug() << message.dataType() << "\n";
	qDebug() << message.data() << "\n";
}


        public slots:
        void childCardDone(const bb::system::CardDoneMessage &message);

 and I just simply want to get the message.data() value into a Label textfield in QML:

 

Label{
id: capturedFilePath
text: <<the function value>>
}

 All help is appreciated and will be liked and accepted as a solution if it works.

 

thanks in advance.

Please use plain text.
Developer
BBSJdev
Posts: 6,089
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: pass childCardDone data to a textfield in QML?

There are two main ways either define a class, register in your applicationui,

 

ApplicationUI::ApplicationUI(bb::cascades::Application *app)
	: QObject(app)
{
    qmlRegisterType<MyClass>("custom.lib", 1, 0, "MyClass");

    [...]
}

 include it in your QML by...

 

import bb.cascades 1.0
import bb.system 1.0
import custom.lib 1.0

Container {

   onCreationCreated: {
       result = myClass.callFunction();
   }

   [ ...]

    attachedObjects: [
        MyClass {
            id: myClass
        }

    ]
}

 

Or you can pass a variable also in th applicationui,

 

    QmlDocument *qml = QmlDocument::create("asset:///main.qml")
    		.property("_app", this)
                 .property("passedProperty", myVariable);
    		.parent(this);

 


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Developer
bmorr
Posts: 318
Registered: ‎04-13-2013
My Device: Z10
My Carrier: Telus

Re: pass childCardDone data to a textfield in QML?

[ Edited ]

Just to be clear in the second example passedProperty refers to "childCardDone" and myVariable refers to message.data()?  Sorry I'm a little confused with the terminology for the different parts that comprise a function.

 

my code is setup like so:

 

QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
    qml->setContextProperty("_app", this);

 

Please use plain text.
Developer
BBSJdev
Posts: 6,089
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: pass childCardDone data to a textfield in QML?

That's right you can tie a global variable to the result from your cardChildDone.

 

In the QML you can then just watch for that variable to change using a slot such as;

 

onPassedVariableChanged: {

  // do something

}


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Developer
BBSJdev
Posts: 6,089
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: pass childCardDone data to a textfield in QML?

However if you choose the first method I talked about you'd be able to tie in a slot directly to the card done signal.


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Developer
BBSJdev
Posts: 6,089
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: pass childCardDone data to a textfield in QML?

But in your example using the second methos you would do something like this...

 

Label {

  text: passedVariable

}

 

So anytime the global variable in applicationui.cpp changes the text gets update in your QML.


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Developer
BBSJdev
Posts: 6,089
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: pass childCardDone data to a textfield in QML?

There is a 3rd option (sorry) of accessing the Label directly from C++ using the findChild() function.

By giving your Lable an objectName it becomes findable from C++ and directly updateable.

 

Label{
  id: capturedFilePath
  objectName: "filePathLabel"
}

 then in your C++,

 

void App::childCardDone(const bb::system::CardDoneMessage &message)
{
    if (message.reason() == "done") {

        Label* cardLabel  = root->findChild<Label*>("filePathLabel");

        cardLabel->setText(message.data());

    }
}

 

Where root is a pointer to your main qml object.

 


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Developer
bmorr
Posts: 318
Registered: ‎04-13-2013
My Device: Z10
My Carrier: Telus

Re: pass childCardDone data to a textfield in QML?

Thank you so much! I will let you know if it works and mark solved.
Please use plain text.
Developer
bmorr
Posts: 318
Registered: ‎04-13-2013
My Device: Z10
My Carrier: Telus

Re: pass childCardDone data to a textfield in QML?

I decided to use your third method.  I get the error " 'root' was not declared in this scope"

 

this is the code I have in the function:

 

void App::childCardDone(const bb::system::CardDoneMessage &message)
{
	if (message.reason() == "done") {

		Label* cardLabel = root->findChild<Label*>("filePathLabel");

	cardLabel->setText("file://" + message.data());
	}
}

 I don't understand the error since I believe my main QML is declared:

 

QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
    qml->setContextProperty("_app", this);

    AbstractPane *root = qml->createRootObject<AbstractPane>();

 

 

Please use plain text.
Developer
BBSJdev
Posts: 6,089
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: pass childCardDone data to a textfield in QML?

To use it in your function you will need to decalre it in the header.

 

In your header declare,

 

AbstractPane* root;

 

Then change,

 

AbstractPane *root = qml->createRootObject<AbstractPane>();

 

to,

 

root = qml->createRootObject<AbstractPane>();

 

 


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.