Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Usage of 'if' statement on ListElement values?



  • Hello everyone,
    I will keep it short, I have a list model,

                ListModel{
                    id: schoolstuff
                    ListElement{
                        name:QT_TR_NOOP("My Grades")
                        target:1
                        selected:false
                    }
                    ListElement{
                        name:QT_TR_NOOP("GPA Calculator")
                        target:2
                        selected:false
                    }
                    ListElement{
                        name:QT_TR_NOOP("Academic Calendar")
                        target:3
                        selected:false
                    }
                }
    

    And I want to use if statement on the 'name' values of list elements.
    it is like,

    if integer smth = 0, name of the first element will be QT_TR_NOOP("My Grades")
    if integer smth = 1, name of the first element will be QT_TR_NOOP("Meeting List")
    if integer smth = 3, name of the first element will be QT_TR_NOOP("Daily Plans")
    

    You got the idea!
    note: I keep asking questions in that forum so often, I don't know if there is a limitation for it, but for real, I ask here if I cannot find the solution anywhere. And I often got questions, I practice for QT like 9 hours a day (not exaggerate).
    THANKS ALL!


  • Moderators

    hi @closx

    you can bind the name to a JS-expression, in this case the if statement

    that would look like this:

    ListModel{
                    id: schoolstuff
                    ListElement{
                        name: If(samt == 0 ) return QT_TR_NOOP("My Grades"); else return "Some other text"
                        target:1
                        selected:false
                    }
                    
                }
    


  • @J.Hilk Hey, thanks for your answer!
    It gave the error

    exited with code 255
    

    Some QT bug or what?

    code:

    ListElement{
                        name: if(hello == 0 )return QT_TR_NOOP("My Grades"); else return "Some other text" // tried QT_TR_NOOP before "some other text"
                        target:1
                        selected:false
                    }
    

  • Moderators

    @closx my bad, ListElement doesn't work that way :(

    this should do however:

    import QtQuick 2.9
    import QtQuick.Window 2.2
    import QtQuick.Controls 2.5
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        id:root
    
        property bool alternative: false
        Timer{
            running: true
            interval: 2000
            repeat: true
            onTriggered: alternative = !alternative
        }
    
        ListModel{
            id: listModel
            ListElement{
                name: "red";
                altName: "blue"}
            ListElement{name: "blue"
                altName:
                    "yellow"}
        }
    
            ListView{
                id: listView
                anchors.fill: parent
    
                model: listModel
                delegate:Text {
                    text: root.alternative ? altName : name
                    color: text
                }
            }
    }
    
    

Log in to reply