注:  该博文为原创博文,转载请注明,摘用请随意;

qml自带的滚动条不太好用;

首先,利用canvas绘制滚动条两端的箭头:

import QtQuick 2.0

Canvas {
width: 20;
height: 20;
property real dir: 0; //0: up; 1: right; 2: down; 3: left;
onPaint: {
var ctx = getContext("2d")
ctx.fillStyle = Qt.rgba(0.1,0.1,0.1,0.7);
ctx.clearRect(0,0,width,height)
ctx.fillRect(0,0,width,height)
ctx.beginPath();
switch(dir)
{
case 0: //up;
ctx.moveTo(width/2,1/4 * height);
ctx.lineTo(width/4,3/4 * height);
ctx.lineTo(width * 3/4, 3/4 * height);
ctx.lineTo(width/2,1/4 *height);
break;
case 1: //right;
ctx.moveTo(width * 3/4,1/2 * height);
ctx.lineTo(width/4,1/4 * height);
ctx.lineTo(width/4, 3/4 * height);
ctx.lineTo(width * 3/4,1/2 *height);
break;
case 2: //down;
ctx.moveTo(width/2,3/4 * height);
ctx.lineTo(width * 3/4,1/4 * height);
ctx.lineTo(width * 1/4, 1/4 * height);
ctx.lineTo(width/2,3/4 *height);
break;
case 3: //left;
ctx.moveTo(width/4,1/2 * height);
ctx.lineTo(width *3/4,3/4 * height);
ctx.lineTo(width * 3/4, 1/4 * height);
ctx.lineTo(width/4,1/2 *height);
break;
}
ctx.closePath();
ctx.fillStyle = Qt.rgba(1,1,1,0.7);
ctx.fill();
}
}

然后,自定义整个滚动条:

import QtQuick 2.0

Rectangle {
property Item attachItem;
property bool bHScroll: false;
property real barSize : 20;
property bool bSpace: false; //是否留空;
property real size: 1; color:Qt.rgba(0.1,0.1,0.1,0.1);
border.color:Qt.rgba(0.8,0.8,0.8,0.8);
border.width: 1;
width: bHScroll ? attachItem.width : barSize;
height: bHScroll ? barSize : attachItem.height;
anchors{
left: bHScroll ? attachItem.left : undefined;
right: attachItem.right;
top: bHScroll? undefined : attachItem.top;
bottom: attachItem.bottom;
rightMargin: bHScroll? (bSpace? barSize: 0): undefined;
bottomMargin: bHScroll? undefined: (bSpace? barSize: 0)
}
function scroll(dir)
{
var step; if( bHScroll)
{
step= (scrollBtnArea.width - btnRect.width)/10;
if( dir < 0 ) //left scroll;
btnRect.x= btnRect.x - step > 0? btnRect.x - step : 0;
else
btnRect.x = btnRect.x + step < scrollBtnArea.width - btnRect.width? btnRect.x + step : scrollBtnArea.width - btnRect.width ;
}else{
step = (scrollBtnArea.height - btnRect.height)/10;
if( dir < 0 )
btnRect.y = btnRect.y - step >0 ? btnRect.y - step: 0 ;
else
btnRect.y = btnRect.y + step < scrollBtnArea.height - btnRect.height? btnRect.y + step : scrollBtnArea.height - btnRect.height;
}
} Arrow{
id: arrow1;
anchors{
left: parent.left;
top: parent.top;
bottom: bHScroll ? parent.bottom : undefined;
right: bHScroll ? undefined:parent.right;
}
width: barSize;
height: barSize;
dir: bHScroll? 3: 0;
MouseArea{
anchors.fill: parent;
onClicked:{
var step =2;
if(bHScroll)
{
btnRect.x = btnRect.x - step > 0? btnRect.x - step : 0;
}else{
btnRect.y = btnRect.y - step >0 ? btnRect.y - step: 0 ;
}
}
}
} Rectangle{
id: scrollBtnArea;
color:Qt.rgba(0.1,0.1,0.1,0.7);
anchors{
left: bHScroll? arrow1.right : parent.left;
right: bHScroll? arrow2.left : parent.right;
top: bHScroll? parent.top: arrow1.bottom;
bottom: bHScroll? parent.bottom : arrow2.top;
}
Rectangle{
id: btnRect;
x: bHScroll? 0 : 2;
y: bHScroll? 2: 0;
width: bHScroll? parent.width/size : barSize -4;
height: bHScroll? barSize -4 : parent.height/size;
color:Qt.rgba(0.1,0.1,0.1,0.7);
border.color: Qt.rgba(1,1,1,0.2);
border.width: 1;
onXChanged:{
if(bHScroll)
{
var children = attachItem.children;
for( var child in children)
{
var ratio =btnRect.x/(arrow2.x - btnRect.width)
children[child].x= - ratio * ( size - 1) * attachItem.width ;
console.log("x changed:"+ btnRect.x +";"+arrow1.x+";"+arrow1.width+";"+arrow2.x +";" + btnRect.width+";"+ratio + ";"+ children[child].x +";" + size + ";" + attachItem.width);
}
}
}
onYChanged:{
console.log("y changed");
if( !bHScroll)
{
var children = attachItem.children;
for( var child in children)
{
children[child].y = -btnRect.y/(arrow2.y - btnRect.height) * ( size- 1) * attachItem.height;
}
}
}
} MouseArea{
id:mouseFunc;
hoverEnabled: true;
anchors.fill: parent;
acceptedButtons: Qt.AllButtons
drag.target: btnRect;
drag.axis: bHScroll? Drag.XAxis : Drag.YAxis;
drag.minimumX: 0
drag.maximumX: bHScroll? arrow2.x - btnRect.width: 0;
drag.minimumY: 0
drag.maximumY: bHScroll? 0: arrow2.y - btnRect.height ;
onWheel:{
console.log(" mouse wheel event")
var step;
if( bHScroll )
{
step= (parent.width - btnRect.width)/10;
if( wheel.angleDelta.y > 0 )
btnRect.x= btnRect.x - step > 0? btnRect.x - step : 0;
else
btnRect.x = btnRect.x + step < parent.width - btnRect.width? btnRect.x + step : parent.width - btnRect.width ;
}else
{
step = (parent.height - btnRect.height)/10;
if(wheel.angleDelta.y >0 )
btnRect.y = btnRect.y - step >0 ? btnRect.y - step: 0 ;
else
btnRect.y = btnRect.y + step < parent.height - btnRect.height? btnRect.y + step : parent.height - btnRect.height;
}
}
onClicked:{
var step
if(bHScroll)
{
step= (parent.width - btnRect.width)/10;
if( mouseX < btnRect.x)
{
btnRect.x = btnRect.x - step > 0? btnRect.x - step : 0;
}else if( mouseX > btnRect.x )
{
btnRect.x = btnRect.x + step < parent.width - btnRect.width? btnRect.x + step : parent.width - btnRect.width ;
}
}else{
step = (parent.height - btnRect.height)/10;
if( mouseY < btnRect.y )
{
btnRect.y = btnRect.y - step >0 ? btnRect.y - step: 0 ;
}else if(mouseY > btnRect.y){
btnRect.y = btnRect.y + step < parent.height - btnRect.height? btnRect.y + step : parent.height - btnRect.height;
}
}
}
}
} Arrow{
id: arrow2;
anchors{
left: bHScroll?undefined: parent.left;
top: bHScroll? parent.top: undefined;
bottom: parent.bottom;
right: parent.right;
}
width: barSize;
height: barSize;
dir: bHScroll?1:2;
MouseArea{
anchors.fill: parent;
onClicked:{
var step = 2;
if(bHScroll)
{
btnRect.x = btnRect.x + step < scrollBtnArea.width - btnRect.width? btnRect.x + step : scrollBtnArea.width -btnRect.width;
}else{
btnRect.y = btnRect.y + step < scrollBtnArea.height - btnRect.height? btnRect.y + step : scrollBtnArea.height - btnRect.height;
}
}
}
}
}    

使用实例:

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2 Window {
visible: true
width: 640
height: 700
title: qsTr("Hello World") Rectangle {
id: frame
clip: true
width: 200
height: 200
border.color: "black"
anchors.centerIn: parent
Image {
id: content;
source: "tvline.jpg"
width: 500;
height: 500;
}
MouseArea{
anchors.fill: parent;
onWheel:{
if(wheel.modifiers & Qt.ControlModifier)
{
if( wheel.angleDelta.y > 0)
hscroll.scroll(-1);
else
hscroll.scroll(1);
return;
}
//单纯的滚动;
if( wheel.angleDelta.y > 0)
vscroll.scroll(-1)
else
vscroll.scroll(1)
}
}
}
UI_ScrollBar{
id: vscroll;
visible: content.height > frame.height;
attachItem: frame;
bHScroll: false;
bSpace: hscroll.visible;
size: content.height/frame.height;
} UI_ScrollBar{
id:hscroll;
visible:content.width > frame.width;
attachItem: frame;
bHScroll: true;
bSpace: vscroll.visible;
size: content.width/frame.width;
} }

  

qml: 自定义滚动条;的更多相关文章

  1. CSS3自定义滚动条样式 -webkit-scrollbar(转)

    有没有觉得浏览器自带的原始滚动条很不美观,同时也有看到很多网站的自定义滚动条显得高端,就连chrome32.0开发板都抛弃了原始的滚动条,美观多了.那webkit浏览器是如何自定义滚动条的呢? 前言 ...

  2. jquery自定义滚动条 鼠标移入或滚轮时显示 鼠标离开或悬停超时时隐藏

    一.需求: 我需要做一个多媒体播放页面,左侧为播放列表,右侧为播放器.为了避免系统滚动条把列表和播放器隔断开,左侧列表的滚动条需要自定义,并且滚动停止和鼠标离开时要隐藏掉. 二.他山之石: 案例来自h ...

  3. 利用JS实现自定义滚动条

    一般默认的滚动条会比较丑,我们可以用简单的js实现自定义滚动条的功能: 代码如下: <!doctype html> <html> <head> <meta c ...

  4. javascript 学习之自定义滚动条加滚轮事件

    要自己写一个自定义滚动条加上滚轮事件,之前的没有滚轮事件不完整,今天整理了一个. 1.滚轮事件是不兼容的,firefox中是必需要用事件绑定的添加,用的DOMMouseScroll,当滚动鼠标的时候, ...

  5. 自定义滚动条 - mCustomScrollbar

    项目中需要使用自定义滚动条,但是试用了很多都功能不够全,今天发现一个比较全而且用法很简单的 -- mCustomScrollbar http://manos.malihu.gr/jquery-cust ...

  6. Flex:自定义滚动条样式/隐藏上下箭头

    Flex组件自定义滚动条的实现 .scrollBar{ downArrowUpSkin:Embed(source="img/mainLeftScrollBar/bar_bottom.png& ...

  7. javascript自定义滚动条插件,几行代码的事儿

    在实际项目中,经常由于浏览器自带的滚动条样式太戳,而且在各个浏览器中显示不一样,所以我们不得不去实现自定义的滚动条,今天我就用最少的代码实现了一个自定义滚动条,代码量区区只有几十行,使用起来也非常方便 ...

  8. jQuery自定义滚动条样式插件mCustomScrollbar

    如果你构建一个很有特色和创意的网页,那么肯定希望定义网页中的滚动条样式,这方面的 jQuery 插件比较不错的,有两个:jScrollPane 和 mCustomScrollbar. 关于 jScro ...

  9. WPF 自定义滚动条样式

    先看一下效果: 先分析一下滚动条有哪儿几部分组成: 滚动条总共有五部分组成: 两端的箭头按钮,实际类型为RepeatButton Thumb 两端的空白,实际也是RepeatButton 最后就是Th ...

随机推荐

  1. Modbus CRC 16 (C#)

    算法 1.预置一个值为 0xFFFF 的 16 位寄存器,此寄存器为 CRC 寄存器. 2.把第 1 个 8 位二进制数据(即通信消息帧的第 1 个字节)与 16 位的 CRC 寄存器相异或,异或的结 ...

  2. codeforces982F

    The Meeting Place Cannot Be Changed CodeForces - 982F Petr is a detective in Braginsk. Somebody stol ...

  3. 自己实现strchr函数与strstr函数

    char* my_strchr(char* str, int i) { if (NULL == str) { return NULL; } while ('\0' != *str && ...

  4. 使用binlog,实现MySQL数据恢复

    mysql的binlog日志,用于记录数据库的增.删.改等修改操作,默认处于关闭状态.使用binlog实现数据恢复的条件为 1.binlog日志功能已开启 2.若binlog在数据库创建一段时候后开启 ...

  5. SPOJ10707 COT2 - Count on a tree II 【树上莫队】

    题目分析: 考虑欧拉序,这里的欧拉序与ETT欧拉序的定义相同而与倍增LCA不同.然后不妨对于询问$u$与$v$让$dfsin[u] \leq dfsin[v]$,这样对于u和v不在一条路径上,它们可以 ...

  6. 洛谷P2678跳石头题解

    题目 这个题也是一个很经典的题了.其主要思想也是二分答案,原因就是题目中只要出现最大值最小或最小值最大,这种描述十有八九就是二分答案. 这个题原题也是让我们求最短的跳跃距离的最大值. 显而易见,最大值 ...

  7. Modelsim脚本仿真文件分析

    Do文件的实质是tcl脚本,本质上是ascii文件.所以扩展名可以任意的,不一定叫xx.do,也可以叫xx.tcl,甚至叫成你的名字也没有关系.看自己喜好,一般取do,不过我喜欢取成tcl,用代码编辑 ...

  8. 【XSY2469】graph 分治 并查集

    题目大意 给你一张\(n\)个点\(m\)条边的无向图,问删去每个点后,原图是不是二分图. \(n,m\leq 100000\) 题解 一个图是二分图\(\Longleftrightarrow\)该图 ...

  9. Git回滚代码暴力法

    Git回滚有多种方式,这里使用的是[强制提交到远程分支] 效果为:如回滚前的提交记录是 1.2.3.4,使用这种方法回滚到2,那么提交记录就变成了1.2. 操作方法: 需要在本地的Git仓库,右键选择 ...

  10. 【BZOJ1831】[AHOI2008]逆序对(动态规划)

    [BZOJ1831][AHOI2008]逆序对(动态规划) 题面 BZOJ 洛谷 题解 显然填入的数拎出来是不降的. 那么就可以直接大力\(dp\). 设\(f[i][j]\)表示当前填到了\(i\) ...