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. [solved] script in property
Forum Updated to NodeBB v4.3 + New Features

[solved] script in property

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 3 Posters 1.3k Views 1 Watching
  • 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.
  • H Offline
    H Offline
    hellaman
    wrote on last edited by
    #1

    hello, my name is Steven and i'm new to QT and quite new to programming in general, so this won't be the last post by me hehe.

    so i want a listview with information about the device the app is running on, with screen info, device info etc.
    but i can't put Screen.screenwidth, Screen.height or Screen.pixelDensity in a list element because its a script and it needs to be a property, if i understand correctly.

    this is the bit of code i use:
    @
    Rectangle {
    width: Screen.width; height: Screen.height

        ListModel {
            id: infoModel
            property int screenWidth: 1
            ListElement {
                name: "Screen width"
                info: Screen.width
            }
            ListElement {
                name: "Screen height"
                info: Screen.height
            }
            ListElement {
                name: "pixel density"
                info: Screen.pixelDensity
            }
        }
    
        Component {
            id: infoDelegate
            Row {
                    id: infor
                    Text { text: name + ": " ; color: infor.ListView.view.infor_color }
                    Text { text: info }
            }
        }
    
        ListView {
            property color infor_color: "green"
            model: infoModel
            delegate: infoDelegate
            anchors.fill: parent
        }
    

    @
    so,
    info: Screen.width
    info: Screen.height
    info: Screen.pixelDensity
    doesn't work while stuff like
    info: 1
    does work.

    any help, feedback, examples is welcome

    thanks in advance!

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      You understood correctly. More details "here":http://qt-project.org/doc/qt-5/qml-qtqml-models-listelement.html#details
      Going by your current implementation, you will need to handle that at delegate level.

      157

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Rolias
        wrote on last edited by
        #3

        You could add the list elements dynamically. For example add a function like this:
        @function fillScreenProps () {
        infoModel.append({"name":"Screen width", "info": Screen.width})
        infoModel.append({"name":"Screen height", "info": Screen.height})
        infoModel.append({"name":"pixel density", "info": Screen.pixelDensity})
        }
        @

        Then in your Rectangle you could invoke the function
        @ Rectangle {
        Component.onCompleted: fillScreenProps()
        width: Screen.width; height: Screen.height
        //...@

        Check out my third course in the trilogy on Qt
        "Integrating Qt Quick with C++"
        http://bit.ly/qtquickcpp
        published by Pluralsight

        1 Reply Last reply
        0
        • H Offline
          H Offline
          hellaman
          wrote on last edited by
          #4

          this is what i have now @ Component {
          id: infoDelegate
          Row {
          id: infor
          Text { text: assign(index, 2) + ": " ; color: infor.ListView.view.infor_color }
          Text { text: assign(index, 1) }

                  }
              }
          
              ListView {
                  property color infor_color: "#7dfafa"
                  model: 50
                  delegate: infoDelegate
                  highlight: Rectangle { color: "lightsteelblue"; radius: 6 }
                  focus: true
                  anchors.fill: parent
          
              }
          }
          
          function assign(number, type){
          
              var a;
              var b;
          
              switch (number){
              case 0 :
                  a=Screen.width; b="Screen pixel width"; break;
              case 1 :
                  a=Screen.height; b="Screen pixel height"; break;
              case 2 :
                  a=Screen.pixelDensity.toFixed(2); b="Screen pixel density"; break;
              case 3 :
                  a=rect.x; b="rectangle X-coördinates "; break;
              case 4 :
                  a=rect.y; b="rectangle Y-coördinates"; break;
              case 5 :
                  a=rect.opacity.toFixed(2); b="rectangle opacity"; break;
              case 6 :
                  a=rect.color; b="rectangle color"; break;
              case 7 :
                  a=Screen.orientation; b="Screen orientation"; break;
              case 8 :
                  a=backpressed; b="times back is pressed"; break;
              case 9 :
                  a=xtonumber(sliderect.x).toFixed(2); b="times needed to go back"; break;
              case 10 :
                  a=orientationToString(Screen.primaryOrientation); b="Screen orientation"; break;
              case 11 :
                  a=Screen.logicalPixelDensity.toFixed(2); b="logical pixel density"; break;
              case 12:
                  a=Screen.name; b="screen name?"; break;
              case 13:
                  a=c;b="time in this page in ms"; break;
              default:
                  a=0; b=""; break
              }
              if (type === 1) return a;
              else return b;
          }@
          

          this works well, thanks for the advice and the help!

          1 Reply Last reply
          0
          • R Offline
            R Offline
            Rolias
            wrote on last edited by
            #5

            I think it would be cleaner (just an opinion) to use a couple of functions to populate the ListModel. You would of course need to add a line to fillScreenListModel() for every additional item you added in your case statement.
            @ function makeObject(roleName, value)
            {
            return {"name": roleName, "info": value}
            }

            function fillScreenListModel()
            {
                var dataArray = [],
                        i=0
                dataArray[i++] = makeObject("Screen width",Screen.width)
                dataArray[i++] = makeObject("Screen height",Screen.height)
                dataArray[i] = makeObject("Pixel density",Math.floor(Screen.logicalPixelDensity*100)/100)
                //Now move each object into the ListModel
                dataArray.forEach(function(element){infoModel.append(element)})
            }@
            

            Then your ListModel could look like this:
            @ ListModel {
            id: infoModel
            Component.onCompleted: fillScreenListModel()
            }@

            If you want to format pixelDensity as a string using toFixed(2), then add toString() to the other numbers so that all the info roles are strings.

            Check out my third course in the trilogy on Qt
            "Integrating Qt Quick with C++"
            http://bit.ly/qtquickcpp
            published by Pluralsight

            1 Reply Last reply
            0

            • Login

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