前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Qm之滚动视图

Qm之滚动视图

作者头像
Qt君
发布2023-03-17 14:35:02
4210
发布2023-03-17 14:35:02
举报
文章被收录于专栏:跟Qt君学编程跟Qt君学编程

❝ScrollView(滚动视图)类似于Qt 5版本的ScrollView,它提供用户定义内容的滚动。兼容Qt4与Qt5版本。❞

1. 演示

  • 可实现轻弹和拖动的功能。

2. 例子

代码语言:javascript
复制
ScrollView {
    width: 640
    height: 480

    Image {
        source: "./qthub_com.png"
    }
}

3. 使用场景

  1. 一般用在显示的内容尺寸大于实际显示的尺寸时使用。
  2. 图片显示器。

4. 源码

代码语言:javascript
复制
/**
 * Author: Qt君
 * WebSite: qthub.com
 * Email: 2088201923@qq.com
 * QQ交流群: 732271126
 * 关于更多: 扫码关注微信公众号: [Qt君] 第一时间获取最新推送.
 */
import QtQuick 2.0

Item {
    default property alias items: view.children
    anchors.fill: parent

    Flickable {
        id: view
        anchors.fill: parent
        contentWidth: _private.getMax().maxWidth
        contentHeight: _private.getMax().maxHeight
        clip: true

        property int childX: visibleArea.xPosition * contentWidth
        property int childY: visibleArea.yPosition * contentHeight

        onChildXChanged: {
            for (var i = 0; i < items.length; i++)
                items[i].x = -childX
        }

        onChildYChanged: {
            for (var i = 0; i < items.length; i++)
                items[i].y = -childY
        }

    }

    Loader {
        id: horizontalBar
        anchors.bottom: view.bottom
        sourceComponent: scrollBar

        Component.onCompleted: horizontalBar.item.orientation = Qt.Horizontal
    }

    Loader {
        id: verticalBar
        anchors.right: view.right
        sourceComponent: scrollBar

        Component.onCompleted: verticalBar.item.orientation = Qt.Vertical
    }

    property Component scrollBar:
        Rectangle {
            id: root
            property Flickable target: view
            property int orientation: Qt.Vertical
            /*
                orientation : enumeration
                This property holds the orientation of the scroll bar.
                Possible values:
                |Constant     |Description|
                |Qt.Horizontal|Horizontal|
                |Qt.Vertical  |Vertical(default)|
            */
            width: orientation == Qt.Vertical ? radius : target.width - radius
            height: orientation == Qt.Vertical ? target.height - radius : radius
            color: "white"
            opacity: 0.3
            radius: 10

            Rectangle {
                y: parent.orientation == Qt.Vertical ? target.visibleArea.yPosition * (target.height - parent.radius) : 0
                x: parent.orientation == Qt.Vertical ? 0 : target.visibleArea.xPosition * (target.width - parent.radius)
                width: parent.orientation == Qt.Vertical ? parent.width :
                        target.visibleArea.widthRatio * (target.width - parent.radius)
                height: parent.orientation == Qt.Vertical ? target.visibleArea.heightRatio * (target.height - parent.radius) :
                         parent.height
                color: "black"
                radius: parent.radius
                opacity: 0.7
            }
        }

    QtObject {
        id: _private
        function getMax() {
            var maxWidth = 0;
            var maxHeight = 0;

            for (var i = 0; i < items.length; i++) {
                if (items[i].width > maxWidth)
                    maxWidth = items[i].width

                if (items[i].height > maxHeight)
                    maxHeight = items[i].height
            }

            return {"maxWidth": maxWidth, "maxHeight": maxHeight}
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-12-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Qt君 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 演示
  • 2. 例子
  • 3. 使用场景
  • 4. 源码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档