简体   繁体   English

如何在MouseArea拖动中平移QML项目

[英]How to pan a QML item on MouseArea drag

I have a Qt app which runs on iOS and OSX using Qt 5.10 commercial version. 我有一个Qt应用程序 ,它使用Qt 5.10商业版本在iOSOSX上运行。 I have a QML item which hosts an image. 我有一个托管图像的QML项目 I am trying to pan the QML item when user's finger drags on it OR mouse is dragged. 我正在尝试在用户的手指上拖动或拖动鼠标时平移QML项目

Following is somewhat I am trying to make my QML item pannable: 以下是我正在尝试使我的QML项目可窗格化的一些信息:

Code: 码:

MyQmlItem {
    id: my_qml_item
    anchors.top: parent.top
    anchors.horizontalCenter: parent.horizontalCenter

    onXChanged: {
        if (my_qml_item_mouse_area.drag.active) {
            console.log("x = " + x)
            my_qml_item.x = // what to set x here to move my_qml_item wrt finger or mouse pressed movement
        }
    }
    onYChanged: {
        if (my_qml_item_mouse_area.drag.active) {
            console.log("y = " + y)
            my_qml_item.y = // what to set y here to move my_qml_item wrt finger or mouse pressed movement
        }
    }

    MouseArea {
        id: my_qml_item_mouse_area
        anchors.fill: parent

        drag {
            id: drag_area
            target: my_qml_item
            axis: Drag.XandYAxis
        }

    }
}

I understand that I have to update the x and y position of MyQmlItem when onXChanged and onYChanged is active and x y are getting updated. 我知道当onXChangedonYChanged处于活动状态并且x y正在更新时,我必须更新MyQmlItemxy位置。 But I am struggling to figure how I should re-calculate the new my_qml_item.x and my_qml_item.y 但是我正在努力弄清楚如何重新计算新的my_qml_item.xmy_qml_item.y

Question: 题:
I am getting x and y updates on onXChanged and onYChanged as well. 我也在onXChangedonYChanged上获得xy更新。 The basic question is, how to calculate plus continuously update my_qml_item.x and my_qml_item.y . 基本问题是,如何计算并不断更新my_qml_item.xmy_qml_item.y

Are there any good examples of Qt/QML for panning or dragging a QML item? 是否有用于平移或拖动QML项目的Qt / QML的良好示例

Is there some way to replicate the following anchors by only setting default x and y ? 是否可以通过仅设置默认的xy来复制以下锚点? Because, it falls in direct conflict with dragging the QML component 因为,它与拖动QML组件直接冲突

anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter

Anchors should not be used if you wish to drag as it links some part of the geometry of the items. 如果要拖动锚,则不应使用锚,因为它链接了项目几何的某些部分。

In your case, you only need to establish the specific positions at certain times, such as when the application starts, so instead of setting the anchors you could use the properties "x", "y", "width" and "height" . 在您的情况下,您仅需要在特定时间确定特定位置,例如在应用程序启动时,因此可以使用属性“ x”,“ y”,“ width”和“ height”来设置锚点,而无需设置锚点。

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2

ApplicationWindow {
    id: app
    visible: true
    visibility: "FullScreen"
    title: qsTr("Scroll")

    function resetPosition(){
        item.x = Screen.orientation === Qt.PortraitOrientation ? (Screen.width - item.width)/2 : (Screen.height - item.height)/2
        item.y = 0
    }

    Image {
        id: item
        source: "http://doc.qt.io/qt-5/images/declarative-qtlogo.png"
        onStatusChanged:  {
            if(status == Image.Ready)
                resetPosition()
        }
        MouseArea{
            anchors.fill: parent
            drag.target: item
            drag.axis: Drag.XAndYAxis
            onClicked: resetPosition()
        }

    }

    property bool isPortrait: Screen.primaryOrientation === Qt.PortraitOrientation || Screen.primaryOrientation === Qt.InvertedPortraitOrientation
    property bool isLandscape: Screen.primaryOrientation === Qt.LandscapeOrientation || Screen.primaryOrientation === Qt.InvertedLandscapeOrientation

    onIsPortraitChanged: resetPosition()
    onIsLandscapeChanged: resetPosition()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM