CPU usage increases when switching images

I created a QML component that simulates an "image slider" that displays different images indefinitely.

import QtQuick 2.3

Item {
    id: root

    // Start auto rotating items
    Timer {
        interval: 500; running: true; repeat: true
        onTriggered:{
            itemModel.append(itemModel.get(listView.currentIndex))
            itemModel.remove(listView.currentIndex)
        }
    }

    ListView {
        id: listView

        anchors.fill: parent

        interactive: false

        model: itemModel
        delegate:
            Image {
                id: bg
                width: listView.width
                height: listView.height
                source: bgSource

                asynchronous: true
                cache: false
            }
    }

    ListModel {
        id: itemModel
        ListElement {
            bgSource: "ui_elements/images/slideimage01.png"
        }
        ListElement {
            bgSource: "ui_elements/images/slideimage02.png"
        }
        ListElement {
            bgSource: "ui_elements/images/slideimage03.png"
        }
    }
}

      

Unfortunately, the CPU usage continues to increase over time until the application stops. If I print the "count" variable itemModel, it stays at 3 as expected.

I run the application as follows: qmlscene SlideImageTest.qml

.

Qt version: 5.3.2

The three images I'm using can be found here:

enter image description here

enter image description here

enter image description here

Question

Why is CPU usage increasing?

+3


source to share


1 answer


A quick profiling reveals that most of the CPU cycles are spent itemModel.get

trying to transform the internal structure of the model into a JavaScript object that you would immediately after jumping into the model.

In general, modifying the model will also involve creating and deleting QtQuick items, which in this case do not seem necessary.

A more efficient way to solve this problem would be to make the ListView scroll to the next image instead of changing the model at the position currently selected in the ListView:



Timer {
    onTriggered:{
        listView.incrementCurrentIndex()
    }
}

ListView {
    id: listView
    keyNavigationWraps: true
    highlightMoveDuration: 0
    ...
}

      

If you really need to keep the way the model is modified, the C ++ model will probably avoid the costly conversion to JavaScript.

+3


source







All Articles