gpt4 book ai didi

qt - 当选项卡到另一个组件位置时,QML 中相应的滚动

转载 作者:行者123 更新时间:2023-12-02 03:30:40 24 4
gpt4 key购买 nike

我想要做的是,如果我从 TextField 切换到另一个组件(ComboBox wtv),我希望滚动能够适应这一点。

当我认为这非常重要时,当我执行连续选项卡时,我会转到 ScrollView 显示的控件下方。

举个例子,假设我在这里

enter image description here现在我做了 2 个选项卡,然后转到

enter image description here

这里应该调整滚动以至少显示 TextField 完整。像这样

enter image description here

我现在提供了一个简约的代码来显示与图像相同的内容:

import QtQuick 2.9
import QtQuick.Controls 2.2

ApplicationWindow {
id: window
title: "Stack"
visible: true
height: 200
width: 400
ListModel {
id: libraryModel
ListElement {
text: "A Masterpiece"
}
ListElement {
text: "Brilliance"
}
ListElement {
text: "Outstanding"
}
}

Item {
id: page
anchors.fill: parent
width:parent.width
height: parent.height
ScrollView {
anchors.fill:parent
width:parent.width
height: parent.height
Column{
width:parent.width
spacing:10
TextField {
id:textField
implicitHeight: 30
font.bold: true
}
ComboBox {
id:comboBox
anchors.topMargin: 10
textRole: "text"
model: libraryModel
}
TextField {
id:textField2
anchors.topMargin: 10
implicitHeight: 30
font.bold: true
}
ComboBox {
id:comboBox2
anchors.topMargin: 10
textRole: "text"
model: libraryModel
}
TextField {
id:textField3
anchors.topMargin: 10
implicitHeight: 30
font.bold: true
}
ComboBox {
id:comboBox3
anchors.topMargin: 10
textRole: "text"
model: libraryModel
}
TextField {
id:textField4
anchors.topMargin: 10
implicitHeight: 30
font.bold: true
}
ComboBox {
id:comboBox4
anchors.topMargin: 10
textRole: "text"
model: libraryModel
}
}
}
}
}

最佳答案

我看到的唯一方法是手动滚动到获得焦点的项目。

您可以将此函数添加到您的 ScrollView 中(使用 id:scrollView)

// For QtQuick.Controls 2.2
function scrollToY(y) {
scrollView.contentItem.contentY = y;
}

// For QtQuick.Controls 1.4
function scrollToY(y) {
scrollView.flickableItem.contentY = y;
}

然后你需要在每个项目获得焦点时调用它:

TextField {
id:textField3
anchors.topMargin: 10
implicitHeight: 30
font.bold: true
onFocusChanged: if(focus) { scrollView.scrollToY(y); }
}

通过传递元素的 y,您会将视口(viewport)滚动到元素的顶部。对于不同的行为,您需要从 y 计算您需要的任何 y 位置

关于qt - 当选项卡到另一个组件位置时,QML 中相应的滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53634108/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com