Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
新的拖放机制在Qt Quick(Qt 5.3)中无法按预期工作_Qt_Drag And Drop_Qml_Qt5_Qt Quick - Fatal编程技术网

新的拖放机制在Qt Quick(Qt 5.3)中无法按预期工作

新的拖放机制在Qt Quick(Qt 5.3)中无法按预期工作,qt,drag-and-drop,qml,qt5,qt-quick,Qt,Drag And Drop,Qml,Qt5,Qt Quick,我尝试在Qt5.3中使用新的QML类型drag、DragEvent和DropArea实现拖放。这是中的原始示例,有一些小的修改: import QtQuick 2.2 Item { width: 800; height: 600 DropArea { width: 100; height: 100; anchors.centerIn: parent Rectangle { anchors.fill: parent

我尝试在Qt5.3中使用新的QML类型
drag
DragEvent
DropArea
实现拖放。这是中的原始示例,有一些小的修改:

import QtQuick 2.2

Item {
    width: 800; height: 600

    DropArea {
        width: 100; height: 100; anchors.centerIn: parent

        Rectangle {
            anchors.fill: parent
            color: parent.containsDrag ? "red" : "green"
        }

        onEntered: print("entered");
        onExited: print("exited");
        onDropped: print("dropped");
    }

    Rectangle {
        x: 15; y: 15; width: 30; height: 30; color: "blue"

        Drag.active: dragArea.drag.active
        // Drag.dragType: Drag.Automatic
        Drag.onDragStarted: print("drag started");
        Drag.onDragFinished: print("drag finished");

        MouseArea {
            id: dragArea
            anchors.fill: parent
            drag.target: parent
        }
    }
}
预期行为:可以用鼠标拖动蓝色小矩形(拖动目标)。如果拖动到窗口中心较大的绿色矩形上,该矩形将在离开时变为红色并返回绿色。此外,dragStarted、entered、Exit、dragFinished和dragFinished信号会及时发出,相应的信号处理程序会打印出它们的消息

有经验的行为

取决于拖动.dragType(参见上面的注释行):

  • 未设置拖动.dragType(默认为拖动.Internal):

    拖放操作如前所述,但仅发出输入和退出的信号。其他信号(dragStarted、dragFinished和dragFinished)被抑制。因此,无法对
    DropArea
    中的下降做出反应

  • Drag.dragType
    设置为
    Drag.Automatic

    所有信号现在都会发出,但蓝色矩形(拖动目标)不会随鼠标移动。相反,鼠标光标会更改其形状以可视化可能的放置目标。释放鼠标后,蓝色矩形跳到最新的鼠标位置

  • 这两种变体都不令人满意。我怎样才能获得所有信号,并且仍然能够在拖动目标周围拖动?不幸的是,文档中对QML中的拖放几乎没有任何明确的说明,特别是关于不祥的
    拖放。dragType

    我自己也遇到了这个问题(使用qt5.2,但同样的问题也存在)。我在X轴上有一个“滑块框”,我只是想知道拖动什么时候完成。。。而不是一路上对每个位置的变化都做出反应。我的解决方法是使用
    ScriptAction
    对状态/转换进行黑客攻击,以提供逻辑。这是模拟“onDragFinished”信号响应的简化版本。因此,虽然它不能覆盖所有的拖放信号,但它可能会让你指向正确的方向

    Rectangle {
        id: sliderControl
    
        height: coordinates.height
        width: 80
        color: "#F78181"
        border.color: "#FE2E2E"
        border.width: 1
        opacity: 0.4
    
        MouseArea {
            id: mouseArea
            anchors.fill: parent
            drag.target: sliderControl
            drag.axis: Drag.XAxis
            drag.minimumX: 0
            drag.maximumX: view.width - sliderControl.width
            hoverEnabled: true
        }
    
        states: [
            State {
                name: "dragging"
                when: mouseArea.drag.active
            },
            State {
                name: "finished_dragging"
                when: !mouseArea.drag.active
            }
        ]
    
        transitions: [
            Transition {
                from: "dragging"
                to: "finished_dragging"
                ScriptAction {
                    script: console.log("finished dragging script");
                }
            }
        ]
    }
    
    ps-我知道这样的“解决方案”不符合赏金参数,但当我搜索有关该问题的帮助时,我非常沮丧地发现只有你的问题(没有解决方案)。希望其他在这条道路上跌跌撞撞的人会发现这一点很有用。不幸的是,我也不知道QML的Drag.dragType是怎么回事。

    如果打开并查看Drag.Internal使用的
    start()
    ,和Drag.Automatic使用的
    startDrag()
    之间的差异,差异是非常明显的,然后,它将使用它来创建附加对象的

    为什么会这样?我不知道!QtQuick 2拖放文档在这里当然有改进的余地

    有一个相当简单的解决办法:从两个世界中取其精华。使用
    Drag.Automatic
    ,但不是设置
    Drag.active
    ,而是手动调用
    start()
    drop()
    。它不会调用
    Drag.onDragStarted()
    Drag.ondragpointed()
    ,但实际上,通过监听
    MouseArea
    Drag.active
    中的更改,您可以免费获得这些文件

    以下是行动中的概念:

    import QtQuick 2.0
    
    Item {
        width: 800; height: 600
    
        DropArea {
            width: 100; height: 100; anchors.centerIn: parent
    
            Rectangle {
                anchors.fill: parent
                color: parent.containsDrag ? "red" : "green"
            }
    
            onEntered: print("entered");
            onExited: print("exited");
            onDropped: print("dropped");
        }
    
        Rectangle {
            x: 15; y: 15; width: 30; height: 30; color: "blue"
    
            // I've added this property for simplicity's sake.
            property bool dragActive: dragArea.drag.active
    
            // This can be used to get event info for drag starts and 
            // stops instead of onDragStarted/onDragFinished, since
            // those will neer be called if we don't use Drag.active
            onDragActiveChanged: {
                if (dragActive) {
                    print("drag started")
                    Drag.start();
                } else {
                    print("drag finished")
                    Drag.drop();
                }
            }
    
            Drag.dragType: Drag.Automatic
    
            // These are now handled above.
            //Drag.onDragStarted: print("drag started");
            //Drag.onDragFinished: print("drag finished");
    
            MouseArea {
                id: dragArea
                anchors.fill: parent
                drag.target: parent
            }
        }
    }
    
    我意识到这不是一个完全令人满意的解决方案,但它确实符合您的预期行为

    此解决方案提供:

    • 所有所需事件的通知:开始拖动、完成拖动、进入拖动区域、退出拖动区域以及拖放到拖动区域
    • 拖动动画由QtQuick自动处理。正方形不会像使用拖动.Automatic运行示例代码时那样冻结在原地
    它没有提供什么:

    • 解释为什么QtQuick的拖放功能以这种方式工作,或者这是否是开发人员的预期行为。目前的文件似乎模棱两可

    谢谢您的回答。我很高兴我不是一个人在那里我也尝试过一些类似的解决方案,但不幸的是,在我的“真实”代码中,拖动启动的
    MouseArea
    完全独立于
    DropArea
    ,后者应该处理拖放。因此,如果没有黑客攻击,从
    MouseArea
    DropArea
    的丢弃事件通信相当复杂/几乎不可能。我开始认为QtQuick中的DnD机制不知何故被破坏了……感谢您的详细回答。尽管我仍然对不同的拖动类型感到困惑,但您的解决方案完全满足了我的需求,因为它模拟了所需的事件,因此可以解耦拖动源和拖放区域。尽管如此,我认为Qt团队应该增强QtQuick DnD,因为它不是真正有用和直观的。