Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 为什么可以';我不能在ColumnLayout中使用滚动视图吗?_Qt_Qml - Fatal编程技术网

Qt 为什么可以';我不能在ColumnLayout中使用滚动视图吗?

Qt 为什么可以';我不能在ColumnLayout中使用滚动视图吗?,qt,qml,Qt,Qml,我有一个围绕列表视图的滚动视图。但当我把它放在ColumnLayout中时,ListView就消失了 我的实际代码更大、更复杂,但我已将问题简化为这个小示例 import QtQuick 2.11 import QtQuick.Window 2.11 import QtQuick.Layouts 1.11 import QtQuick.Controls 2.4 Window { visible: true width: 640 height: 480 title

我有一个围绕列表视图的滚动视图。但当我把它放在ColumnLayout中时,ListView就消失了

我的实际代码更大、更复杂,但我已将问题简化为这个小示例

import QtQuick 2.11
import QtQuick.Window 2.11
import QtQuick.Layouts 1.11
import QtQuick.Controls 2.4

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    ListModel {
        id: theModel
        ListElement { display: "one" }
        ListElement { display: "two" }
        ListElement { display: "three" }
        ListElement { display: "four" }
        ListElement { display: "five" }
    }

    ColumnLayout {
        ScrollView
        {
            width: 150
            height: 150
            clip: true
            ListView {
                model: theModel
                anchors.fill: parent
                delegate: Column {
                    TextField {
                        text: display
                    }
                }
            }
        }
        Rectangle {
            color: "black"
            width: 100
            height: 30
        }
    }
}
如果没有ColumnLayout和矩形,我会得到一个可滚动的窗口,如预期的那样显示ListView的一部分。但是,如果包含它们,除了矩形上方的一些空白之外,没有任何ListView的迹象。

A调整其所有子项的大小(例如,
ColumnLayout
调整子项的高度,
RowLayout
调整子项的宽度),因此您应该使用
Layout
来指示如何布局它们,而不是设置大小。e、 g

ScrollView {
    Layout.maximumHeight: 150 // height will be updated according to these layout properties

    width: 150
    clip: true
    ListView {
        model: theModel
        anchors.fill: parent
        delegate: Column {
            TextField {
                text: display
            }
        }
    }
}
调整其所有子项的大小(例如,
ColumnLayout
调整子项的高度,
RowLayout
调整子项的宽度),因此您应该使用
Layout
来指示如何布局它们,而不是设置大小。e、 g

ScrollView {
    Layout.maximumHeight: 150 // height will be updated according to these layout properties

    width: 150
    clip: true
    ListView {
        model: theModel
        anchors.fill: parent
        delegate: Column {
            TextField {
                text: display
            }
        }
    }
}

布局会更改其子布局的大小和位置。但当我指定孩子的尺寸时,我只想改变位置。定位器用于此操作(具体而言,是柱而不是柱布局)。此外,我还没有设置父布局(/Positioner)的大小,所以我现在使用anchors.fill:parent进行设置

    Column {
        anchors.fill: parent
        ScrollView
        {
            width: 150
            height: 150
            clip: true
            ListView {
                model: theModel
                anchors.fill: parent
                delegate: Column {
                    TextField {
                        text: display
                    }
                }
            }
        }
        Rectangle {
            color: "black"
            width: 100
            height: 30
        }
    }

感谢他人的评论和回答,帮助我认识到这一点

布局更改其子级的大小和位置。但当我指定孩子的尺寸时,我只想改变位置。定位器用于此操作(具体而言,是柱而不是柱布局)。此外,我还没有设置父布局(/Positioner)的大小,所以我现在使用anchors.fill:parent进行设置

    Column {
        anchors.fill: parent
        ScrollView
        {
            width: 150
            height: 150
            clip: true
            ListView {
                model: theModel
                anchors.fill: parent
                delegate: Column {
                    TextField {
                        text: display
                    }
                }
            }
        }
        Rectangle {
            color: "black"
            width: 100
            height: 30
        }
    }

感谢他人的评论和回答,帮助我认识到这一点

我宁愿为该ColumnLayout定义大小(宽度/高度或其他方式),然后使嵌套项适合。现在我们有一个没有大小的ColumnLayout,而且它也没有被定位在那个窗口中。很好@alexander-v。我逐渐掌握了这个窍门!我在下面的回答中使用了anchors.fill:parent。我更愿意定义该ColumnLayout的大小(宽度/高度或其他方式),然后制作嵌套项。现在我们有一个没有大小的ColumnLayout,而且它也没有被定位在那个窗口中。很好@alexander-v。我逐渐掌握了这个窍门!我在下面的回答中使用了锚。填充:家长。谢谢!这让我意识到我根本不想要布局,我想要一个定位器。(即柱而不是柱布局)。请参见下面的答案。@NigelSharp您的评论似乎与出版物的标题相矛盾:我根本不想要版面-为什么我不能在列版面中使用滚动视图?谢谢!这让我意识到我根本不想要布局,我想要一个定位器。(即柱而不是柱布局)。请看下面的回答。@NigelSharp您的评论似乎与您出版物的标题相矛盾:我根本不想要布局-为什么我不能在列布局中使用滚动视图?选择定位器或布局时有一些原则,但最重要的是,如果容器的大小是指定的(由其自身或其父级指定),你应该考虑一个布局,否则,如果一个容器的大小是从它的孩子计算出来的,那么在大多数情况下,一个定位器将是合适的。<代码>锚。填充< /代码>在这里没有多大意义,除非你希望占用比它的孩子所需要的更多的空间。如果没有它,它将隐含宽度为150(其子容器的最大宽度)和高度为180(其子容器的高度之和)。在选择定位器或布局时,有一些原则,但最重要的是,如果容器的大小(由其自身或其父容器指定)的话你应该考虑一个布局,否则,如果一个容器的大小是从它的孩子计算出来的,那么一个定位器将在大多数情况下适合。<>代码>锚。填充< /代码>在这里没有多大意义,除非你希望占用比它的孩子所需要的更多的空间。没有它,它将隐含地有150的宽度。(其子对象的最大宽度)和180(其子对象的高度之和)的高度。