简体   繁体   English

QML - 移动到可轻弹的顶部底部

[英]QML - Move to top bottom of flickable

How do you tell flickable to move to the bottom of the current page.你如何告诉 flickable 移动到当前页面的底部。 QML - Move to top #TOP does not provide an answer for this. QML - 移至顶部 #TOP没有为此提供答案。

You will need to calculate it and set it to contentY.您需要计算它并将其设置为 contentY。 For eg:例如:

Flickable {
    width: 200; height: 200
    contentWidth: image.width; contentHeight: image.height

    Image { id: image; source: "file:///path/toBigImageFile" }

    contentY : contentHeight-height
}

I kept adding text to a TextArea and wanted it to stay at the bottom unless someone changed the scroll position.我一直向 TextArea 添加文本,并希望它保持在底部,除非有人更改了滚动位置。

Flickable{
    id: flickable
    anchors.fill: parent

    boundsBehavior: Flickable.DragAndOvershootBounds
    flickableDirection: Flickable.VerticalFlick

    ScrollBar.vertical: ScrollBar {
        id: flickScroll
    }

    TextArea.flickable: TextArea{
        id: monitor
        width: parent.width
        height: parent.height

        readOnly: true
        wrapMode: TextArea.Wrap
        persistentSelection: true
        leftPadding: 6
        rightPadding: 6
        topPadding: 0
        bottomPadding: 0
        background: null
    }

    function currPos(){
        return flickable.contentY
    }

    function setPos(pos){
        flickable.contentY = pos;
    }

    function getEndPos(){
        var ratio = 1.0 - flickable.visibleArea.heightRatio;
        var endPos = flickable.contentHeight * ratio;
        return endPos;
    }

    function scrollToEnd(){
        flickable.contentY = getEndPos();
    }

    function append(text){
        var pos, endPos, value;

        value = monitor.text + String(text);
        // Limit value size here

        endPos = getEndPos();
        pos = currPos();

        monitor.text = value;

        if(pos == endPos){
            scrollToEnd();
        } else {
            setPos(pos);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM