前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >兼容Qt4/Qt5版本Qml控件ScrollBar

兼容Qt4/Qt5版本Qml控件ScrollBar

作者头像
Qt君
发布2019-07-15 15:50:26
1.3K0
发布2019-07-15 15:50:26
举报
文章被收录于专栏:跟Qt君学编程跟Qt君学编程

1. ScrollBar演示

2. 对外属性

  • 继承于Rectangle;
  • target属性继承于Flickable(默认值父控件);
  • orientation设置控件水平还是垂直方向(默认值垂直方向).

ScrollBar.qml

代码语言:javascript
复制
import QtQuick 2.0

Rectangle {
    id: root

    property Flickable target: parent // parent: Flickable
    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 ? 15 : target.width
    height: orientation == Qt.Vertical ? target.height : 15
    color: "white"
    opacity: 0.3
    radius: 5

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

3. 使用示例

3.1 图片显示器

代码语言:javascript
复制
import QtQuick 2.0
import "../"

Rectangle {
    anchors.fill: parent

    Flickable {
        id: view
        anchors.fill: parent
        contentWidth: picture.width
        contentHeight: picture.height

        Image {
            id: picture
            source: "Test.png"
            asynchronous: true
        }
    }


    ScrollBar {
        target: view
    }

    ScrollBar {
        target: view
        orientation: Qt.Horizontal
    }
}

3.2 ListView附加滚动条

代码语言:javascript
复制
import QtQuick 2.0
import "../"

ListView {
    anchors.fill: parent

    model: 10
    delegate: Rectangle {
        width: parent.width; height: 100
        color: "lightblue"
        Text {
            anchors.centerIn: parent
            text: index
        }
    }

    ScrollBar { }
}

4. 注意事项

  • 在Qt4下使用需要将QtQuick 2.x改为QtQuick 1.x

5. 源码地址

代码语言:javascript
复制
https://github.com/QtComponent/ScrollBar.git
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-02-20,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. ScrollBar演示
  • 2. 对外属性
  • 3. 使用示例
    • 3.1 图片显示器
      • 3.2 ListView附加滚动条
      • 4. 注意事项
      • 5. 源码地址
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档