qt - QML-使用箭头键移动项目

标签 qt qml

我使用的是 Windows 8.1 中的 Qt 版本 5.6.0。我绘制一个有 4 个边的形状,并且能够拖动其顶点。我想用箭头键移动形状的顶点。我使用此代码,但它不起作用。 点.qml:

Item {
id: root

signal dragged()
property alias color :point.color
Rectangle {
    id:point
    anchors.centerIn: parent
    width: 20
    height: 20

    opacity: 0.2

    MouseArea {
        anchors.fill: parent
        drag.target: root
        onPositionChanged: {
            if(drag.active) {
                dragged()
            }
        }
        onClicked: point.focus=true;
    }

    //        Keys.onPressed: {
    //            console.log("move");
    //            if (event.key === Qt.Key_Left) {
    //                console.log("move left");
    //                event.accepted = true;
    //                point.x-=1;
    //            }
    //        }
    //        Keys.onRightPressed: {
    //            console.log("move");
    //            point.x+=1;
    //        }

    //        Keys.onLeftPressed: {
    //            console.log("move");
    //            point.x-=1;
    //        }
    Keys.onPressed: {
        switch(event.key) {
        case Qt.Key_Left: point.x-=1;
            break;
        case Qt.Key_Right: point.x+=1;
            break;
        case Qt.Key_Up: point.y-=1;
            break;
        case Qt.Key_Down: point.y+=1;
            break;
        }
    }

    focus: true;
}}

main.qml:

 Point {
    id: pointA
    x: 50
    y: 50
}

Point {
    id: pointB
    x: 250
    y: 50
}

Point {
    id: pointC
    x: 250
    y: 250
}

Point {
    id: pointD
    x: 50
    y: 250
}


Item {
    anchors.fill: parent

    Canvas {
        id: canvas
        anchors.fill: parent
        onPaint: {
            var ctx = canvas.getContext('2d');
            ctx.moveTo(pointA.x, pointA.y);
            ctx.lineTo(pointB.x, pointB.y);
            ctx.lineTo(pointC.x, pointC.y);
            ctx.lineTo(pointD.x, pointD.y);
            ctx.lineTo(pointA.x, pointA.y);
            ctx.stroke();
        }
        Component.onCompleted: {
            pointA.dragged.connect(repaint)
            pointB.dragged.connect(repaint)
            pointC.dragged.connect(repaint)
            pointD.dragged.connect(repaint)
        }

        function repaint() {
            var ctx = getContext("2d");
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            ctx.beginPath();
            requestPaint()
        }
    }
}

更新: main.qml:

PhotoPreview {
                            id : photoPreview
                            anchors.fill : parent
                            //focus:true //visible
                            visible: capture
                        }

照片预览.qml:

Item {
  id:mywin    
  property real defaultSize:mywin.width
property var currentFrame: undefined
property real surfaceViewportRatio: 1.5
focus: true    
ScrollView {
    anchors.fill: parent
    flickableItem.interactive: true
    frameVisible: true
    highlightOnFocus: true           
    Flickable {
        id: flick
        anchors.fill: parent
        contentWidth: parent.width
        contentHeight: parent.height
        property alias source :image.source
        signal closed
        Rectangle {
            id: photoFrame
            width: parent.width
            height: parent.height
            color:"transparent"
            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: {
                    if (pinch.scale > 0) {
                        photoFrame.rotation = 0;
                        photoFrame.scale = Math.min(mywin.width, mywin.height) / Math.max(image.sourceSize.width, image.sourceSize.height) * 0.85
                        photoFrame.x = flick.contentX + (flick.width - photoFrame.width) / 2
                        photoFrame.y = flick.contentY + (flick.height - photoFrame.height) / 2
                        zRestore = photoFrame.z
                        photoFrame.z = ++mywin.highestZ;
                    } else {
                        photoFrame.rotation = pinch.previousAngle
                        photoFrame.scale = pinch.previousScale
                        photoFrame.x = pinch.previousCenter.x - photoFrame.width / 2
                        photoFrame.y = pinch.previousCenter.y - photoFrame.height / 2
                        photoFrame.z = zRestore
                        --mywin.highestZ
                    }
                }                    
                MouseArea {
                    id: dragArea
                    hoverEnabled: true
                    anchors.fill: parent
                    drag.target: photoFrame
                    scrollGestureEnabled: false  // 2-finger-flick gesture should pass through to the Flickable
                    onPressed: {
                        photoFrame.z = ++mywin.highestZ;
                    }                        
                    onWheel: {
                        if (wheel.modifiers & Qt.ControlModifier) {
                            photoFrame.rotation += wheel.angleDelta.y / 120 * 5;
                            if (Math.abs(photoFrame.rotation) < 4)
                                photoFrame.rotation = 0;
                        } else {
                            photoFrame.rotation += wheel.angleDelta.x / 120;
                            if (Math.abs(photoFrame.rotation) < 0.6)
                                photoFrame.rotation = 0;
                            var scaleBefore = photoFrame.scale;
                            photoFrame.scale += photoFrame.scale * wheel.angleDelta.y / 120 / 10;
                        }
                    }
                }
            }                
            Point {
                id: pointA
                x: image.width/4
                y: image.height/4
                color: "blue"
            }
            Point {
                id: pointB
                x: image.width/2
                y: image.height/2
                color: "blue"                    
            }                
            Point {
                id: pointD
                x: image.width/4
                y: image.height/2
                color: "red"
            }                
            Point {
                id: pointC
                x: image.width/2
                y: image.height/4
                color: "red"                    
            }                
            Item {
                anchors.fill: parent                    
                Canvas {
                    id: canvas
                    anchors.fill: parent                        
                    onPaint: {
                        //...
                    }
                    Component.onCompleted: {
                        //...
                    }                        
                    function repaint() {
                        //..
                    } 
                }
            }
        }
    }
}}

最佳答案

您正在使用继承FocusScopeScrollView,它需要将focus:true设置为forward it 。 相反,您将 focus 设置为 PhotoPreview,这是不应该聚焦的普通 Item

因此您需要简单地删除 focus:visible 并将其设置为 ScrollView

更好的解决方法是删除多余的 Item,并将 ScrollView 保留为 PhotoPreview 根项及其所有属性和信号。

关于qt - QML-使用箭头键移动项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37085707/

相关文章:

qt - 获取客户电话号码

c++ - 有没有一种方法可以在不创建该类实例的情况下获取 QObject 派生类的类名?

QTextEdit 或 QTextBrowser 性能问题

qt - QML 文件对话框(选择文件夹)

c++ - 使用 QML 构建基本的 Qt 界面

c++ - QAudioDeviceInfo 在 Ubuntu 上找不到默认音频设备

c++ - OSX Yosemite 上的 Valgrind callgrind

c++ - Valgrind 显示简单 QML 应用程序的许多警告/错误

button - qml 按钮未触发,与 MouseArea 相同

c++ - 无法使用 CMake 导入 QML 模块