Русскоязычный портал о QML и QtQuick, QmlPortal What the duck
[ Новые сообщения · Участники · Правила форума · Поиск · RSS ]
  • Страница 1 из 1
  • 1
Форум QML » QML форум » Вопросы по QML » Не получается настроить ScrollBar (Не получается настроить ScrollBar)
Не получается настроить ScrollBar
RomkoДата: Вторник, 18.10.2011, 12:11 | Сообщение # 1
Постоялец
Группа: Проверенные
Сообщений: 22
Награды: 0
Репутация: 2
Статус: Offline
Здравствуйте, у меня не совсем получается настроить 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 }
     }
}


Заранее большое спасибо)
 
CYANДата: Среда, 19.10.2011, 21:06 | Сообщение # 2
Администратор
Группа: Администраторы
Сообщений: 21
Награды: 1
Статус: Offline
Попробуйте как-нибудь вот так:

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 {
         id: slider
         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

         MouseArea {
             anchors.fill: parent
             drag.target: parent
             drag.axis: Drag.YAxis
             drag.minimumY: 0
             drag.maximumY: (container.height-slider.height)

             onPositionChanged: {
                 if (pressedButtons == Qt.LeftButton) {
                     scrollArea.contentY = (container.orientation == Qt.Vertical ?
                    (slider.y * scrollArea.contentHeight / container.height) :
                    (slider.x * scrollArea.contentWidth / container.width))
                 }
             }
         }
     }

     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 }
     }
}
 
RomkoДата: Понедельник, 24.10.2011, 02:47 | Сообщение # 3
Постоялец
Группа: Проверенные
Сообщений: 22
Награды: 0
Репутация: 2
Статус: Offline
Спасибо большое. У меня ещё один вопрос, я добавил этой строчке
Code
Rectangle { anchors.fill: parent; color: "Black"; opacity: 0.3 }

MouseArea, так как она является задним фоном бегунка.
То есть получилось:
Code
      Rectangle {id:background; anchors.fill: parent;
                 color: "white"; opacity: 0.3; radius: 15

          MouseArea{anchors.fill: parent;
              onClicked: {scrollArea.contentY = (mouse.y * scrollArea.contentHeight / container.height)}
          }
      }

Но теперь я никак не могу добится плавности передвежения ListView. Подскажите пожалуйста каким образом можно этого добится.
 
CYANДата: Понедельник, 24.10.2011, 21:25 | Сообщение # 4
Администратор
Группа: Администраторы
Сообщений: 21
Награды: 1
Статус: Offline
Добавьте в Ваш ListView (id: list) что-нибудь наподобие этого и поэкспериментируйте:

Code
Behavior on contentY {
                 NumberAnimation { easing.type: Easing.OutElastic; easing.amplitude: 3.0; easing.period: 2.0; duration: 300 }
}
 
Форум QML » QML форум » Вопросы по QML » Не получается настроить ScrollBar (Не получается настроить ScrollBar)
  • Страница 1 из 1
  • 1
Поиск: