I'm following the reply of my previous question . But I got stuck U_U.
I'm developing a Qt5 application where I want to catch mouseMoveEvent event when the mouse hovers a QQuickItem item. But I don't know how to make it work.
I have isolated the problematic code (using this code):
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"
}
}
In this example code mousePressEvent is catched, but mouseMoveEvent isn't, Why? I think the solution must be trivial but I cannot see my mistake.
Thanks to Jeremy Friesner's comment I get the working code:
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"
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.