gpt4 book ai didi

qt - 如何在 QML 中制作可调整大小的矩形?

转载 作者:行者123 更新时间:2023-12-03 05:52:32 25 4
gpt4 key购买 nike

我正在寻找一种在QQuickItem中创建矩形的简单方法。我想调整大小,然后像这样拖动这个矩形的边框(位于 resizable QRubberBand )

enter image description here

有人有想法吗?

最佳答案

可能有几种方法可以达到预期的结果。由于我考虑过为我的裁剪工具软件实现类似的 Component,因此我将分享一个使用部分代码的玩具示例。

与示例中的橡皮筋不同,我的矩形一次只能在一个轴上调整大小。我相信您可以在此基础上进行构建并自定义代码来满足您的需求。

该代码的基本思想是利用 drag MouseArea 的属性。它可用于在矩形 周围移动,并与MouseX 结合使用。和 MouseY属性,调整其大小。

拖动在矩形内处于事件状态,而调整大小在位于矩形两侧的旋钮上处于事件状态(为简洁起见,未设置鼠标光标更改) .

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
}
}
}
}
}
}
}

示例截图: enter image description here

关于qt - 如何在 QML 中制作可调整大小的矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29087710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com