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

标签 qt qml

我想要做的是,如果我从 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/

相关文章:

qt - Q_GADGET 未知方法返回类型

c++ - Moodle REST 网络服务 : Problems sending an array as POST parameter

c++ - 如何让 QML 容器中的最后一项填充剩余空间?

c++ - 带有 GUI 的 Qt 多线程

python - 在 Qt gui 中嵌入绘图

c++ - 将Qt qml文件转换为位图图像

Qt linguist lupdate 忽略 qml 文件

qt - 向 QML 布局添加边距

Qt 加载指示器小部件

c++ - QLineEdit 可见宽度设置?