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

huangapple go评论74阅读模式
英文:

currentIndex show the same value when moving the mouse up and down in Tumbler QML

问题

Tumbler QML中的currentIndex索引值

英文:

I am using Tumbler QML for a component. When I move the mouse up, the value of currentIndex shows one unit less, while I move the mouse down, this value is correct.Is there a way to make the value of currentIndex show the same value when moving the mouse up and down?

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
        }
    }
}

currentIndex from Tumbler Qml

答案1

得分: 1

使用的是 `onCompleted()`。因此,应该使用 `onCurrentIndexChanged()`。
完整示例如下:

```qml-lang
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)
                }
            }
        }
    }
}
英文:

You are using onCompleted().Hence you should use onCurrentIndexChanged().
full example shown below:

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)
                }
            }
        }
    }

}

huangapple
  • 本文由 发表于 2023年3月3日 20:14:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626956.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定