Здравствуйте, у меня не совсем получается настроить ScrollBar. Зависимость бегунка от listView есть, а наоборот сделать не получается. То есть нужно что б при передвижении ползунка двигался и listView.
Вот код того что есть:
Main.qml
Code
import QtQuick 1.0
Rectangle {
id: rectangle1
width: 360
height: 360
color: "lightgray"
ListModel {id: contactModel
ListElement {name: "Bill Smith"; number: "555 3264"}
ListElement {name: "John Brown"; number: "555 8426"}
ListElement {name: "Sam Wise"; number: "555 0473"}
ListElement {name: "Bill Smith"; number: "555 3264"}
ListElement {name: "John Brown"; number: "555 8426"}
ListElement {name: "Sam Wise"; number: "555 0473"}
ListElement {name: "Bill Smith"; number: "555 3264"}
ListElement {name: "John Brown"; number: "555 8426"}
ListElement {name: "Sam Wise"; number: "555 0473"}
ListElement {name: "Bill Smith"; number: "555 3264"}
ListElement {name: "John Brown"; number: "555 8426"}
ListElement {name: "Sam Wise"; number: "555 0473"}
}
Component {
id: contactDelegate
Item {
width: 180; height: 40
Column {
Text { text: '<b>Name:</b> ' + name }
Text { text: '<b>Number:</b> ' + number }
}
}
}
ListView {id: list
width: 180; height: 200
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
model: contactModel
delegate: contactDelegate
}
ScrollBar{ scrollArea: list; height: list.height; width: 8
anchors.right: list.right; anchors.top: list.top }
}
ScrollBar.qml
Code
import QtQuick 1.0
Item {
id: container
property variant scrollArea
property variant orientation: Qt.Vertical
function position()
{
var ny = 0;
if (container.orientation == Qt.Vertical)
ny = scrollArea.visibleArea.yPosition * container.height;
else
ny = scrollArea.visibleArea.xPosition * container.width;
if (ny > 2) return ny; else return 2;
}
function size()
{
var nh, ny;
if (container.orientation == Qt.Vertical)
nh = scrollArea.visibleArea.heightRatio * container.height;
else
nh = scrollArea.visibleArea.widthRatio * container.width;
if (container.orientation == Qt.Vertical)
ny = scrollArea.visibleArea.yPosition * container.height;
else
ny = scrollArea.visibleArea.xPosition * container.width;
if (ny > 3) {
var t;
if (container.orientation == Qt.Vertical)
t = Math.ceil(container.height - 3 - ny);
else
t = Math.ceil(container.width - 3 - ny);
if (nh > t) return t; else return nh;
} else return nh + ny;
}
Rectangle { anchors.fill: parent; color: "Black"; opacity: 0.3 }
Rectangle {
color: "blue"; radius: 10
x: container.orientation == Qt.Vertical ? 2 : position()
width: container.orientation == Qt.Vertical ? container.width - 4 : size()
y: container.orientation == Qt.Vertical ? position() : 2
height: container.orientation == Qt.Vertical ? size() : container.height - 4
}
states: State {
name: "visible"
when: container.orientation == Qt.Vertical ? scrollArea.movingVertically : scrollArea.movingHorizontally
PropertyChanges { target: container; opacity: 1.0 }
}
transitions: Transition {
from: "visible"; to: ""
NumberAnimation { properties: "opacity"; duration: 600 }
}
}
Заранее большое спасибо)