I have strange problem with mouse events in Qt. I have following code:
#ifndef QSTONEFIELD_HPP_
#define QSTONEFIELD_HPP_
#include <QtGui>
#include <QWidget>
class QStoneField : public QWidget
{
Q_OBJECT
private:
// some stuff
public:
// some methods
protected:
void paintEvent(QPaintEvent *event);
virtual void mousePressEvent(QMouseEvent * event);
virtual void enterEvent(QMouseEvent * event);
virtual void leaveEvent(QMouseEvent * event);
signals:
public slots:
};
#endif
And in second file I have:
#include "qstonefield.hpp"
// FIXME temporary include
#include <iostream>
using namespace std;
// some other methods ..
void QStoneField::mousePressEvent(QMouseEvent * event)
{
cout << "CLICK!" << endl << flush;
}
void QStoneField::enterEvent(QMouseEvent * event)
{
cout << "ENTER!" << endl << flush;
}
void QStoneField::leaveEvent(QMouseEvent * event)
{
cout << "LEAVE!" << endl << flush;
}
And now when I compile and run it, I can invoke mousePressEvent because program prints "CLICK!", but when I am crossing the widget by mouse, it prints just nothing. Of course in main.cpp I didnt forget to stone.setMouseTracking(true).
Why enterEvent and leaveEvent doesnt work? It should work according to documentation. Thanks in advance!
The signature of your function should be:
virtual void QStoneField::enterEvent(QEvent * event);
virtual void QStoneField::leaveEvent(QEvent * event);
You are simply using the wrong parameter for the function which means the one from Qwidget are not overwritten.
I recommend you to consider what the default implementation does, for example :
virtual void QStoneField::leaveEvent(QEvent * event){
//do my own things
QWidget::leaveEvent(event);
}