QML:通过拖动移动无框窗口
我有一个无框QQuickWindow,我想用鼠标拖动它。在我的大应用程序尝试之前,我已经创建了一个简单的测试应用程序来尝试我在这里找到的东西,使用C++类的光标位置来避免QML问题: 但是我用下面的代码失败了。当我按下红色矩形并移动鼠标时,我的黄色矩形(根矩形)会移动,但只在原来的大小内移动(在本例中为500x500)。。。我做错了什么 提前谢谢 在my main.cpp中:QML:通过拖动移动无框窗口,qml,drag,mousemove,Qml,Drag,Mousemove,我有一个无框QQuickWindow,我想用鼠标拖动它。在我的大应用程序尝试之前,我已经创建了一个简单的测试应用程序来尝试我在这里找到的东西,使用C++类的光标位置来避免QML问题: 但是我用下面的代码失败了。当我按下红色矩形并移动鼠标时,我的黄色矩形(根矩形)会移动,但只在原来的大小内移动(在本例中为500x500)。。。我做错了什么 提前谢谢 在my main.cpp中: int main(int argc, char *argv[]) { QtQuickControlsAp
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
\ifndef cursorprovider\u H
#定义游标提供程序
#包括
#包括
#包括
类游标提供程序:公共QObject
{
Q_对象
公众:
显式游标提供程序(QObject*parent=nullptr):QObject(parent)
{
}
virtual~cursorpoprovider()=默认值;
Q_可调用的QPointF cursorPos()命令
{
返回QCursor::pos();
}
};
#endif//CURSORPOSPROVIDER\u H
我编写了这个示例,没有看到抖动(在Linux上运行)
我已经改变了结构,我使用了一个QQuickWidget,里面有一个QML,现在我得到了我想要的。这是我的代码,以防任何人需要类似的东西 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;
}
#包括“movableWidget.h”
#包括
// ****************************************************************************
MovableWidget::MovableWidget(QWidget*父项)
:QQuickWidget(父项),
m_previousPos(0,0)
{
installEventFilter(此);
}
// ****************************************************************************
bool MovableWidget::eventFilter(QObject*obj,QEvent*event)
{
如果(事件->类型()==QEvent::MouseButtonPress)
{
m_previousPos=QCursor:os();
}
else if(事件->类型()==QEvent::MouseMove)
{
QMouseEvent*mouseeEvent=静态_转换(事件);
if(mouseEvent->buttons()==Qt::LeftButton)
{
QPoint offset=m_previousPos-QCursor:os();
m_previousPos=QCursor:os();
移动(位置()-偏移);
}
}
返回false;
}
非常感谢,但它让我感到不安……:(
#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;
}