QML ListView populate property does not work (its animation)

你离开我真会死。 提交于 2020-01-23 12:29:45

问题


I have following QML ListView inside ueLoginWindow:

import QtQuick 2.0
import QtMultimedia 5.5
import QtQuick.Controls 1.3
import QtQuick.Extras 1.4
import QtQuick.Layouts 1.2
import QtQuick.Window 2.2
import QtTest 1.1
import Enginio 1.0
import QtBluetooth 5.5
import QtCanvas3D 1.0
import QtLocation 5.5
import QtNfc 5.5
import QtPositioning 5.5
import QtQuick.LocalStorage 2.0
import QtQuick.XmlListModel 2.0
import QtSensors 5.5
import QtWebChannel 1.0
import QtWebKit 3.0
import QtWebSockets 1.0

import "../items"

Rectangle
{
    id: ueLoginWindow

    width: 768
    height: 512

    radius: 16

    border.color: "#ffffff"
    border.width: 4

    clip: true

    anchors.centerIn: parent

    gradient: Gradient
    {
        GradientStop
        {
            position: 0
            color: "#ffffff"
        }   // GradientStop

        GradientStop
        {
            position: 0.81
            color: "#000000"
        }   // GradientStop
    }   // gradient

    ColumnLayout
    {
        id: ueMainLayout

        anchors.margins: ueLoginWindow.radius
        anchors.fill: parent

        clip: true

        antialiasing: true

        layoutDirection: Qt.LeftToRight
        spacing: 16

        Text
        {
            id: ueLoginText

            color: "#000000"

            text: qsTr("STAFF LOGIN")
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 458
            anchors.top: parent.top
            anchors.topMargin: 0
            styleColor: "#ffffff"
            font.family: "Courier"
            font.bold: true

            clip: true

            font.pointSize: 23
            font.capitalization: Font.AllUppercase

            textFormat: Text.RichText

            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter

            Layout.alignment: Qt.AlignHCenter|Qt.AlignTop

            Layout.fillWidth: true
            Layout.fillHeight: true
        }   // Text

//        ListView
//        {
//            id: uePeopleView

//            antialiasing: true

//            model: uePeopleModel

//            spacing: 16

//            clip: true

//            Layout.alignment: Qt.AlignCenter

//            Layout.fillWidth: true

//            Layout.margins: ueLoginWindow.radius

//            Layout.minimumHeight: 192
//            Layout.preferredHeight: 192
//            Layout.maximumHeight: 192

//            orientation: ListView.Horizontal

//            delegate: Component
//            {
//                id: uePersonDelegate

//                Item
//                {
//                    width: 192
//                    height: 192

//                    Column
//                    {
//                        Image
//                        {
//                            source: "image://uePeopleModel/"+model.ueRoleImage
//                        }   // Image

//                        Text
//                        {
//                            text: model.ueRoleName

//                            font.pointSize: 10
//                            verticalAlignment: Text.AlignVCenter
//                            horizontalAlignment: Text.AlignHCenter
//                        }   // Text
//                    }   // Column
//                }   // Item
//            }   // delegate

//            scale: parent.ListView.isCurrentItem?0.8:1
//            Behavior on scale
//            {
//                NumberAnimation
//                {
//                    duration: 200
//                }   // NumberAnimation
//            }   // Behavior

//            preferredHighlightBegin: width/2-15
//            preferredHighlightEnd: width/2+15
//            highlightRangeMode: ListView.StrictlyEnforceRange
//            Component.onCompleted:
//            {
//                currentIndex=count/2
//            }   // Component.onCompleted

//            populate: Transition
//            {
//                NumberAnimation
//                {
//                    property: "opacity";
//                    from: 0;
//                    to: 1.0;
//                    duration: 400
//                }   // NumberAnimation

//                NumberAnimation
//                {
//                    property: "scale";
//                    from: 0;
//                    to: 1.0;
//                    duration: 400
//                }   // NumberAnimation
//            }   // Transition
//        }   // Listview

        ListView
        {
            id: uePeopleView
            anchors.top: parent.top
            anchors.topMargin: 29
            highlightFollowsCurrentItem: false

            antialiasing: true

            model: uePeopleModel

            spacing: 16

            clip: true

            Layout.alignment: Qt.AlignCenter

            Layout.fillWidth: true

            Layout.margins: ueLoginWindow.radius

            Layout.minimumHeight: 192
            Layout.preferredHeight: 192
            Layout.maximumHeight: 192

            orientation: ListView.Horizontal

            delegate: Component
            {
                id: uePersonDelegate

                Item
                {
                    width: 192
                    height: 192

                    Column
                    {
                        Image
                        {
                            source: "image://uePeopleModel/"+model.ueRoleImage
                        }   // Image

                        Text
                        {
                            text: model.ueRoleName

                            font.pointSize: 10
                            verticalAlignment: Text.AlignVCenter
                            horizontalAlignment: Text.AlignHCenter
                        }   // Text
                    }   // Column
                }   // Item
            }   // delegate

            scale: parent.ListView.isCurrentItem?0.8:1
            Behavior on scale
            {
                NumberAnimation
                {
                    duration: 200
                }   // NumberAnimation
            }   // Behavior

            preferredHighlightBegin: width/2-15
            preferredHighlightEnd: width/2+15
            highlightRangeMode: ListView.StrictlyEnforceRange
            Component.onCompleted: currentIndex=count/2
        }   // Listview

        Tumbler
        {
            id: ueLoginKeypadTumbler
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 77
            anchors.top: parent.top
            anchors.topMargin: 224
            clip: true

            Layout.preferredHeight: 96

            Layout.fillWidth: true
            Layout.alignment: Qt.AlignCenter

            Layout.margins: ueLoginWindow.radius

            antialiasing: true

            TumblerColumn
            {
                id: ueNumericTumblerColumnDigit1000

                model: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

                width: 165
            }   // ueNumericTumblerColumnDigit1000

            TumblerColumn
            {
                id: ueNumericTumblerColumnDigit100

                model: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

                width: 165
            }   // ueNumericTumblerColumnDigit100

            TumblerColumn
            {
                id: ueNumericTumblerColumnDigit10

                model: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

                width: 165
            }   // ueNumericTumblerColumnDigit10

            TumblerColumn
            {
                id: ueNumericTumblerColumnDigit1

                model: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

                width: 165
            }   // ueNumericTumblerColumnDigit1
        }   // ueLoginKeypadTumbler

        RowLayout
        {
            id: ueButtonsLayout

            layoutDirection: Qt.LeftToRight
            spacing: 16

            UeButton
            {
                id: ueButtonLogin
                text: qsTr("Login")

                Layout.alignment: Qt.AlignHCenter|Qt.AlignBottom

                Layout.fillWidth: true
                Layout.fillHeight: true

                ueText: qsTr("Login")
            }   // UeButton

            UeButton
            {
                id: ueButtonClear
                text: qsTr("Clear")

                Layout.alignment: Qt.AlignHCenter|Qt.AlignBottom

                Layout.fillWidth: true
                Layout.fillHeight: true

                ueText: qsTr("Clear")
            }   // UeButton

            UeButton
            {
                id: ueButtonQuit

                Layout.alignment: Qt.AlignHCenter|Qt.AlignBottom

                Layout.fillWidth: true
                Layout.fillHeight: true

                ueText: qsTr("Quit")
            }   // UeButton
        }   // RowLayout
    }   // ColumnLayout
}   // Rectangle

Now, when I run app, the items are fed to ListView from model correctly, but non of the items get animated according to populate property. I would like all items get animated according to populate property. What am I missing, because in the populate property docs states:

populate : Transition This property holds the transition to apply to the items that are initially created for a view.

It is applied to all items that are created when:

The view is first created The view's model changes The view's model is reset, if the model is a QAbstractItemModel subclass


回答1:


I think the problem is the model property should be updated after ListView has been initialised:

ListView {

    // don't initialise model property this time.
    //model: uePeopleModel

    // update the model property after ListView initialised.
    Component.onCompleted: {
        model = uePeopleModel;
    }
}


来源:https://stackoverflow.com/questions/32373982/qml-listview-populate-property-does-not-work-its-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!