2016-11-21 51 views
1

フレームなしのQQuickWindowがあり、マウスでドラッグして移動したいと思います。大きなアプリケーションを試す前に、C++クラスのカーソル位置を使用してQMLの問題を回避する簡単なテストアプリケーションを作成しました。 http://www.tickanswer.com/solved/5390888353/dragging-frameless-window-jiggles-in-qmlQML:フレームレスウィンドウをドラッグして移動する

しかし、以下のコードでは失敗しました。赤いRECTを押してマウスを動かすと、黄色い矩形(根のRECT)が動いていますが、元のサイズ(この場合は500x500)の中だけ...何が間違っていますか?私のmain.cppには、事前

ありがとう:

int main(int argc, char *argv[]) 
{ 
    QtQuickControlsApplication a(argc, argv); 

    QQuickView* pView = new QQuickView(); 

    CursorPosProvider mousePosProvider; 
    pView->rootContext()->setContextProperty("mousePosition", &mousePosProvider); 
    pView->setSource(QUrl("qrc:/Test.qml")); 
    pView->setFlags(Qt::FramelessWindowHint); 
    pView->show(); 

    return a.exec(); 
} 

Test.qml:

import QtQuick 2.0 

Rectangle { 
    id: myWindow 
    width: 500; height: 500 
    color: "yellow" 

    Rectangle { 
     anchors.centerIn: parent 
     width: 200; height: 200 

     color: "red" 

     MouseArea { 
      id: titleBarMouseRegion 
      property var clickPos 
      anchors.fill: parent 

      onPressed: clickPos = { x: mousePosition.cursorPos().x, y: mousePosition.cursorPos().y } 

      onPositionChanged: { 
       myWindow.x = mousePosition.cursorPos().x - clickPos.x 
       myWindow.y = mousePosition.cursorPos().y - clickPos.y 
      } 
     } 
    } 
} 

cursorprovider.h:

#ifndef CURSORPOSPROVIDER_H 
#define CURSORPOSPROVIDER_H 

#include <QObject> 
#include <QPointF> 
#include <QCursor> 

class CursorPosProvider : public QObject 
{ 
    Q_OBJECT 
public: 
    explicit CursorPosProvider(QObject *parent = nullptr) : QObject(parent) 
    { 
    } 
    virtual ~CursorPosProvider() = default; 

    Q_INVOKABLE QPointF cursorPos() 
    { 
     return QCursor::pos(); 
    } 
}; 

#endif // CURSORPOSPROVIDER_H 

答えて

1

私はこの例を書きました私は揺れは見ません(Linuで走っていますx)

ApplicationWindow { 
    id: iWindow 
    visible: true 
    title: "My title" 
    color: "gray" 
    width: 500 
    height: 500 

    MouseArea{ 
     id: iMouseArea 
     property int prevX: 0 
     property int prevY: 0 
     anchors.fill: parent 
     onPressed: {prevX=mouse.x; prevY=mouse.y} 
     onPositionChanged:{ 
      var deltaX = mouse.x - prevX; 
      iWindow.x += deltaX; 
      prevX = mouse.x - deltaX; 

      var deltaY = mouse.y - prevY 
      iWindow.y += deltaY; 
      prevY = mouse.y - deltaY; 
     } 
    } 

} 
+0

おかげで多くのことを必要とするが、それはジグル私のために... :( – Diego

0

私は構造を変更しました。私はQML内部でQQuickWidgetを使用しましたが、今は私が望むものを持っています。ここではケース誰で私のコードは、似たような

main.cppに

... 
MovableWidget *view = new MovableWidget; 
view->setSource(QUrl("qrc:/Test.qml")); 
view->setWindowFlags(Qt::FramelessWindowHint); 
view->show(); 
... 

Test.qml

import QtQuick 2.0 

Rectangle { 
    id: myWindow 
    width: 500; height: 500 
    color: "yellow" 

    Rectangle { 
     anchors.centerIn: parent 
     width: 200; height: 200 

     color: "red" 
    } 
} 

MovableWidget.cpp

#include "movableWidget.h" 

#include <QMouseEvent> 

// **************************************************************************** 
MovableWidget::MovableWidget(QWidget *parent) 
    : QQuickWidget(parent), 
    m_previousPos(0,0) 
{ 
    installEventFilter(this); 
} 

// **************************************************************************** 
bool MovableWidget::eventFilter(QObject *obj, QEvent *event) 
{ 
    if (event->type() == QEvent::MouseButtonPress) 
    { 
     m_previousPos = QCursor:os(); 
    } 
    else if (event->type() == QEvent::MouseMove) 
    { 
     QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event); 

     if(mouseEvent->buttons() == Qt::LeftButton) 
     { 
      QPoint offset = m_previousPos - QCursor:os(); 
      m_previousPos = QCursor:os(); 
      move(pos() - offset); 
     } 
    } 

    return false; 
} 
関連する問題