【问题标题】:Bounds of Flickable.contentY in QMLQML 中 Flickable.contentY 的边界
【发布时间】:2013-05-03 00:40:14
【问题描述】:

询问 Flickable.contentY 边界的正确方法是什么?我需要它作为滚动条。

实验发现

offsetY <= contentY <= offsetY + contentHeight - height

其中 offsetY 可以计算为

var offsetY = contentY-Math.round(visibleArea.yPosition*contentHeight)

offsetY 在应用程序开始时为零,并且似乎是恒定的,除非调整 Flickable 的大小。

这个公式通常有效,但可能应该有一个专门的函数。

【问题讨论】:

  • 为什么需要这个?我在 QML 中做了几个滚动条,我们所需要的只是在 visibleArea ...

标签: qml qt-quick flickable


【解决方案1】:

我很容易地做了一个没有偏移的滚动条:

// 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滚动时自动移动。

【讨论】:

    猜你喜欢
    • 2017-02-19
    • 2015-01-15
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多