在 Qt5 的 QQuickItem 上捕捉 mouseMoveEvent
catch mouseMoveEvent on Qt5's QQuickItem
我正在关注 的回复。但是我卡住了 U_U.
我正在开发一个 Qt5 应用程序,当鼠标悬停在 QQuickItem 项目上时,我想在其中捕获 mouseMoveEvent 事件。但我不知道如何让它工作。
我已经隔离了有问题的代码(使用 代码):
main.cpp:
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtQuick>
class MyItem : public QQuickItem
{
public:
MyItem()
{
setAcceptHoverEvents(true);
setAcceptedMouseButtons(Qt::AllButtons);
setFlag(ItemAcceptsInputMethod, true);
}
void mousePressEvent(QMouseEvent* event)
{
QQuickItem::mousePressEvent(event);
qDebug() << event->pos();
}
void mouseMoveEvent(QMouseEvent* event)
{
QQuickItem::mouseMoveEvent(event);
qDebug() << event->pos();
}
};
int main(int argc, char** argv)
{
QGuiApplication app(argc, argv);
QQuickView* view = new QQuickView;
qmlRegisterType<MyItem>("Test", 1, 0, "MyItem");
view->setSource(QUrl(QStringLiteral("qrc:/main.qml")));
view->show();
return app.exec();
}
main.qml:
import QtQuick 2.4
import QtQuick.Controls 1.3
import Test 1.0
Rectangle {
width: 400
height: 400
visible: true
MyItem {
anchors.fill: parent
}
Button {
x: 100
y: 100
text: "Button"
}
}
在此示例代码中捕获了 mousePressEvent,但没有捕获 mouseMoveEvent,为什么?我认为解决方案一定很简单,但我看不出我的错误。
感谢 Jeremy Friesner 的评论,我得到了工作代码:
main.cpp:
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtQuick>
class MyItem : public QQuickItem
{
public:
MyItem()
{
setAcceptHoverEvents(true);
setAcceptedMouseButtons(Qt::AllButtons);
setFlag(ItemAcceptsInputMethod, true);
}
void mousePressEvent(QMouseEvent* event)
{
QQuickItem::mousePressEvent(event);
qDebug() << event->pos();
}
//This is function to override:
void hoverMoveEvent(QHoverEvent* event) {
QQuickItem::hoverMoveEvent(event);
qDebug() << event->pos();
}
};
int main(int argc, char** argv)
{
QGuiApplication app(argc, argv);
QQuickView* view = new QQuickView;
qmlRegisterType<MyItem>("Test", 1, 0, "MyItem");
view->setSource(QUrl(QStringLiteral("qrc:/main.qml")));
view->show();
return app.exec();
}
main.qml:
import QtQuick 2.4
import QtQuick.Controls 1.3
import Test 1.0
Rectangle {
width: 400
height: 400
visible: true
MyItem {
anchors.fill: parent
}
Button {
x: 100
y: 100
text: "Button"
}
}
我正在关注
我正在开发一个 Qt5 应用程序,当鼠标悬停在 QQuickItem 项目上时,我想在其中捕获 mouseMoveEvent 事件。但我不知道如何让它工作。
我已经隔离了有问题的代码(使用
main.cpp:
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtQuick>
class MyItem : public QQuickItem
{
public:
MyItem()
{
setAcceptHoverEvents(true);
setAcceptedMouseButtons(Qt::AllButtons);
setFlag(ItemAcceptsInputMethod, true);
}
void mousePressEvent(QMouseEvent* event)
{
QQuickItem::mousePressEvent(event);
qDebug() << event->pos();
}
void mouseMoveEvent(QMouseEvent* event)
{
QQuickItem::mouseMoveEvent(event);
qDebug() << event->pos();
}
};
int main(int argc, char** argv)
{
QGuiApplication app(argc, argv);
QQuickView* view = new QQuickView;
qmlRegisterType<MyItem>("Test", 1, 0, "MyItem");
view->setSource(QUrl(QStringLiteral("qrc:/main.qml")));
view->show();
return app.exec();
}
main.qml:
import QtQuick 2.4
import QtQuick.Controls 1.3
import Test 1.0
Rectangle {
width: 400
height: 400
visible: true
MyItem {
anchors.fill: parent
}
Button {
x: 100
y: 100
text: "Button"
}
}
在此示例代码中捕获了 mousePressEvent,但没有捕获 mouseMoveEvent,为什么?我认为解决方案一定很简单,但我看不出我的错误。
感谢 Jeremy Friesner 的评论,我得到了工作代码:
main.cpp:
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtQuick>
class MyItem : public QQuickItem
{
public:
MyItem()
{
setAcceptHoverEvents(true);
setAcceptedMouseButtons(Qt::AllButtons);
setFlag(ItemAcceptsInputMethod, true);
}
void mousePressEvent(QMouseEvent* event)
{
QQuickItem::mousePressEvent(event);
qDebug() << event->pos();
}
//This is function to override:
void hoverMoveEvent(QHoverEvent* event) {
QQuickItem::hoverMoveEvent(event);
qDebug() << event->pos();
}
};
int main(int argc, char** argv)
{
QGuiApplication app(argc, argv);
QQuickView* view = new QQuickView;
qmlRegisterType<MyItem>("Test", 1, 0, "MyItem");
view->setSource(QUrl(QStringLiteral("qrc:/main.qml")));
view->show();
return app.exec();
}
main.qml:
import QtQuick 2.4
import QtQuick.Controls 1.3
import Test 1.0
Rectangle {
width: 400
height: 400
visible: true
MyItem {
anchors.fill: parent
}
Button {
x: 100
y: 100
text: "Button"
}
}