如何垂直滚动到特定项目

时间:2015-09-17 16:31:43

标签: qt listview qml qtquick2

我有一个ListView,包括很多单选按钮。该列表大于可见区域。检查其中一个单选按钮。有时,如果所选单选按钮位于可见区域之外,我想滚动到它:

ScrollView {
    anchors.fill:parent
    ListView {
        anchors.fill: parent
        model: valuesList
        delegate: RadioButton {
            id: radioBtn
            //check of value is index type and do the corresponding checked? test
            checked: valueIsIndex ? (parseInt(valueFromParent) == index ? true : false) : (valueFromParent == valueString ? true : false)
            onClicked: {
                 root.selected(valueString, index)
            }    
            Component.onCompleted: {
                 if(checked) 
                //Here i want to scroll the list to display this radiobutton
            }
        }
    }
}

任何想法如何滚动列表?我在hightlightscontentY玩了很多但没什么用。

我使用ScrollView周围的ListView自动获取桌面上的系统滚动条。在移动设备上,我只有轻弹ListView

修改

我在BaCaRoZzo的帮助下得到了它。这是我目前的工作示例:

ScrollView {
    id: scrollView
    anchors.fill:parent
    property int yOfCheckedRadioButton: 0
    ListView {
        id:listView
        anchors.fill: parent
        spacing: Math.round(appWindow.height*0.05)
        model: internalValuesList
        delegate: RadioButton {
            id: radioBtn
            //check of value is index type and do the corresponding checked? test
            checked: checktest()
            style: MyRadioButtonStyle {
                myRadioBtn: radioBtn
                labelString: value
            }
            Component.onCompleted: {
                //set the position of the checked RadioButton to scroll to it later onContentHeightChange
                if(checked) {
                    var checkedRadioBtnPositionY = Math.round((radioBtn.height + listView.spacing) * index - radioBtn.height * 1.5)
                    if( checkedRadioBtnPositionY > 0)
                        scrollView.yOfCheckedRadioButton = checkedRadioBtnPositionY
                    else
                        scrollView.yOfCheckedRadioButton = 0
                }
            }
        }
        onContentHeightChanged: {
            //scroll to the checked RadioButton
            contentY = scrollView.yOfCheckedRadioButton
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我在Qt 5.4之前回想起滚动的问题,当时我找到了一个解决方法,如:

        ScrollView {
            anchors.fill: parent // mind how you stretch it
            contentItem:
                Flow {
                    id: flow
                    spacing: 10 // mind gaps
                    width: parent.parent.width - 20 // select proper width

                    // Put anything you would like to scroll in here
                    // Mind that Flow positions items one after another
                    // left to right, top to bottom

                    // You can also try containers other than Flow
                    // but whether it works or not may depend on Qt version

                    ExclusiveGroup { id: tabPositionGroup }
                    RadioButton {
                        text: "RB1"
                        checked: true
                        exclusiveGroup: tabPositionGroup
                    }
                    RadioButton {
                        text: "RB2"
                        exclusiveGroup: tabPositionGroup
                    }
                }
        }

ScrollView是否需要显式的contentItem是另一回事,它当然可能不需要它,但如果SrollView需要解析实际滚动的内容,那就不会受到影响。