qtlistqmlstates

Qt High Score System


I'm still quite new to Qt and I want to add a High Score system to my game. I found this file http://grip.espace-win.net/doc/apps/qt4/html/demos-declarative-snake-content-highscoremodel-qml.html which is a high score model qml element. I have have added it to my project but I am at an utter loss on how to implement it. I simply want to know how I can use it to show a High Score Table when my window goes to a certain state. I also want to know how to add scores and close it when the game is restarted. This my seem silly but I really can't figure out how to use it.


Solution

  • From the linked file above:

    Use this component like this:

    HighScoreModel {
      id: highScores
      game: "MyCoolGame"
    }
    

    Then ... use the model in a view:

    ListView {
      model: highScores
      delegate: Component {
        ... player ... score ...
      }
    }
    

    So by slightly altering the simpler of the two examples given in the QML ListView docs, we get:

    import QtQuick 1.0
    
    ListView {
      width: 180; height: 200
      model: highScores {}
      delegate: Text {
        text: player + ": " + score
      }
    }
    

    Though if you want further control over the formatting of each element of the list, as suggested by the use of delegate: Component in the example quoted above from HighScoreModel.qml, the second usage example in the documentation shows you how.