How do you tell flickable to move to the bottom of the current page. QML - Move to top #TOP does not provide an answer for this.
You will need to calculate it and set it to contentY. For e.g:
Flickable {
width: 200; height: 200
contentWidth: image.width; contentHeight: image.height
Image { id: image; source: "file:///path/toBigImageFile" }
contentY : contentHeight-height
}
I kept adding text to a TextArea and wanted it to stay at the bottom unless someone changed the scroll position.
Flickable{
id: flickable
anchors.fill: parent
boundsBehavior: Flickable.DragAndOvershootBounds
flickableDirection: Flickable.VerticalFlick
ScrollBar.vertical: ScrollBar {
id: flickScroll
}
TextArea.flickable: TextArea{
id: monitor
width: parent.width
height: parent.height
readOnly: true
wrapMode: TextArea.Wrap
persistentSelection: true
leftPadding: 6
rightPadding: 6
topPadding: 0
bottomPadding: 0
background: null
}
function currPos(){
return flickable.contentY
}
function setPos(pos){
flickable.contentY = pos;
}
function getEndPos(){
var ratio = 1.0 - flickable.visibleArea.heightRatio;
var endPos = flickable.contentHeight * ratio;
return endPos;
}
function scrollToEnd(){
flickable.contentY = getEndPos();
}
function append(text){
var pos, endPos, value;
value = monitor.text + String(text);
// Limit value size here
endPos = getEndPos();
pos = currPos();
monitor.text = value;
if(pos == endPos){
scrollToEnd();
} else {
setPos(pos);
}
}
}
Related
I'm trying to make zoomable Item (Image or any other component) and make it for tablet. Currently, my code looks like this:
Rectangle {
id: root
anchors.fill: parent
color: mainWindow.toGradient
Flickable {
id: flick
anchors.fill: parent
contentWidth: width
contentHeight: height
boundsBehavior: Flickable.StopAtBounds
PinchArea {
id: pArea
width: Math.max(flick.contentWidth, flick.width)
height: Math.max(flick.contentHeight, flick.height)
property real initialWidth
property real initialHeight
onPinchStarted: {
initialWidth = flick.contentWidth
initialHeight = flick.contentHeight
}
onPinchUpdated: {
flick.contentX += pinch.previousCenter.x - pinch.center.x
flick.contentY += pinch.previousCenter.y - pinch.center.y
flick.resizeContent(initialWidth * pinch.scale, initialHeight * pinch.scale, pinch.center)
}
onPinchFinished: {
flick.returnToBounds()
}
Rectangle {
width: flick.contentWidth
height: flick.contentHeight
color: "black"
Image {
anchors.fill: parent
source: "qrc:/image.jpg"
}
MouseArea {
anchors.fill: parent
onDoubleClicked: {
flick.contentWidth = root.width
flick.contentHeight = root.height
}
}
}
}
}
}
It works good, but when it's over all screen, i can still zoom out (make it smaller, that parent).
How can I stop zooming out (and if possible zooming in too) at some point/scale?
Thank you for your help!
In the end, I entrily removed Flickable and scaled Item instead.
Rectangle {
id: root
anchors.fill: parent
color: "black"
Item {
id: photoFrame
width: root.width
height: root.height
Image {
id: image
anchors.fill: parent
source: "image.jpg"
}
PinchArea {
anchors.fill: parent
pinch.target: photoFrame
pinch.minimumRotation: 0 //Rotation (i guess we don't need to rotate it)
pinch.maximumRotation: 0
pinch.minimumScale: 1 //Minimum zoom for camera
pinch.maximumScale: 4 //Maximum zoom for camera
onPinchUpdated: { //Dont zoom behind border
if(photoFrame.x < dragArea.drag.minimumX)
photoFrame.x = dragArea.drag.minimumX
else if(photoFrame.x > dragArea.drag.maximumX)
photoFrame.x = dragArea.drag.maximumX
if(photoFrame.y < dragArea.drag.minimumY)
photoFrame.y = dragArea.drag.minimumY
else if(photoFrame.y > dragArea.drag.maximumY)
photoFrame.y = dragArea.drag.maximumY
}
MouseArea {
id: dragArea
hoverEnabled: true
anchors.fill: parent
drag.target: photoFrame
scrollGestureEnabled: false // 2-finger-flick gesture should pass through to the Flickable
drag.minimumX: (root.width - (photoFrame.width * photoFrame.scale))/2
drag.maximumX: -(root.width - (photoFrame.width * photoFrame.scale))/2
drag.minimumY: (root.height - (photoFrame.height * photoFrame.scale))/2
drag.maximumY: -(root.height - (photoFrame.height * photoFrame.scale))/2
onDoubleClicked: { //Reset size and location of camera
photoFrame.x = 0
photoFrame.y = 0
photoFrame.scale = 1
}
onWheel: {
var scaleBefore = photoFrame.scale
photoFrame.scale += photoFrame.scale * wheel.angleDelta.y / 120 / 10
if(photoFrame.scale < 1)
photoFrame.scale = 1
else if(photoFrame.scale > 4)
photoFrame.scale = 4
if(photoFrame.x < drag.minimumX)//Dont zoom behind border of image
photoFrame.x = drag.minimumX
else if(photoFrame.x > drag.maximumX)
photoFrame.x = drag.maximumX
if(photoFrame.y < drag.minimumY)
photoFrame.y = drag.minimumY
else if(photoFrame.y > drag.maximumY)
photoFrame.y = drag.maximumY
}
}
}
}
}
I have a TextEdit inside a ScrollView. How can I implement logic so that the ScrollView moves when the user presses the up or down arrow key and it moves the text beyond the ScrollView bounds?
//qml
ScrollView {
id: palGenTextScrollView
property int scrollBarWidth: 15
anchors.fill: parent
MouseArea {
id: mouseArea
anchors.fill: parent
onWheel: {
if (wheel.modifiers & Qt.ControlModifier){
if (wheel.angleDelta.y > 0)
{
mainTextEdit.font.pixelSize++
}
else
{
mainTextEdit.font.pixelSize--
}
}
else{
wheel.accepted=false
}
}
}
TextEdit {
id: mainTextEdit
text: fileio.palFileText
font.family: "Courier"
wrapMode: TextEdit.Wrap
selectByMouse: true
//when going out of upward bounds: palGenTextScrollView.flickableItem.contentY--
//when going out of downward bounds: palGenTextScrollView.flickableItem.contentY++
}
}
You can use TextArea which does exactly that for you. If you want to bake your own, take a look at the flickable example in TextEdit's docs in the detailed description.
Note that the TextEdit does not implement scrolling, following the cursor, or other behaviors specific to a look-and-feel. For example, to add flickable scrolling that follows the cursor:
Flickable {
id: flick
width: 300; height: 200;
contentWidth: edit.paintedWidth
contentHeight: edit.paintedHeight
clip: true
function ensureVisible(r)
{
if (contentX >= r.x)
contentX = r.x;
else if (contentX+width <= r.x+r.width)
contentX = r.x+r.width-width;
if (contentY >= r.y)
contentY = r.y;
else if (contentY+height <= r.y+r.height)
contentY = r.y+r.height-height;
}
TextEdit {
id: edit
width: flick.width
height: flick.height
focus: true
wrapMode: TextEdit.Wrap
onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
}
}
I have two images:
1. normal image
2. same image with the ability to zoom.
I have 4 draggable shapes (A,B,C,D) on second image (image with the ability to zoom).
I want to display position of mouse on normal image (with draw a shape (canvas3)) when the position of A/B/C/D shapes changed on the zoomed image.
I used the following code but it does not show correct position. In fact, I want user can see position of mouse.
property int xwheel: 0
property int ywheel: 0
property alias source :image.source
id:mywin
property int highestZ: 0
property real defaultSize: mywin.width
property real surfaceViewportRatio: 1.5
ScrollView {
anchors.fill: parent
flickableItem.interactive: true
frameVisible: true
highlightOnFocus: true
style: ScrollViewStyle {
transientScrollBars: true
.........
}
Flickable {
id: flick
anchors.fill: parent
contentWidth: parent.width
contentHeight: parent.height
Rectangle {
id: photoFrame
width: parent.width
height: parent.height
scale:defaultSize / parent.width
Behavior on scale { NumberAnimation { duration: 200 } }
Behavior on x { NumberAnimation { duration: 200 } }
Behavior on y { NumberAnimation { duration: 200 } }
smooth: true
antialiasing: true
Image {
id:image
anchors.fill: parent
fillMode: Image.PreserveAspectFit
smooth: true
}
PinchArea {
anchors.fill: parent
pinch.target: photoFrame
pinch.minimumRotation: -360
pinch.maximumRotation: 360
pinch.minimumScale: 0.1
pinch.maximumScale: 10
pinch.dragAxis: Pinch.XAndYAxis
property real zRestore: 0
onSmartZoom: {
.....
}
MouseArea {
id: dragArea
hoverEnabled: true
anchors.fill: parent
drag.target: photoFrame
scrollGestureEnabled: false
onPressed: {
photoFrame.z = ++mywin.highestZ;
}
onWheel: {
...
}
}
Point {
id: pointA
onDragged: {
xwheel=pointA.x;
ywheel=pointA.y;
canvas3.requestPaint()
}
}
//Point B
//Point D
//Pint C
}
}
}
Image {
id:mainimage
width: parent.width/4
height: parent.height/4
smooth: true
source: image.source
Canvas {
id: canvas3
width: parent.width
height: parent.height
onPaint: {
var context = getContext("2d");
// Make canvas all white
......
context.beginPath();
//Style
.......
context.moveTo(xwheel/4, ywheel/4);
context.arc(xwheel/4, ywheel/4, 5, 0, 2*Math.PI, true)
context.fill();
context.stroke();
}
}
}
I have one DropArea and two elements. I want DropArea reject the drop event if the DropArea already got one element be dropped, the another element not allow drop into, unless the first one move out.
DropArea {
property bool dropped: false
onDropped: {
drop.accepted = !dropped;
dropped = true;
}
onExited: dropped = false
}
But looks like drop.accepted not work,
BTW anyway to get the objects was dropped in DropArea
You should control if the item must be dropped or not in onReleased, checking the dropped property.
Full example:
import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
Window {
id: win
visible: true
width: 800
height: 600
title: qsTr("Hello World")
Repeater {
model: 10
Rectangle {
id: rect
width: 50
height: 50
z: mouseArea.drag.active || mouseArea.pressed ? 2 : 1
color: Qt.rgba(Math.random(), Math.random(), Math.random(), 1)
x: Math.random() * (win.width / 2 - 100)
y: Math.random() * (win.height - 100)
property point beginDrag
property bool caught: false
border { width:2; color: "white" }
radius: 5
Drag.active: mouseArea.drag.active
Text {
anchors.centerIn: parent
text: index
color: "white"
}
MouseArea {
id: mouseArea
anchors.fill: parent
drag.target: parent
onPressed: {
rect.beginDrag = Qt.point(rect.x, rect.y);
}
onReleased: {
if(!rect.caught || dragTarget.dropped) {
backAnimX.from = rect.x;
backAnimX.to = beginDrag.x;
backAnimY.from = rect.y;
backAnimY.to = beginDrag.y;
backAnim.start()
}
parent.Drag.drop()
console.log("MouseArea - containsDrag " + dragTarget.dropped)
}
}
ParallelAnimation {
id: backAnim
SpringAnimation { id: backAnimX; target: rect; property: "x";
duration: 500; spring: 2; damping: 0.2 }
SpringAnimation { id: backAnimY; target: rect; property: "y";
duration: 500; spring: 2; damping: 0.2 }
}
}
}
Rectangle {
anchors {
top: parent.top
right: parent.right
bottom: parent.bottom
}
width: parent.width / 2
color: "gold"
DropArea {
id: dragTarget
anchors.fill: parent
property bool dropped: false
onEntered: {
console.log("onEntered " + containsDrag)
drag.source.caught = true;
}
onExited: {
console.log("onExited " + containsDrag)
dropped = false;
}
onDropped:
{
console.log("onDropped");
dropped = true;
}
}
}
}
Use drop.accept() instead. The above can be done as follows:
property bool containsItem: false
DropArea {
id: dropArea
anchors.fill: parent
onDropped: {
if(containsItem)
drop.accept(Qt.IgnoreAction)
else
drop.accept()
containsItem = true;
}
}
Also donot use dropped property as it is already an attached property inside onDropped event handler.
Edit:
If rect is the Item to be dragged and dropped then:
Rectangle {
id: rect
width: 40; height: 40
color: "red"
Drag.active: dragArea.drag.active
Drag.hotSpot.x: 20
Drag.hotSpot.y: 20
MouseArea {
id: dragArea
anchors.fill: parent
drag.target: parent
onReleased: if (rect.Drag.drop() !== Qt.IgnoreAction) {
console.log("Accepted!");
} else {
console.log("Rejected!");
}
}
}
I want to have a single (vertical) ListView with (horizontal) ListView delegates.
The horizontal delegates should scroll synchronously. To do so, I put a Flickable on top of the ListViews and bind the contentX of the horizontal ListView to the contentX of the Flickable (and the same for the contentY of the vertical ListView) (Note: Here a different approach was described for the synchronous ListView scrolling but this seems to have performance issues on mobile devices)
The code below kind of works but still has the following issues
I don't get the onClicked in the Rectangle (I do get it when I remove the top Flickable)
I want either horizontal flicking or vertical flicking but not both at the same time. I can restrict the flicking of the top Flickable by setting flickableDirection: Flickable.HorizontalFlick but then I can't flick vertically anymore (I was hoping that the Flickable would pass on unused mouse events to the vertical ListView but this doesn't seem to happen)
Suggestions on how to fix these issues?
Any help appreciated!
import QtQuick 2.0
Item {
id: main
visible: true
width: 600
height: 600
ListView {
id: verticalList
width: parent.width;
height: parent.height;
contentY : flickable.contentY
anchors.fill: parent
spacing: 10
orientation: ListView.Vertical
model: 100
delegate:
ListView {
id: horizontalList
width: parent.width;
height: 100;
contentX : flickable.contentX
spacing: 10
orientation: ListView.Horizontal
model: 20
property var verticalIndex : index
delegate:
Rectangle
{
property var colors : ['red', 'green', 'blue']
property var widths : [100, 200, 300]
height: 100
width: widths[(verticalIndex + model.index) % widths.length]
color: colors[model.index % colors.length]
MouseArea {
anchors.fill: parent
onClicked: {
console.log("Rectangle.onClicked")
}
}
}
}
}
//on top a Flickable
Flickable {
id: flickable
height: parent.height
width: parent.width
contentHeight: 100*100 //nrOfRows * rowHeight
contentWidth: 20*300 //nrOfEvent * max/averageEventWidth
}
}
I'm not giving you a perfect solution, but it's working. When you are using Flickable on the top of the ListView, you are not able to interact with it. So, I've used Flickable bellow the ListView and bounded the contentX of Flickable and ListView, but this is causing a loop and I'm getting the following output, but we're getting the desired behavior.
QML Binding: Binding loop detected for property "value"
EDIT
So, instead of using ListView for vertical list, I just used a Repeater and Column and used property binding. It's working well now.
Following is the updated version.
import QtQuick 2.0
Item {
id: main
visible: true
width: 600
height: 600
property bool virticalFlick: false //To get either vertical or horizontal flicking
Flickable {
anchors.fill: parent
contentWidth: contentItem.childrenRect.width
contentHeight: contentItem.childrenRect.height
flickableDirection: Flickable.VerticalFlick
interactive: (virticalFlick === true)?true:false
Column {
id: column
spacing: 10
Repeater {
id: repeater
model: 20
ListView {
id: horizontalList
width: 600;
height: 100;
clip: true
interactive: (virticalFlick === true)?false:true
spacing: 10
orientation: ListView.Horizontal
model: 20
property var verticalIndex : index
onMovingChanged: {
if(moving == true) {
for(var i=0; i<repeater.count ; i++) {
/* If the property is later assigned a static value from a JavaScript statement,
this will remove the binding.
However if the intention is to create a new binding then the property
must be assigned a Qt.binding() value instead. This is done by passing a function to
Qt.binding() that returns the desired result */
if (i !== index)
repeater.itemAt(i).contentX = Qt.binding(function() { return contentX });
}
}
else {
for(var i=0; i<repeater.count ; i++) {
if (i !== index)
repeater.itemAt(i).contentX = contentX; // This will remove binding
}
}
}
delegate: Rectangle {
property var colors : ['red', 'green', 'blue']
property var widths : [100, 200, 300]
height: 100
width: widths[(ListView.view.verticalIndex + model.index) % widths.length]
color: colors[model.index % colors.length]
MouseArea {
anchors.fill: parent
onClicked: {
console.log("Rectangle.onClicked")
}
}
}
}
}
}
}
}
The following does work, however the initial attempt seemed more elegant.
I still need to compare the performance (fps) when flicking, especially on a mobile device. I also get "Binding loop" warnings but I think they are false positives.
import QtQuick 2.0
Item {
id: main
visible: true
width: 600
height: 600
ListView {
id: verticalList
width: parent.width;
height: parent.height;
anchors.fill: parent
spacing: 10
cacheBuffer: 500 // in pixels
orientation: ListView.Vertical
model: 100
property var activeIndex : 0
property var lastContentX : 0
delegate:
ListView {
id: horizontalList
width: parent.width;
height: 100;
spacing: 10
cacheBuffer: 500 // in pixels
orientation: ListView.Horizontal
model: 20
property var verticalIndex : index
delegate:
Rectangle
{
property var colors : ['red', 'green', 'blue']
color: colors[model.index % colors.length]
height: 100
property var widths : [100, 200, 300]
width: widths[(verticalIndex + model.index ) % widths.length]
MouseArea {
z:10
anchors.fill: parent
onClicked: {
console.log("Rectangle.onClicked")
}
onPressed: {
console.log("Rectangle.onPressed")
}
onReleased: {
console.log("Rectangle.onReleased")
}
}
}
onContentXChanged: {
if(model.index === verticalList.activeIndex)
{
verticalList.lastContentX = contentX
}
}
onMovementStarted: {
verticalList.activeIndex = model.index
unbind();
}
onMovementEnded: {
bind();
}
Component.onCompleted: {
bind();
}
function bind()
{
contentX = Qt.binding(function() { return verticalList.lastContentX })
}
function unbind()
{
contentX = contentX ;
}
}
}
}
The following modifications were needed to my initial attempt
limit the Flickable to flickableDirection : Flickable.HorizontalFlick and remove the contentY : flickable.contentY on the verticalList
by doing so, there is no vertical scrolling anymore. This can be fixed by moving the Flickable inside the ListView
onClicked events are received by adding the following MouseArea to the Flickable
eg.
MouseArea {
anchors.fill: parent
//see http://stackoverflow.com/questions/29236762/mousearea-inside-flickable-is-preventing-it-from-flicking
onReleased: {
if (!propagateComposedEvents) {
propagateComposedEvents = true
}
}
}