Qt World Summit: Register Today!

[Solved] Help with Counter in Delegate

  • I have this code that some what works. If I scroll through the model till the end all the minutes count down correctly. But if I don't do anything than only the minutes on the view get updated..

    I can't do the following:

    • Update Model's values
    • Start Trigger on non-visible delegates

    Any help would be appreciated.

    Video of the Problem:
    "Your text to link here...":http://www.youtube.com/watch?v=6dL3KuybwWM

    Here is the sample code:
    @import QtQuick 1.1
    import com.nokia.meego 1.0

    Page {
    tools: commonTools

    ListView {
         id: mainListView
         x: 0
         y: 70
         width: 480;
         height: 400
         clip: true
         boundsBehavior: Flickable.DragAndOvershootBounds
         cacheBuffer: 0
         snapMode: ListView.NoSnap
         orientation: ListView.Horizontal
         flickableDirection: Flickable.HorizontalFlick
         model: mainListViewXML;
         delegate:  myDelegate;

    XmlListModel {
    source: "http://nextbart.info/etd.php?station=12th";
    query: "/nextbart/etd"
    XmlRole { name: "minutes"; query: "@minutes/string()"; }

         onStatusChanged: {
                 if (status === XmlListModel.Ready)
                     // Method #2

    Timer {
    id: updateMinutes
    interval: 6000 ;
    running: false;
    repeat: true
    onTriggered: {

          var xcount = mainListViewXML.count,
          for (i=0; i< xcount; i++)
            var xmin  = parseFloat(mainListViewXML.get(i).minutes) - 1;

    id: myDelegate

    Item {

       width: 160;
                 Timer {
                     interval: 9000; running: true; repeat: true
                     onTriggered: {
                         //! Method #1 (Only Updates visible minutes)
                         var xmin  = parseFloat(trainMinutes.text) - 1;
                         trainMinutes.text = xmin;
                 Text {
                     id: trainMinutes
                     text: minutes
                     font.bold: true
                     color: "#000000"
                     smooth: true
                     x: 40
                     y: 70
                     width: 86
                     height: 68
                     opacity: 1



    Here is a sample screen of 3 numbers on the screen.

    !http://i.imgur.com/2HF3N.png(3 Numbers)!
    Note: There are lots of more trains but only these 3 get updated if you don't scroll.

  • A couple things you could try:

    • Regarding Method 1: ListView only creates visible delegates (that is, if the model has 10 items but the ListView can only display 3 items, it will not create the other 7). If you set a large cacheBuffer value it will force creation of all the items, and the Timer in the delegate method should work.
    • Regarding Method 1: Assuming the XML data is automatically updated, you could try changing your timer to call mainListViewXML.reload(), which will pull the latest file from the network and re-parse it.


  • Thanks Michael, I updated the cacheBuffer based on mainListViewXML.count, which seems to be working fine now.

    @ //! Reset Buffer
    mainListView.cacheBuffer = 160*mainListViewXML.count;@

    I don't know how much the cache buffer affects the memory usage, but I would rather load the items than reload the XML.

Log in to reply