询问Flickable.contentY界限的正确方法是什么?我需要它来做滚动条。
我在实验中发现
offsetY <= contentY <= offsetY + contentHeight - height其中offsetY可以计算为
var offsetY = contentY-Math.round(visibleArea.yPosition*contentHeight)offsetY在应用程序开始时为零,并且似乎是恒定的,除非调整了Flickable的大小。
这个公式通常是有效的,但是可能应该有一个专门的函数用于它。
发布于 2013-05-28 19:50:25
我轻而易举地做了一个没有偏移的滚动条:
// ScrollBar.qml
import QtQuick 2.0
Rectangle {
id: scrollbar;
color: "#3C3C3C";
visible: (flicker.visibleArea.heightRatio < 1.0);
property Flickable flicker : null;
width: 20;
anchors {
top: flicker.top;
right: flicker.right;
bottom: flicker.bottom;
}
Rectangle {
id: handle;
height: (scrollbar.height * flicker.visibleArea.heightRatio);
color: "#5E5E5E";
border {
width: 1;
color: "white";
}
anchors {
left: parent.left;
right: parent.right;
}
Binding { // Calculate handle's x/y position based on the content position of the Flickable
target: handle;
property: "y";
value: (flicker.visibleArea.yPosition * scrollbar.height);
when: (!dragger.drag.active);
}
Binding { // Calculate Flickable content position based on the handle x/y position
target: flicker;
property: "contentY";
value: (handle.y / scrollbar.height * flicker.contentHeight);
when: (dragger.drag.active);
}
MouseArea {
id: dragger;
anchors.fill: parent;
drag {
target: handle;
minimumX: handle.x;
maximumX: handle.x;
minimumY: 0;
maximumY: (scrollbar.height - handle.height);
axis: Drag.YAxis;
}
}
}
}就像这样使用它:
Flickable {
id: myFlick;
}
ScrollBar {
flicker: myFlick;
}它可以用鼠标移动,并在滚动Flickable时自动移动。
https://stackoverflow.com/questions/16349893
复制相似问题