无法正确跟踪鼠标移动,setMouseTracking 无效 - Qt
Cannot track mouse moves correctly, setMouseTracking has no effect - Qt
我正在做一个由别人开始的项目,所以我不能改变代码的结构。
我的 class 的代码如下(这只是一个示例):
class myClass : public QWidget
{
Q_OBJECT
public:
explicit myClass();
~myClass();
bool eventFilter(QObject *watched, QEvent *e);
private:
QMainWindow* window;
FRAMEWORK_InfoWidget *zone_notif;
};
因此,我的 class 包含一个 QmainWindow "window" 和一个自定义小部件 "zone_notif"(基本上是一个矩形)。我想当鼠标光标经过"zone_notif"时显示一条消息,出来时显示另一条消息。
我首先尝试了以下方法:
myClass::myClass():QWidget()
{
window = new QMainWindow();
window->setFixedSize(SCREEN_REZOLUTION);
window->setWindowTitle(QString("window"));
zone_notif = new FRAMEWORK_InfoWidget(restit_window);
zone_notif->setGeometry(299, 452, 320, 55);
window->installEventFilter(this);
this->setMouseTracking(true);
}
myClass::~myClass()
{
delete zone_notif;
delete window;
}
bool myClass::eventFilter(QObject *watched, QEvent *e)
{
if(e->type() == QEvent::MouseMove)
{
int x = cursor().pos().x() - restit_window->geometry().x();
int y = cursor().pos().y() - restit_window->geometry().y();
qDebug() << "Moving !";
if((x > zone_notif->pos().x()) && (x < zone_notif->pos().x() + zone_notif->width()) && (y > zone_notif->pos().y()) && (y < zone_notif->pos().y() + zone_notif->height()))
{
qDebug() << "OVER !";
}
else
{
qDebug() << "IN !";
}
return true;
}
return QWidget::eventFilter(watched, e);
}
但只有在移动前按下鼠标按钮才有效。
我找到了一个肮脏的解决方案:
bool myClass::eventFilter(QObject *watched, QEvent *e)
{
if(e->type() == QEvent::Enter)
{
qDebug() << "ENTREE";
window->grabMouse();
return true;
}
else if(e->type() == QEvent::Leave)
{
qDebug() << "Sortie";
window->releaseMouse();
return true;
}
else if(e->type() == QEvent::MouseMove)
{
int x = cursor().pos().x() - restit_window->geometry().x();
int y = cursor().pos().y() - restit_window->geometry().y();
qDebug() << "Moving !";
if((x > zone_notif->pos().x()) && (x < zone_notif->pos().x() + zone_notif->width()) && (y > zone_notif->pos().y()) && (y < zone_notif->pos().y() + zone_notif->height()))
{
qDebug() << "OVER !";
}
else
{
qDebug() << "IN !";
}
return true;
}
return QWidget::eventFilter(watched, e);
}
但是这个解决方案是不正确的。我的应用程序包含其他 windows,如果我将 QMainWindow 设置为全屏,我永远不会退出它,所以我的其他 windows 不再可点击。
我尝试将 window->setMouseTracking(true)
更改为 zone_notif->setMouseTracking(true)
,与 ìnstallEventFilter 相同,但没有效果。
我也尝试覆盖 mouseMoveEvent :
void myClass::mouseMoveEvent(QMouseEvent *e)
{
qDebug() << "I am here !";
QWidget::mouseMoveEvent(e);
}
但它永远不会被调用。
你知道我如何让它发挥作用吗?
提前致谢
覆盖 QWidget::mouseMoveEvent(QMouseEvent * 事件)
您应该在鼠标跟踪打开时收到事件。
我找到了解决方案。
为了确保捕捉到鼠标移动事件,我在我的 QApplication 上安装了一个事件过滤器。
我创建了一个新的 class "myEventFilter",这个 class 作为事件过滤器安装。
myEventFilter.h
#ifndef MYEVENTFILTER_H
#define MYEVENTFILTER_H
#include <QObject>
#include <QMouseEvent>
class myEventFilter: public QObject
{
Q_OBJECT
public:
myEventFilter();
~myEventFilter();
protected:
bool eventFilter(QObject* object,QEvent* event);
};
#endif // MYEVENTFILTER_H
myEventFilter.cpp
#include "myEventFilter.h"
#include <QDebug>
#include <QCursor>
myEventFilter::myEventFilter():QObject()
{}
myEventFilter::~myEventFilter()
{}
bool myEventFilter::eventFilter(QObject* object, QEvent* event)
{
if(event->type() == QEvent::MouseMove)
{
int x = QCursor::pos().x();
int y = QCursor::pos().y();
qDebug() << "I am here -> (" + QString::number(x) + "," + QString::number(y) + ")";
return true;
}
else
return object->eventFilter(object, event);
}
main.cpp
#include <QtGui/QApplication>
#include "myEventFilter.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow sw = new QMainWindow();
sw->show();
a.installEventFilter(new myEventFilter());
return a.exec();
}
而且有效!
我正在做一个由别人开始的项目,所以我不能改变代码的结构。 我的 class 的代码如下(这只是一个示例):
class myClass : public QWidget
{
Q_OBJECT
public:
explicit myClass();
~myClass();
bool eventFilter(QObject *watched, QEvent *e);
private:
QMainWindow* window;
FRAMEWORK_InfoWidget *zone_notif;
};
因此,我的 class 包含一个 QmainWindow "window" 和一个自定义小部件 "zone_notif"(基本上是一个矩形)。我想当鼠标光标经过"zone_notif"时显示一条消息,出来时显示另一条消息。 我首先尝试了以下方法:
myClass::myClass():QWidget()
{
window = new QMainWindow();
window->setFixedSize(SCREEN_REZOLUTION);
window->setWindowTitle(QString("window"));
zone_notif = new FRAMEWORK_InfoWidget(restit_window);
zone_notif->setGeometry(299, 452, 320, 55);
window->installEventFilter(this);
this->setMouseTracking(true);
}
myClass::~myClass()
{
delete zone_notif;
delete window;
}
bool myClass::eventFilter(QObject *watched, QEvent *e)
{
if(e->type() == QEvent::MouseMove)
{
int x = cursor().pos().x() - restit_window->geometry().x();
int y = cursor().pos().y() - restit_window->geometry().y();
qDebug() << "Moving !";
if((x > zone_notif->pos().x()) && (x < zone_notif->pos().x() + zone_notif->width()) && (y > zone_notif->pos().y()) && (y < zone_notif->pos().y() + zone_notif->height()))
{
qDebug() << "OVER !";
}
else
{
qDebug() << "IN !";
}
return true;
}
return QWidget::eventFilter(watched, e);
}
但只有在移动前按下鼠标按钮才有效。
我找到了一个肮脏的解决方案:
bool myClass::eventFilter(QObject *watched, QEvent *e)
{
if(e->type() == QEvent::Enter)
{
qDebug() << "ENTREE";
window->grabMouse();
return true;
}
else if(e->type() == QEvent::Leave)
{
qDebug() << "Sortie";
window->releaseMouse();
return true;
}
else if(e->type() == QEvent::MouseMove)
{
int x = cursor().pos().x() - restit_window->geometry().x();
int y = cursor().pos().y() - restit_window->geometry().y();
qDebug() << "Moving !";
if((x > zone_notif->pos().x()) && (x < zone_notif->pos().x() + zone_notif->width()) && (y > zone_notif->pos().y()) && (y < zone_notif->pos().y() + zone_notif->height()))
{
qDebug() << "OVER !";
}
else
{
qDebug() << "IN !";
}
return true;
}
return QWidget::eventFilter(watched, e);
}
但是这个解决方案是不正确的。我的应用程序包含其他 windows,如果我将 QMainWindow 设置为全屏,我永远不会退出它,所以我的其他 windows 不再可点击。
我尝试将 window->setMouseTracking(true)
更改为 zone_notif->setMouseTracking(true)
,与 ìnstallEventFilter 相同,但没有效果。
我也尝试覆盖 mouseMoveEvent :
void myClass::mouseMoveEvent(QMouseEvent *e)
{
qDebug() << "I am here !";
QWidget::mouseMoveEvent(e);
}
但它永远不会被调用。
你知道我如何让它发挥作用吗?
提前致谢
覆盖 QWidget::mouseMoveEvent(QMouseEvent * 事件)
您应该在鼠标跟踪打开时收到事件。
我找到了解决方案。
为了确保捕捉到鼠标移动事件,我在我的 QApplication 上安装了一个事件过滤器。
我创建了一个新的 class "myEventFilter",这个 class 作为事件过滤器安装。
myEventFilter.h
#ifndef MYEVENTFILTER_H
#define MYEVENTFILTER_H
#include <QObject>
#include <QMouseEvent>
class myEventFilter: public QObject
{
Q_OBJECT
public:
myEventFilter();
~myEventFilter();
protected:
bool eventFilter(QObject* object,QEvent* event);
};
#endif // MYEVENTFILTER_H
myEventFilter.cpp
#include "myEventFilter.h"
#include <QDebug>
#include <QCursor>
myEventFilter::myEventFilter():QObject()
{}
myEventFilter::~myEventFilter()
{}
bool myEventFilter::eventFilter(QObject* object, QEvent* event)
{
if(event->type() == QEvent::MouseMove)
{
int x = QCursor::pos().x();
int y = QCursor::pos().y();
qDebug() << "I am here -> (" + QString::number(x) + "," + QString::number(y) + ")";
return true;
}
else
return object->eventFilter(object, event);
}
main.cpp
#include <QtGui/QApplication>
#include "myEventFilter.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow sw = new QMainWindow();
sw->show();
a.installEventFilter(new myEventFilter());
return a.exec();
}
而且有效!