qt - How to make a resizable rectangle in QML? -
i'm looking simple way create rectangle in qquickitem
. want resize, , drag borders of rectangle (found @ resizable qrubberband
)
has idea?
there several ways achieve desired result. since i've considered implementation of similar component
cropping tool software of mine, i'm going share toy example uses part of code.
differently rubber band in example, rectangle
resizable on single axis @ time. i'm confident can build on , customise code meet needs.
the basic idea of code exploit drag
property of mousearea
. can used move around rectangle
and, combined mousex
, mousey
properties, resize it.
dragging active inside rectangle
whereas resizing active on knobs set on sides of rectangle
(no mouse cursor change set 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 example:
Comments
Post a Comment