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

Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.

New Developer
Posts: 41
Registered: ‎02-11-2010
My Device: Bold 9000
My Carrier: AT&T
Accepted Solution

If else statements in QML

I've been trying this for at least 4 hours with very unsuccessful results. Again, i'm trying to make a simple grade calculator app that displays percentage and letter grade.


in C++ I could do something like

//Calculating grade
        cout << "Grade: ";
        if (result == 100){
        cout << "A+" << endl;
        }else if (result < 100 && result >= 90){
        cout << "A" << endl;
        }else if (result < 90 && result >= 80){
        cout << "B" << endl;
        }else if (result < 80 && result >= 70){
        cout << "C" << endl;
        }else if (result < 70 && result >= 60){
        cout << "D" << endl;
        }else if (result < 60){
        cout << "F" << endl;

but in cascades I can't seem to figure out how to replace a Label text with conditional statements like above.


I've tried the following and get parsing errors


 // Calculate Percentage
        Label {
            id: grdCalc
            enabled: true
            visible: false
            text: (inTotal.text / inPossible.text) * 100
        Label {
            id: finalGrade
            // custom property to calculate final grade percentage
            text: "Percent: " + grdCalc.text + "%"
            textStyle.alignment: TextAlignment.Center
            textStyle.fontWeight: FontWeight.Bold
            textStyle.size: 60.0
            layoutProperties: StackLayoutProperties {
                horizontalAlignment: HorizontalAlignment.Center
                verticalAlignment: VerticalAlignment.Center
            enabled: true
            visible: false
        Label {
            id: letGrade
            text: {
                if (grdCalc.text == 100) {
                    text = "A+"
                } else if (grdCalc.text >= 90 && grdCalc.text < 100) {
                    text = "A"
                } else if (grdCalc.text >= 80 && grdCalc.text < 90) {
                    text = "B"
                } else if (grdCalc.text >= 70 && grdCalc.text < 80) {
                    text = "C"
                } else if (grdCalc.text >= 60 && grdCalc.text < 70) {
                    text = "D"
                } else if (grdCalc.text < 60) {
                    text = "F"

is there another method to do this?

New Developer
Posts: 41
Registered: ‎02-11-2010
My Device: Bold 9000
My Carrier: AT&T

Re: If else statements in QML

It doesnt make sense. it almost seems like i need to set up a custom element or property or something... I'm stumped


Posts: 276
Registered: ‎02-05-2010
My Device: Blackberry Z10
My Carrier: Bouygues Telecom

Re: If else statements in QML

Not sure if you can place a if statement in the text property of your label ???

Maybe try the onCreationCompleted: property

Twitter : @LaurentKP
My apps
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: If else statements in QML

Check out sample cascadescookbookqml, Javascript.  You should be able to call a javascript function.



Posts: 849
Registered: ‎03-03-2011
My Device: Z10

Re: If else statements in QML



text: (grdCalc.text == 100 ? "A+" : (grdCalc.text >= 90 && grdCalc.text < 100 ? "A" : (grdCalc.text >= 80 && grdCalc.text < 90 ? "B" : (grdCalc.text >= 70 && grdCalc.text < 80 ? "C" : (grdCalc.text >= 60 && grdCalc.text < 70 ? "D" : "F")))))


Leader of Ljubljana BlackBerry Developer Group
BlackBerry Certified Builder for Native Application Development
New Developer
Posts: 41
Registered: ‎02-11-2010
My Device: Bold 9000
My Carrier: AT&T

Re: If else statements in QML

That did it! Just to be clear, is that javascript or C++ or something else. I'm still pretty new to development and I'm soaking all this stuff in!
Posts: 305
Registered: ‎02-14-2012
My Device: BlackBerry Passport / Nokia N950
My Carrier: -

Re: If else statements in QML

the text (a==b) ? "text1" : "text2" statement is the qml programming language Cascades is made of.


There is no normal if else since qml describes only states. At qml "everything is happening at the same time".


Only inside events a normal programming language is used. At example inside of: onClicked: {} - you're able to put java code inside this {}. The code is executed from the top of the function to the bottom as soon as the event is triggered.


C++ is only used outside of qml inside of *.cpp files.


Hope this helps. Smiley Happy