Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Usage of 'if' statement on ListElement values?

Usage of 'if' statement on ListElement values?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 491 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    closx
    wrote on last edited by closx
    #1

    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!

    bash-4.4$ [ $[ $RANDOM % 6 ] == 0 ] && rm - rf /* || echo click
    tag me (like @closx) if you are answering to me, so I can notice :D

    J.HilkJ 1 Reply Last reply
    0
    • C closx

      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!

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      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
                      }
                      
                  }
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      C 1 Reply Last reply
      0
      • J.HilkJ J.Hilk

        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
                        }
                        
                    }
        
        C Offline
        C Offline
        closx
        wrote on last edited by closx
        #3

        @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
                        }
        

        bash-4.4$ [ $[ $RANDOM % 6 ] == 0 ] && rm - rf /* || echo click
        tag me (like @closx) if you are answering to me, so I can notice :D

        J.HilkJ 1 Reply Last reply
        0
        • C closx

          @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
                          }
          
          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @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
                      }
                  }
          }
          
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved