How to make a resizable rectangle in QML?

501 - not implemented

I'm looking for a simple way to create a rectangle in a QQuickItem. I want to resize, and drag the borders of this rectangle like this (found at resizable QRubberBand)

enter image description here

Has someone an idea?

BaCaRoZzo

There are probably several ways to achieve the desired result. Since I've considered the implementation of a similar Component for a cropping tool software of mine, I'm going to share a toy example which uses part of that code.

Differently from the rubber band in the example, my Rectangle is resizable only on a single axis at a time. I'm confident that you can build on that and customise the code to meet your needs.

The basic idea of the code is to exploit the drag property of MouseArea. It can be used to move around the Rectangle and, combined with MouseX and MouseY properties, resize it.

Dragging is active inside the Rectangle whereas resizing is active on the knobs set on the sides of the Rectangle (no mouse cursor change is set for the sake of brevity).

import QtQuick 2.4
import QtQuick.Controls 1.3

ApplicationWindow {
    title: qsTr("Test Crop")
    width: 640
    height: 480
    visible: true
    property var selection: undefined

    Image {
        id: image1
        anchors.fill: parent
        source: "http://cdn.cutestpaw.com/wp-content/uploads/2013/01/l-Kitty-attack.jpg"

        MouseArea {
            anchors.fill: parent
            onClicked: {
                if(!selection)
                    selection = selectionComponent.createObject(parent, {"x": parent.width / 4, "y": parent.height / 4, "width": parent.width / 2, "height": parent.width / 2})
            }
        }
    }

    Component {
        id: selectionComponent

        Rectangle {
            id: selComp
            border {
                width: 2
                color: "steelblue"
            }
            color: "#354682B4"

            property int rulersSize: 18

            MouseArea {     // drag mouse area
                anchors.fill: parent
                drag{
                    target: parent
                    minimumX: 0
                    minimumY: 0
                    maximumX: parent.parent.width - parent.width
                    maximumY: parent.parent.height - parent.height
                    smoothed: true
                }

                onDoubleClicked: {
                    parent.destroy()        // destroy component
                }
            }

            Rectangle {
                width: rulersSize
                height: rulersSize
                radius: rulersSize
                color: "steelblue"
                anchors.horizontalCenter: parent.left
                anchors.verticalCenter: parent.verticalCenter

                MouseArea {
                    anchors.fill: parent
                    drag{ target: parent; axis: Drag.XAxis }
                    onMouseXChanged: {
                        if(drag.active){
                            selComp.width = selComp.width - mouseX
                            selComp.x = selComp.x + mouseX
                            if(selComp.width < 30)
                                selComp.width = 30
                        }
                    }
                }
            }

            Rectangle {
                width: rulersSize
                height: rulersSize
                radius: rulersSize
                color: "steelblue"
                anchors.horizontalCenter: parent.right
                anchors.verticalCenter: parent.verticalCenter

                MouseArea {
                    anchors.fill: parent
                    drag{ target: parent; axis: Drag.XAxis }
                    onMouseXChanged: {
                        if(drag.active){
                            selComp.width = selComp.width + mouseX
                            if(selComp.width < 50)
                                selComp.width = 50
                        }
                    }
                }
            }

            Rectangle {
                width: rulersSize
                height: rulersSize
                radius: rulersSize
                x: parent.x / 2
                y: 0
                color: "steelblue"
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.verticalCenter: parent.top

                MouseArea {
                    anchors.fill: parent
                    drag{ target: parent; axis: Drag.YAxis }
                    onMouseYChanged: {
                        if(drag.active){
                            selComp.height = selComp.height - mouseY
                            selComp.y = selComp.y + mouseY
                            if(selComp.height < 50)
                                selComp.height = 50
                        }
                    }
                }
            }


            Rectangle {
                width: rulersSize
                height: rulersSize
                radius: rulersSize
                x: parent.x / 2
                y: parent.y
                color: "steelblue"
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.verticalCenter: parent.bottom

                MouseArea {
                    anchors.fill: parent
                    drag{ target: parent; axis: Drag.YAxis }
                    onMouseYChanged: {
                        if(drag.active){
                            selComp.height = selComp.height + mouseY
                            if(selComp.height < 50)
                                selComp.height = 50
                        }
                    }
                }
            }
        }
    }
}

A screenshot of the example: enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

javaFX - How to create a resizable rectangle?

From Dev

How to make JScrollPane Resizable

From Dev

How to use a resizable rounded rectangle as KeyIcon

From Dev

How to make canvas Resizable in javaFX?

From Dev

How to make a div resizable for users?

From Dev

How to make a Tkinter window not resizable?

From Dev

How to make image resizable in UIWebView?

From Dev

How to move a rectangle component in qml

From Dev

resizable and movable rectangle

From Java

How to go about drawing the border of a rectangle in a resizable window using pygame?

From Dev

How to make a rectangle of characters?

From Dev

How to make an arc on the rectangle?

From Dev

How can I make a div resizable

From Dev

How to make a dijit/editor manualy resizable?

From Dev

How to make resizable line with jQuery-ui?

From Dev

How to make a background resizable for different screens?

From Dev

How to make a dijit/editor manualy resizable?

From Dev

How to place a QPainter element in QML Rectangle?

From Dev

How to wrap some text in a rectangle in QML?

From Dev

How to place a QPainter element in QML Rectangle?

From Dev

How to make a Round Rectangle JTextField?

From Dev

How to make a rectangle tangent to a circle

From Dev

How to make a Round Rectangle JTextField?

From Dev

How to make QtQuick2.0 application window not resizable?

From Dev

JavaFX nodes - How to make them resizable by the end user?

From Dev

How to make TableLayoutPanel with resizable cells like using Splitter

From Dev

JQuery Resizable : How to make multiples divs resizing independently

From Dev

JavaFX nodes - How to make them resizable by the end user?

From Dev

How to make QtQuick2.0 application window not resizable?

Related Related

HotTag

Archive