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
Contributor
romulusiulian
Posts: 19
Registered: ‎08-08-2012
My Device: Blackberry Z10
My Carrier: Vodafone
Accepted Solution

Using java script with QML, saving a text in a java script variable and showing in a label

I am trying to figure out how to take a text entered by the user in a text field and showing it in a label using java script. The most tricky part is that I want it to be seen in the label after the app is restarted.
Any idea?
Should I use a JS file with functions. If so what are the best practices?

Thanks in advance!
Romulus-Iulian LESNIC
Please use plain text.
Developer
slashkyle
Posts: 814
Registered: ‎10-16-2012
My Device: Red Z10, Dev Alpha C
My Carrier: Telus

Re: Using java script with QML, saving a text in a java script variable and showing in a label

[ Edited ]

This example uses a wee bit of C++ I'm not sure if this can be done with purely JS

 

 

for your text area or label apply onTextChanged you'll likely want to have a button setup to enable & disable the textarea or else if they erase it, the label text would be erased

 

.qml

TextArea {
	id: myTextArea
	onTextChanged: {
		myTextArea.text = myLabel.text;
		//you could also apply ^ to a button's onClicked function etc
		App.saveValueFor("mySavedText" myTextArea.text); 
	}
}
Label {
	id: myLabel
	text: App.getValueFor("mySavedText", "");
}

App.saveValueFor(); uses QSettings will need to be initialized in your ApplicationUI.cpp &.h

 

.cpp

App. is achieved by setting a context property

qml->setContextProperty("App", this);

 

QString App::getValueFor(const QString &objectName, const QString &defaultValue)
{
    QSettings settings;

    // If no value has been saved, return the default value.
    if (settings.value(objectName).isNull()) {
        return defaultValue;
    }

    // Otherwise, return the value stored in the settings object.
    return settings.value(objectName).toString();
}

void App::saveValueFor(const QString &objectName, const QString &inputValue)
{
    // A new value is saved to the application settings object.
    QSettings settings;
    settings.setValue(objectName, QVariant(inputValue));

}

 

.hpp

public:
	
	Q_INVOKABLE
    QString getValueFor(const QString &objectName, const QString &defaultValue);

    Q_INVOKABLE
    void saveValueFor(const QString &objectName, const QString &inputValue);

 You will aslo need to slect the Shared Files permission in the bar descriptor for QSettings to be able to work

 

 

Please use plain text.
Contributor
romulusiulian
Posts: 19
Registered: ‎08-08-2012
My Device: Blackberry Z10
My Carrier: Vodafone

Re: Using java script with QML, saving a text in a java script variable and showing in a label

Ok!

 

This could work. I think. Only one problem: tried to put the code where you said and I am getting lots of errors. Guess I am doing something wrong.

 

Could please give a full sample?

I would be in great debt.

Thanks in advance!

Romulus-Iulian LESNIC
Please use plain text.
Developer
slashkyle
Posts: 814
Registered: ‎10-16-2012
My Device: Red Z10, Dev Alpha C
My Carrier: Telus

Re: Using java script with QML, saving a text in a java script variable and showing in a label

sorry about that, I noticed  I forgot a comma whichcaused the issue

 

//was
App.saveValueFor("mySavedText" myTextArea.text);
 
//should have been
App.saveValueFor("mySavedText", myTextArea.text); 

and i've made a working sample as well that you can build, run and view the source sorry about the delay.

 

 

Please use plain text.