我对 Qt 中的鼠标事件有奇怪的问题。我有以下代码:
#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
在第二个文件中我有:
#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;
}
现在,当我编译并运行它时,我可以调用 mousePressEvent,因为程序会打印“CLICK!”,但是当我用鼠标穿过小部件时,它什么也没打印。当然在 main.cpp 中我没有忘记 Stone.setMouseTracking(true)。
为什么 enterEvent 和 leaveEvent 不起作用?它应该根据文档工作。提前致谢!