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
marioi
Posts: 43
Registered: ‎07-23-2013
My Device: Z10
Accepted Solution

Manipulate a string with 'left', 'mid' or 'right' functions

I'm battling to apply the following principle (from the Qstring reference on dev site), in my QML using left, mid and right functions, in order to manipulate a string..

 QString x = "Pineapple";
 QString y = x.left(4);      // y == "Pine"

 I've included the #include <QString> library in my app.CPP file. Cascades doesn't seem to recognize Qstring as a statement. It gives 'qsTr' as an option though. Are these two statements one and the same thing? Also, is there perhaps cpp code missing required to pass it to QML?

 

Thank you.

Developer
greenmr
Posts: 912
Registered: ‎03-20-2013
My Device: Red LE Developer Z10

Re: Manipulate a string with 'left', 'mid' or 'right' functions

[ Edited ]

Code in QML files is JavaScript, not C++, so you can't declare variables as QString. Do this instead:

 

 var x = "Pineapple";
 var y = x.left(4);      // y == "Pine"

String vars in QML are automatically QStrings, so you can call any QString functions that were declared Q_INVOKABLE from your QML code.

 

qsTr() is not the same at all. It is a macro that hooks the string into the Cascades translation engine. Any strings you wrap in qsTr() will be added to the translation table. If you decide later to support multiple languages or locales any strings you put inside qsTr macros can be translated by editing the.ts file that is generated when you compile your app. The same can be done in C++ code by wrapping strings in tr().

 

Check this out for more information.

 

CIAO

 


marioi wrote:

I'm battling to apply the following principle (from the Qstring reference on dev site), in my QML using left, mid and right functions, in order to manipulate a string..

 QString x = "Pineapple";
 QString y = x.left(4);      // y == "Pine"

 I've included the #include <QString> library in my app.CPP file. Cascades doesn't seem to recognize Qstring as a statement. It gives 'qsTr' as an option though. Are these two statements one and the same thing? Also, is there perhaps cpp code missing required to pass it to QML?

 

Thank you.






Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.
Developer
marioi
Posts: 43
Registered: ‎07-23-2013
My Device: Z10

Re: Manipulate a string with 'left', 'mid' or 'right' functions

Thanks for responding. I'm a bit of newbie with statements in qml but I have the understanding of variables coming from the VB stable.

 

I tried 'var' previously, got the below error. Is it perhaps a library I need to include?

 

What I did now is I copied and pasted the pineapple example instead, as is, into my main.qml and I get the following error with the console.log command, which I was getting all along and the app won't go further:

 

var x = "Pineapple";
var y = x.left(4);

 The error in my IDE console window:

asset:///main.qml:94: TypeError: Result of expression 'x.left' [undefined] is not a function.

 

Developer
BBSJdev
Posts: 6,116
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30

Re: Manipulate a string with 'left', 'mid' or 'right' functions

QML only allows JavaScript declarations within certain constrained areas of the code (e.g. inside a function or slot).

 

To declare variables within QML you need to use the property declaration.

 

i.e.

property variant anyData1: 5

property variant anyData2: "hello"

property string anyString: "hello"


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.
Developer
marioi
Posts: 43
Registered: ‎07-23-2013
My Device: Z10

Re: Manipulate a string with 'left', 'mid' or 'right' functions

[ Edited ]

I've exctracted the actual code below which is the onTriggered signal within my listview container.

 

This is confusing because as you'll see, the declaration of variables 'feedItem' and 'page' works fine and it's within the same container, as it parses the link to the 'page' without error. If more of the code required, i can paste it.

 

To me it seems the 'left' property is not being recognized.

 

onTriggered: {
                    var feedItem = dataModel.data(indexPath);
                    var page = detailsPage.createObject();
                    
                    //*** manipulate string here
                    var x = "Pineapple";
                    var y = x.left(4);
                    var feedItemLink = feedItem.link;
                    var feedItemModified = feedItemLink.left(7);
                    //***
                    
                    page.htmlContent = feedItem.link;
                    
                    console.log("feedItemModified: " +y);
                    console.log("feedItem.link: " +feedItem.link);
                    
                    newsPane.push(page);
                }

 

Developer
AvisDx
Posts: 101
Registered: ‎12-02-2012
My Device: Z10 LE, PlayBook 32GB

Re: Manipulate a string with 'left', 'mid' or 'right' functions

You can not use Qt functions (of fucntions of Qt classes) directly from QML (except those specially declared).

It's better to use JS functions - in your case it could be substring:

var y = x.substring(0,4)



My BB10 apps: Photo Memo | TriQuick | SkidStone | MeeBirthdays
If anyone has been helpful to you, please show your appreciation by clicking the button inside of their post.
Developer
marioi
Posts: 43
Registered: ‎07-23-2013
My Device: Z10

Re: Manipulate a string with 'left', 'mid' or 'right' functions

[ Edited ]

Ahhh, I understand... I have to distinguish what is JS and what is Qt and which context each of those can be used.

 

substring worked!! Thanks guys.

Developer
marioi
Posts: 43
Registered: ‎07-23-2013
My Device: Z10

Re: Manipulate a string with 'left', 'mid' or 'right' functions

Also how would I apply the substring statement if i wanted to return the 3 characters from the right of a string "hello" for example, to return "llo"?

Developer
AvisDx
Posts: 101
Registered: ‎12-02-2012
My Device: Z10 LE, PlayBook 32GB

Re: Manipulate a string with 'left', 'mid' or 'right' functions

I suppose:

 

var y = x.substring(x.length-3, 3)



My BB10 apps: Photo Memo | TriQuick | SkidStone | MeeBirthdays
If anyone has been helpful to you, please show your appreciation by clicking the button inside of their post.