c++ 在Tumbler QML中上下移动鼠标时,currentIndex显示相同的值

zf9nrax1  于 2023-03-05  发布在  其他
关注(0)|答案(1)|浏览(202)

我正在对一个组件使用Tumbler QML。当我向上移动鼠标时,currentIndex的值显示少了一个单位,当我向下移动鼠标时,这个值是正确的。有没有办法使currentIndex的值在上下移动鼠标时显示相同的值?

Component {
    id: delegateComponent

    Label {
        text: formatText(Tumbler.tumbler.count, modelData)
        opacity: 1.0 - Math.abs(Tumbler.displacement) / (Tumbler.tumbler.visibleItemCount / 2)
        horizontalAlignment: Text.AlignHCenter
        verticalAlignment: Text.AlignVCenter
        font.pixelSize: fontMetrics.font.pixelSize * 1.25
        
        Component.onCompleted: {
            console.log("log ", Tumbler.tumbler.currentIndex)
        }
        
    }
}

Frame {
    id: frame
    padding: 0
    anchors.centerIn: parent

    Row {
        id: row

        Tumbler {
            id: hoursTumbler
            model: 12
            delegate: delegateComponent
        }
    }
}

来自滚筒Qml的当前索引

mqkwyuun

mqkwyuun1#

您正在使用onCompleted()。因此,您应该使用onCurrentIndexChanged()。完整示例如下所示:

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.12

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")
    function formatText(c,t){
        return t;
    }

    Component {
        id: delegateComponent

        Label {
            text: formatText(Tumbler.tumbler.count, modelData)
            opacity: 1.0 - Math.abs(Tumbler.displacement) / (Tumbler.tumbler.visibleItemCount / 2)
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
//            font.pixelSize: fontMetrics.font.pixelSize * 1.25 
//            Component.onCompleted: {
//                console.log("log ", Tumbler.tumbler.currentIndex)
//            }

        }
    }

    Frame {
        id: frame
        padding: 0
        anchors.centerIn: parent

        Row {
            id: row

            Tumbler {
                id: hoursTumbler
                model: 3
                delegate: delegateComponent
                onCurrentIndexChanged: {
                    if(currentIndex !== undefined) console.log("log ", currentIndex)
                }
            }
        }
    }

}

相关问题