Qt Qml:包含ListView的ScrollView不调整滚动条句柄

Qt Qml:包含ListView的ScrollView不调整滚动条句柄,qt,listview,qml,scrollview,Qt,Listview,Qml,Scrollview,我正在尝试根据需要向ListView添加垂直滚动条。有人告诉我,最好的方法是将ListView放在ScrollView中,而不是在ListView中插入滚动条(如中),因为这将使GPU更高效 我插入了它,如下面的例子所示——但无论我尝试了什么,如果滚动条显示,它的手柄总是占据整个高度,当然不会移动 我希望您能看看我的样品,并给我一个建议,为什么滚动条显示不正确。 代码中有一些注释解释了我做了什么以及为什么 import QtQuick 2.0 import QtQuick.Controls 1

我正在尝试根据需要向ListView添加垂直滚动条。有人告诉我,最好的方法是将ListView放在ScrollView中,而不是在ListView中插入滚动条(如中),因为这将使GPU更高效

我插入了它,如下面的例子所示——但无论我尝试了什么,如果滚动条显示,它的手柄总是占据整个高度,当然不会移动

我希望您能看看我的样品,并给我一个建议,为什么滚动条显示不正确。
代码中有一些注释解释了我做了什么以及为什么

import QtQuick 2.0
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4

Item
{
    readonly property int parentWidth: 280
    readonly property int parentMaxHeight: 400

    // Main reason for doing this - the items are custom objects and
    // their width does not automatically adjust for having the scroll bar or not
    // But also, to set scroll bars because Qt.ScrollBarAsNeeded makes them not show
    property bool scrollBarVisible: myListView.contentHeight > myListView.height

    width: parentWidth
    height: parentMaxHeight

    Rectangle
    {
        id: myMenuRect

        anchors.rightMargin: 2
        anchors.leftMargin: 2
        anchors.bottomMargin: 4
        anchors.topMargin: 4

        width: parentWidth
        height: myListView.height
        radius: 10
        z: 2

        color: "red"   // Adding this to show why the height of the rectangle must match the listview
    }

    ScrollView
    {
        id: myScrollView
        parent: myMenuRect
        anchors.fill: parent

        anchors.topMargin: 5
        anchors.bottomMargin: 5
        anchors.rightMargin: 5

        frameVisible: false

        // I have tried to set implicitHeight in many different ways,
        // no matter what I do the scroll bar handle occupies the enire bar and doesn't move

        // The Qt.ScrollBarAsNeeded didn't work... so I did this
        verticalScrollBarPolicy: scrollBarVisible ? Qt.ScrollBarAlwaysOn : Qt.ScrollBarAlwaysOff

    // Adding colors on scrollbar to show which part is showing     
    style: ScrollViewStyle
    {
        handle: Rectangle
        {
            implicitWidth: 10
            implicitHeight: 2
            radius: 10
            anchors.leftMargin: 1
            anchors.left: parent.left
            color: "yellow"
        }

        scrollBarBackground: Rectangle
        {
            implicitWidth: 12
            anchors.right: parent.right
            color: "green"
        }
    }

    ListView
    {
        id: myListView
        parent: myScrollView
        model: wifiComboListModel

        focus: true
        clip: true
        interactive: false

        width: parent.width

        // I am trying to tell my view to take the minimum space it needs that is below 
        // a certain height. Ignore the "myListView." prefixes here, I know they are not needed but 
        // make it easier to move this outside if needed
        height: (myListView.contentHeight > 0 ?
                     (myListView.contentHeight < parentMaxHeight ?
                          myListView.contentHeight : parentMaxHeight) : 0)

        // I made this as simple as possible, without affecting "quality"
        delegate: Text
        {
            text: _comboBoxText
            height: 70
            width: parent.width - 20
        }
    }

    ListModel
    {
        id: wifiComboListModel
    }

    // I want to populate my model from outside, not be static. Not sure if this affects the bars
    function populateComboBoxListModel()
    {
        wifiComboListModel.clear();
        for (var itemIndex = 0; itemIndex < listItems.length; itemIndex++)
        {
            wifiComboListModel.append
                ({
                    _id: itemIndex,
                    _comboBoxText: listItems[itemIndex]
                });
        }
    }

    Component.onCompleted:
    {
        populateComboBoxListModel();
    }

    property var listItems: [
            "This",
            "annoying",
            "list",
            "view",
            "does",
            "not behave the way",
            "I expect.",
            "I",
            "tried many",
            "things,",
            "now I am",
            "begging for your",
            "help",
            "."
    ]
}
导入QtQuick 2.0
导入QtQuick.Controls 1.4
导入QtQuick.Controls.Styles 1.4
项目
{
只读属性int parentWidth:280
只读属性int parentMaxHeight:400
//这样做的主要原因-项目是自定义对象和
//它们的宽度不会因是否有滚动条而自动调整
//但是,由于Qt.scrollbarasneed而设置滚动条也会使它们不显示
属性布尔滚动条可见:myListView.contentHeight>myListView.height
宽度:parentWidth
高度:parentMaxHeight
矩形
{
id:myMenuRect
1.5.2.2.2
1.leftMargin:2
下边距:4
1.topMargin:4
宽度:parentWidth
高度:myListView.height
半径:10
z:2
颜色:“红色”//添加此选项以显示矩形的高度必须与listview匹配的原因
}
卷轴视图
{
id:myScrollView
家长:myMenuRect
锚定。填充:父级
1.5页边距:5
下边距:5
5.右页边距:5
frameVisible:false
//我试着用很多不同的方法设定隐式八,
//无论我做什么,滚动条手柄都会占据enire条并且不会移动
//Qt.scrollbarasneed不起作用…所以我做了这个
垂直滚动条策略:滚动条可见?Qt.ScrollBarAlwaysOn:Qt.ScrollBarAlwaysOff
//在滚动条上添加颜色以显示正在显示的零件
样式:ScrollViewStyle
{
句柄:矩形
{
宽度:10
八:2
半径:10
1.leftMargin:1
anchors.left:parent.left
颜色:“黄色”
}
滚动背景:矩形
{
宽度:12
anchors.right:父项.right
颜色:“绿色”
}
}
列表视图
{
id:myListView
父项:myScrollView
型号:wifiComboListModel
焦点:正确
剪辑:对
交互:错误
宽度:parent.width
//我试图告诉我的观点,它需要的最小空间如下
//一定高度。忽略“myListView”。这里的前缀,我知道它们不是必需的,但是
//如果需要,可以更轻松地将其移出
高度:(myListView.contentHeight>0?
(myListView.contentHeight是否小于parentMaxHeight?
myListView.contentHeight:parentMaxHeight:0)
//我尽可能简单,不影响“质量”
代表:文本
{
文本:_comboBoxText
身高:70
宽度:parent.width-20
}
}
列表模型
{
id:wifiComboListModel
}
//我想从外部填充我的模型,而不是静态的。不确定这是否会影响条形图
函数populateComboxListModel()
{
wifiComboListModel.clear();
对于(var itemIndex=0;itemIndex
您在myMenuRect中有一个高度绑定循环。出现这种情况是因为myMenuRect依赖于列表视图的高度,反之亦然。修复后,它似乎可以工作:

导入QtQuick 2.0
导入QtQuick.Controls 1.4
应用程序窗口
{
只读属性int parentWidth:280
只读属性int parentMaxHeight:400
可见:正确
//这样做的主要原因-项目是自定义对象和
//它们的宽度不会因是否有滚动条而自动调整
//但是,由于Qt.scrollbarasneed而设置滚动条也会使它们不显示
属性布尔滚动条可见:myListView.contentHeight>myListView.height
宽度:parentWidth
高度:parentMaxHeight
矩形
{
id:myMenuRect
1.5.2.2.2
1.leftMargin:2
下边距:4
1.topMargin:4
宽度:parentWidth
高度:parentMaxHeight
半径:10
z:2
}
卷轴视图
{
id:myScrollView
家长:myMenuRect
锚定。填充:父级
1.5页边距:5
下边距:5
5.右页边距:5
frameVisible:false
//我试着用很多不同的方法设定隐式八,
//无论我做什么,滚动条手柄都会占据enire条并且不会移动
//Qt.scrollbarasneed不起作用…所以我做了这个
垂直滚动条策略:滚动条可见?Qt.ScrollBarAlwaysOn:Qt。
import QtQuick 2.0 
import QtQuick.Controls 1.4

ApplicationWindow
{
    readonly property int parentWidth: 280
    readonly property int parentMaxHeight: 400
    visible: true

    // Main reason for doing this - the items are custom objects and
    // their width does not automatically adjust for having the scroll bar or not
    // But also, to set scroll bars because Qt.ScrollBarAsNeeded makes them not show
    property bool scrollBarVisible: myListView.contentHeight > myListView.height

    width: parentWidth
    height: parentMaxHeight

    Rectangle
    {
        id: myMenuRect

        anchors.rightMargin: 2
        anchors.leftMargin: 2
        anchors.bottomMargin: 4
        anchors.topMargin: 4

        width: parentWidth
        height: parentMaxHeight
        radius: 10
        z: 2
    }

    ScrollView
    {
        id: myScrollView
        parent: myMenuRect
        anchors.fill: parent

        anchors.topMargin: 5
        anchors.bottomMargin: 5
        anchors.rightMargin: 5

        frameVisible: false

        // I have tried to set implicitHeight in many different ways,
        // no matter what I do the scroll bar handle occupies the enire bar and doesn't move

        // The Qt.ScrollBarAsNeeded didn't work... so I did this
        verticalScrollBarPolicy: scrollBarVisible ? Qt.ScrollBarAlwaysOn : Qt.ScrollBarAlwaysOff

        ListView
        {
            id: myListView
            model: wifiComboListModel

            focus: true
            clip: true
            interactive: false

            width: parent.width

            // I am trying to tell my view to take the minimum space it needs that is below
            // a certain height. Ignore the "myListView." prefixes here, I know they are not needed but
            // make it easier to move this outside if needed
            height: (myListView.contentHeight > 0 ?
                         (myListView.contentHeight < parentMaxHeight ?
                              myListView.contentHeight : parentMaxHeight) : 0)

            // I made this as simple as possible, without affecting "quality"
            delegate: Text
            {
                text: _comboBoxText
                height: 70
                width: parent.width - 20
            }
        }
    }

    ListModel
    {
        id: wifiComboListModel
    }

    // I want to populate my model from outside, not be static. Not sure if this affects the bars
    function populateComboBoxListModel()
    {
        wifiComboListModel.clear();
        for (var itemIndex = 0; itemIndex < listItems.length; itemIndex++)
        {
            wifiComboListModel.append
                ({
                    _id: itemIndex,
                    _comboBoxText: listItems[itemIndex]
                });
        }
    }

    Component.onCompleted:
    {
        populateComboBoxListModel();
    }

    property var listItems: [
            "This",
            "annoying",
            "list",
            "view",
            "does",
            "not behave the way",
            "I expect.",
            "I",
            "tried many",
            "things,",
            "now I am",
            "begging for your",
            "help",
            "."
    ]
}
ListView
{
    parent: myScrollView
ScrollView
{
    id: myScrollView
    parent: myMenuRect
    anchors.fill: parent

    ListView
    {
        id: myListView
        model: wifiComboListModel