Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Solved Why can't I update the score value?

    QML and Qt Quick
    3
    4
    113
    Loading More Posts
    • 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.
    • N
      newbiSoso last edited by

      I have the score defined as below to be printed on screen

       property int score_int : 0
      Text{
          id: score
          text: score_int
          color: "white"
          anchors.verticalCenter: timer.verticalCenter
          anchors.horizontalCenter: attributes.horizontalCenter
          font.pixelSize: timer.height
          font.family: gill.name
       }
      

      and here are two shapes "plus" and "minus" where their colors change:

      Text{
                      id: plus
                      text: "+"
                      color: "#ff4140"
                      font.family: gill.name
                      anchors.top: indicator.bottom
                      anchors.topMargin: -(attributes.height-indicator.height)*0.4
                      anchors.left: attributes.left
                      anchors.leftMargin: attributes.width/6
                      font.pixelSize: (attributes.height-indicator.height)*1.5
                  }
                  Text{
                      id: minus
                      text: "|"
                      rotation: 90
                      color: "#ff4140"
                      font.family: gill.name
                      y: Math.ceil((attributes.height-indicator.height)/2)
                      anchors.verticalCenter: plus.verticalCenter
                      anchors.right: attributes.right
                      anchors.rightMargin: attributes.width/6
                      font.pixelSize: plus.font.pixelSize*0.5
                      font.bold: true
                  }
      

      and this list model to access and change square colors and text values in a grid

      property variant colorArray: ["#008499","#963A65","#01FF97","#FF4140"]
      ListModel {
              id: dummyModel
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0.3
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
      
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
              ListElement  {
                  squareColor: "white"
                  txt: 0
                  op: 0
              }
          }
      Timer { 
          // to change square colors randomly
          id: alfa
          interval: 2000; running: true; repeat: true
          onTriggered: {
              for (var i=0;i<dummyModel.count;i++) {                
                  dummyModel.setProperty(i,"squareColor",colorArray[Math.floor(Math.random()*3)])
              }
          }
      }
      Timer { 
          // to change score values in grid element randomly
          id: beta
          interval: 2000; running: true; repeat: true
          onTriggered: {
              for (var i=0;i<dummyModel.count;i++) {
                  var sc = Math.floor(Math.random()*20) // random value from 0 to 20
                  dummyModel.setProperty(i,"txt",sc) // score in each square
              }
          }
      }
      

      and this grid contains colored squares, when a square is clicked and color is the same as plus the number should be added to the score:

      Grid{
                      id: grid
                      columns: 5
                      rows: 8
                      spacing: 9
                      anchors.top: playfeild.top
                      anchors.horizontalCenter: playfeild.horizontalCenter
                      anchors.topMargin: 40
                      Repeater{
                          id: gridRect
                          model: dummyModel
                          Rectangle{
                              id: rect
                              layer.enabled: true
                              width: (playfeild.width-60)/5*0.9
                              height: width
                              color: model.squareColor
                              opacity: 1
                              radius: 10
                              Text {
                                  id: tttt
                                  anchors.centerIn: rect
                                  color: "black"
                                  opacity: 1
                                  text : model.txt
                                  font.family: gill.name
                                  font.pixelSize: rect.height*0.6
                              }
                              MouseArea{
                                  anchors.fill: parent
                                  onClicked: {
                                      if (model.squareColor === plus.color){  
                                          score_int = score_int + model.txt
                                          //time.value -=100
                                          model.squareColor = "transparent"
                                      }
                                      else if (model.squareColor === minus.color){
                                          score_int = score_int - model.txt
                                          //time.value+=100
                                          model.squareColor = "transparent"
                                      }
                                      else if (model.squareColor === "black"){
                                          score_int = 0
                                      }
                                  }
                              }
                          }
                      }
                  }
      

      but score can't be updated and there seems no syntax errors, why is that?

      ODБOï 1 Reply Last reply Reply Quote 0
      • ODБOï
        ODБOï @newbiSoso last edited by ODБOï

        hi,
        it's hard to say with little code snippets like this,

        @newbisoso said in Why can't I update the score value?:

        but score can't be updated and there seems no syntax errors, why is that?

        score_int = score_int - model.txt
        // console.log(model.txt) ;  console.log(typeof(model.txt))
        

        have you checked the value and type of model.txt here ?

        1 Reply Last reply Reply Quote 0
        • N
          newbiSoso last edited by

          Yes I did.
          If I have the console.log statement directly after the "score_int = " statement it prints nothing to the console, unlike when I have it below the last if statement I get the value of model.txt and NUMBER :

          score_int = score_int - model.txt
          console.log(model.txt) ;  console.log(typeof(model.txt))   //prints nothing
          .......
          else if (model.squareColor === "black"){
                                              score_int = 0
                                          }
          console.log(model.txt) ;  console.log(typeof(model.txt))   //prints output to the screen
          
          1 Reply Last reply Reply Quote 0
          • Shrinidhi Upadhyaya
            Shrinidhi Upadhyaya last edited by

            Hi @newbiSoso , just use "==" instead of "==="

            Code:-

             if (squareColor == plus.color){
                                                     score_int = score_int + txt
                                                     //time.value -=100
                                                     squareColor = "transparent"
                                                 }
                                                 else if (squareColor == minus.color){
                                                     score_int = score_int - txt
                                                     //time.value+=100
                                                     squareColor = "transparent"
                                                 }
                                                 else if (squareColor == "black"){
                                                     score_int = 0
                                                 }
            

            Sample Snapshot:-

            0_1567751233450_1428895a-5a83-4b57-9ab8-bd652628ee02-image.png

            Shrinidhi Upadhyaya.
            Upvote the answer(s) that helped you to solve the issue.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post