簡體   English   中英

Qt/QML:如何在 QML 中雙向同步 ScrollView?

[英]Qt/QML: How to bidirectional sync ScrollView in QML?

我想同步兩個可滾動列表視圖的 contentY,如此簡化的代碼所示

Item {
    SplitView {
        orientation: Qt.Horizontal

        Component1 {
            id: left
            contentY: right.contentY
        }
        Component1 {
            id: right
            contentY: left.contentY
        } 
    }
}

//Component1.qml

Item {
    property alias contentY: component2.contentY

    Component2 {
        id: component2
    }
}

//Component2.qml

Item {
    property alias contentY: list.contentY

    ScrollView {
        ListView {
            id: list
        }
    }
} 

當我啟動或重新加載 QML 場景並僅在一個拆分視圖中滾動時,它正在工作。 但是,一旦我開始在另一個列表視圖中滾動,雙向綁定就會被破壞並且 contentY 不再同步。 我只能彼此分開滾動列表視圖。 我怎樣才能避免這種情況? 有沒有更好的方法來同步 contentY?

我找到了一個似乎有效的解決方案:

Item {
    SplitView {
        orientation: Qt.Horizontal
        
        Component1 {
            id: left
            Binding {
                target: right
                property: "contentY"
                value: left.contentY
            }
        }
        Component1 {
            id: right
            Binding {
                target: left
                property: "contentY"
                value: right.contentY
            }
        } 
    }
}

//Component1.qml

Item {
    property alias contentY: component2.contentY
    
    Component2 {
        id: component2
    }
}

//Component2.qml

Item {
    property alias contentY: list.contentY

    ScrollView {
        ListView {
            id:list
        }
    }
}

但是,如果有更好的解決方案,我將不勝感激任何提示:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM