我有以下 QML 代码:
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
Window {
id: win
width: 1024
height: 768
visible: true
ScrollView {
id:scrollView
anchors.fill: parent
Rectangle{
id:rect
z:5
color:"red"
width: 2048
height: win.height
border{
color: "black"
width: 2
}
}
}
}
在此代码中,较大的 Rectangle
使水平滚动条正确显示。但是,由于滚动条距离窗口有一定的高度,因此也会出现垂直滚动条。
我怎样才能制作Rectangle
仅填充我的 ScrollView
中的可用空间这样垂直滚动条就不会出现吗?使用类似win.height - <someNumber>
的东西 Not Acceptable 。添加verticalScrollBarPolicy: Qt.ScrollBarAlwaysOff
也是 Not Acceptable ,因为它隐藏了 rect
底部的一些内容.
最佳答案
一般来说,ScrollView
不适合这种用途。它更像是一个布局项目并通过提供的滚动条显示它们的容器。如果绑定(bind)设置不正确,绑定(bind)循环可能会到处弹出。另外 Flickable
+ 自定义滚动条(例如可用的 here )可以完全满足您的需求。
也就是说,viewport
属性为该问题提供了所需的(跨平台)解决方法。文档指出:
The viewport determines the current "window" on the contentItem. In other words, it clips it and the size of the viewport tells you how much of the content area is visible.
因此,子 Item
的 height
可以根据 viewport
的 height
设置。最后一个带有 Image
的简单示例(可爱的小猫传入)如下所示:
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
Window {
id: win
width: 300
height: 300
visible: true
ScrollView {
id:scrollView
anchors.fill: parent
Image{
height: scrollView.viewport.height
source: "http://c1.staticflickr.com/9/8582/16489458700_c9d82954b7_z.jpg"
}
}
}
关于qt - 如何让矩形高度填充ScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32235839/