Qt 如何垂直滚动到特定项目
我有一个Qt 如何垂直滚动到特定项目,qt,listview,qml,qtquick2,Qt,Listview,Qml,Qtquick2,我有一个列表视图,包括许多单选按钮。列表大于可见区域。其中一个单选按钮已选中。有时,如果所选单选按钮在可见区域之外,我想滚动到它: ScrollView { anchors.fill:parent ListView { anchors.fill: parent model: valuesList delegate: RadioButton { id: radioBtn //check
列表视图
,包括许多单选按钮。列表大于可见区域。其中一个单选按钮已选中。有时,如果所选单选按钮在可见区域之外,我想滚动到它:
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
}
}
}
}
你知道如何滚动列表吗?我已经玩了很多关于hightlights
和contentY
的游戏,但都没用
我已经在列表视图
周围使用了滚动视图
,以自动获取桌面上的系统滚动条。在移动设备上,我只有flickable列表视图
编辑
在巴卡罗佐的帮助下,我在路上得到了它。以下是我当前的工作示例:
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
}
}
}
我记得Qt 5.4之前的scroll出现了以下问题:
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需要解决它实际滚动的内容,这并没有什么坏处。我回忆起Qt 5.4之前的scroll问题,当时我发现了一个解决方法,如:
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需要解决它实际滚动的内容,则这不会有任何影响。
ContentY
应该可以工作(例如所示)。您当然应该考虑这里是否存在ListView
。作为一个基本测试,我在ListView
的末尾尝试了以下内容:Component.onCompleted:{contentY=200}
,但这也不会改变任何事情。我做错了什么?没有。谢谢。我意识到这取决于我的模型。当我使用一个静态的,比如你的例子,它就像一个符咒。但是当我在组件
完成后动态创建它时,滚动动作是无用的。因此,我需要根据以下内容滚动:onContentHeightChanged
。现在我只需要找出如何计算选中单选按钮的位置…没问题。您可以尝试将代码减少到最低限度,并编辑问题。这可能值得一试。:)<代码>内容应该可以工作(例如,如所示)。您当然应该考虑这里是否存在ListView
。作为一个基本测试,我在ListView
的末尾尝试了以下内容:Component.onCompleted:{contentY=200}
,但这也不会改变任何事情。我做错了什么?没有。谢谢。我意识到这取决于我的模型。当我使用一个静态的,比如你的例子,它就像一个符咒。但是当我在组件
完成后动态创建它时,滚动动作是无用的。因此,我需要根据以下内容滚动:onContentHeightChanged
。现在我只需要找出如何计算选中单选按钮的位置…没问题。您可以尝试将代码减少到最低限度,并编辑问题。这可能值得一试。:)当我试图将滚动视图
环绕列表视图
时,它似乎不起作用。我试图将列表视图
放入流
中,但它只显示了一个单选按钮。如果您添加一些单选按钮,如固定示例中所示,会怎么样?马上去流动?我对不同类型的内容使用了这种模式,包括动态内容。我猜我错过了流的宽度。也已修复。您是否考虑为动态创建的单选按钮使用中继器
?是的,我们可以使用中继器,尽管它有点不方便。首先创建一些项目,然后使用itemAt(index)检查它们并设置它们各自的属性。当我试图将滚动视图
包装在列表视图
周围时,这似乎不起作用。我试图将列表视图
放入流
中,但它只显示了一个单选按钮。如果您添加一些单选按钮,如固定示例中所示,会怎么样?马上去流动?我对不同类型的内容使用了这种模式,包括动态内容。我猜我错过了流的宽度。也已修复。您是否考虑为动态创建的单选按钮使用中继器
?是的,我们可以使用中继器,尽管它有点不方便。首先创建一些项目,然后使用itemAt(index)对它们进行检查,并设置它们各自的属性。