View Single Post
Posts: 339 | Thanked: 1,623 times | Joined on Oct 2013 @ France
#19
OK.

I took some time to create a new empty QML project on Desktop to make some tests and be able to confirm what works and not only giving you some pointers.
To keep it simple (for me...) I used a slider to simulate the values of the weather.windDirection variable, and Text elements to show several solutions that all works for me (With the one you have above, I get the same result : stuck to "NE").

Feel free to choose whichever fits bets your way to code, and adjust it to your variable names (weather.windDirection in place of windDirection.value at least). Maybe some QML expert could point out what is the "QML way" to do this properly, even if they all works.

In order you have :
* if/else blocks with return
* if/else blocks without return
* ternary if operators
* string list based indexing (as wind direction can only be multiples of 45, dividing it by 45 gives a simple 0 based index to lookup in the string list)

If that example is not clear enough, I can try to put that at work directly in place in the patch you want to create, but would need your current code and if there is anything special to install (only Jolla's weather app ?) as I didn't follow all that was done at this point.

Code:
import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.3

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    ColumnLayout {

        Slider {
            id : windDirection
            value: 180
            minimumValue: 0
            maximumValue: 315
            stepSize: 45
        }

        Text {
            text: {
                if      (windDirection.value == 0) return 'N';
                else if (windDirection.value == 45) return 'NE';
                else if (windDirection.value == 90) return 'E';
                else if (windDirection.value == 135) return 'SE';
                else if (windDirection.value == 180) return 'S';
                else if (windDirection.value == 225) return 'SW';
                else if (windDirection.value == 270) return 'W';
                else if (windDirection.value == 315) return 'NW';
            }
            font.pixelSize: 36
            color: "red"
        }

        Text {
            text: {
                if      (windDirection.value == 0) 'N';
                else if (windDirection.value == 45) 'NE';
                else if (windDirection.value == 90) 'E';
                else if (windDirection.value == 135) 'SE';
                else if (windDirection.value == 180) 'S';
                else if (windDirection.value == 225) 'SW';
                else if (windDirection.value == 270) 'W';
                else if (windDirection.value == 315) 'NW';
            }
            font.pixelSize: 36
            color: "green"
        }


        Text {
            text: (windDirection.value == 0) ? 'N' :
                (windDirection.value == 45) ? 'NE':
                (windDirection.value == 90) ? 'E':
                (windDirection.value == 135) ? 'SE':
                (windDirection.value == 180) ? 'S':
                (windDirection.value == 225) ? 'SW':
                (windDirection.value == 270) ? 'W':
                (windDirection.value == 315) ? 'NW' : '--'
            font.pixelSize: 36
            color: "blue"
        }

        Text {
            text: ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'][windDirection.value/45]
            font.pixelSize: 36
            color: "#FF7F00"
        }

    }
}
Hope this helps !
 

The Following 5 Users Say Thank You to Zeta For This Useful Post: