0

我有一个QQuickWidget内部主窗口。这似乎有效,但是,拖动时它会剧烈摇晃。

//main.qml
Rectangle{
      id: root
      property point dragStart
      signal moved(point offset)

      MouseArea{
          id: dragArea
          anchors.fill: parent

          onPressed: root.dragStart = Qt.point(dragArea.mouseX,dragArea.mouseY)
          onMouseXChanged: move()
          onMouseYChanged: move()
          function move(){
                var offset = Qt.point(dragArea.mouseX-root.dragStart.x, dragArea.mouseY-root.dragStart.y)
                root.moved(offset)
                console.log(offset)
          }
      }
}
//MainWindow.cpp
void MainWindow::moveQml(QPointF offset){
    ui->quickWidget->move(ui->quickWidget->pos()+offset.toPoint());
}

这是我只拖到左下角时的调试输出,该点不应该有正 x:

qml: QPointF(6, 2)
qml: QPointF(6, 2)
qml: QPointF(-6, -1)
qml: QPointF(-6, -1)
qml: QPointF(5, 1)
qml: QPointF(5, 1)
qml: QPointF(-5, 0)
qml: QPointF(-5, 0)
qml: QPointF(4, 0)
qml: QPointF(4, 0)
qml: QPointF(-5, 1)
qml: QPointF(- 5、1)

4

1 回答 1

0

我懂了。检查输出后,我知道出了什么问题。移动鼠标时,鼠标通常会改变它的xy。所以move()将被执行两次。

onMouseXChanged: {
    var offset = Qt.point(dragArea.mouseX - root.dragStart.x, 0)
    root.moved(offset)
}

onMouseYChanged: {
    var offset = Qt.point(0, dragArea.mouseY - root.dragStart.y)
    root.moved(offset)
}
于 2015-08-23T15:23:11.300 回答