【问题标题】:How to make a resizable rectangle when drag corners of rectangle in qml在qml中拖动矩形的角时如何制作可调整大小的矩形
【发布时间】:2018-06-14 10:02:15
【问题描述】:

我正在为我的示例寻找解决方案。我想拖动矩形的一个角,它会改变比例,但是当我拖动一个角来缩小矩形时,它会出错。

请给出一个解决方案。非常感谢!

================================================ ==================================================== ==================================================== ===

我的代码:

import QtQuick 2.9
import QtQuick.Window 2.2

Window {
    title: qsTr("Test Crop")
    width: 640
    height: 480
    visible: true
    property double photohfake: selComp.height
    property double photowfake: selComp.width
    property double photowold: selComp.width
    property double photohold: selComp.height


    Image {
        id: image1
        anchors.fill: parent
        source: "http://dongdomobile.vn/wp-content/uploads/2014/09/Cute-Grey-Seamless-Pattern-For-Website-Background-300x136.jpg"
        Rectangle {
            x:parent.width / 4
            y: parent.height / 4
            width: parent.width / 2
            height: parent.width / 2
            id: selComp
            border {
                width: 2
                color: "steelblue"
            }
            color: "#354682B4"
            Rectangle {
                width: 18
                height: 18
                color: "steelblue"
                anchors.verticalCenter:parent.top
                anchors.horizontalCenter: parent.left
                MouseArea {
                    anchors.fill: parent
                    drag{ target: parent; axis: Drag.XAndYAxis }
                    onPositionChanged: {
                        console.log("mouseX "+mouseX)
                        console.log("mouseY "+mouseY)
                        if(drag.active){
                            photohfake=photohfake-mouseY //calculator new height
                            selComp.scale=selComp.scale/photohold*photohfake //calculator new scale
                            photowfake=photowold*photohfake/photohold //calculator new width
                            selComp.x+=(photowold-photowfake)/2
                            selComp.y+=mouseY/2
                            console.log("photohfake "+photohfake)
                            console.log("photohold "+photohold)
                            photohold=photohfake
                            photowold=photowfake
                            console.log("selComp.scale "+selComp.scale)
                            if(selComp.scale<0.5)
                                selComp.scale=0.5
                            else if(selComp.scale>4)
                                selComp.scale=4

                        }
                    }
                }
            }
        }
    }
}

【问题讨论】:

  • 你遇到了什么错误?
  • 矩形的形状发生了奇怪的变化!

标签: image qt qml scale


【解决方案1】:

如果错误是指矩形在调整大小时奇怪地移动,这应该可以解决它:

import QtQuick 2.9
import QtQuick.Window 2.2

Window {
    title: qsTr("Test Crop")
    width: 640
    height: 480
    visible: true

    Rectangle {
        id: image1
        anchors.fill: parent
        color: "lightgrey"
        Rectangle {
            x:parent.width / 4
            y: parent.height / 4
            width: parent.width / 2
            height: parent.width / 2
            id: selComp
            border {
                width: 2
                color: "steelblue"
            }
            color: "#354682B4"
            Rectangle {
                width: 18
                height: 18
                color: "steelblue"
                anchors.verticalCenter:parent.top
                anchors.horizontalCenter: parent.left
                MouseArea {
                    anchors.fill: parent
                    drag{ target: parent; axis: Drag.XAndYAxis }
                    onPositionChanged: {
                        if(drag.active){
                            var delta = Math.max(mouseX, mouseY)
                            var newWidth = selComp.width - delta
                            var newHeight = selComp.height - delta;

                            if (newWidth < width || newHeight < height)
                                return

                            selComp.width = newWidth
                            selComp.x = selComp.x + delta

                            selComp.height = newHeight
                            selComp.y = selComp.y + delta
                        }
                    }
                }
            }
        }
    }
}

【讨论】:

  • 非常感谢!但我希望矩形保持纵横比并返回矩形的比例值。
猜你喜欢
  • 2015-05-19
  • 1970-01-01
  • 1970-01-01
  • 2022-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多