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
New Developer
laclekim
Posts: 41
Registered: ‎02-11-2010
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?

Please use plain text.
New Developer
laclekim
Posts: 41
Registered: ‎02-11-2010
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

 

Please use plain text.
Developer
laurentC
Posts: 264
Registered: ‎02-05-2010
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
Please use plain text.
BlackBerry Development Advisor (Retired)
smacmartin
Posts: 499
Registered: ‎05-07-2012
My Carrier: developer

Re: If else statements in QML

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

 

Stuart

Please use plain text.
Developer
soaman
Posts: 841
Registered: ‎03-03-2011

Re: If else statements in QML

Try:

 

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
Please use plain text.
New Developer
laclekim
Posts: 41
Registered: ‎02-11-2010
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!
Please use plain text.
Developer
helex
Posts: 256
Registered: ‎02-14-2012
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. :smileyhappy:

Please use plain text.