1

我想对其进行子类化QLineEdit以添加一些信号,mouseEnter()mouseExit()在鼠标光标位于小部件上方以及何时离开小部件时获得通知。我可以mouseEnter()像下面这样写。

class MyLineEdit:public QLineEdit{
//
// ..
//
protected: 
    void mouseMoveEvent(QMouseEvent *e);

signals:
    void mouseEnter();
}

void MyLineEdit::mouseMoveEvent(QMouseEvent *e)
{
    emit mouseEnter();
    QLineEdit::mouseMoveEvent(e);
}

它工作正常。我该怎么写mouseExit()

4

1 回答 1

2

leaveEvent 不是一个很好的名字!

#include <QtGui>
#include <QEvent>

class Editor : public QLineEdit
{
    Q_OBJECT

public:
    void leaveEvent(QEvent *);

signals:
    void mouseLeave();
};

void Editor::leaveEvent(QEvent *e);
{
    qDebug() << "Mouse has left the building..";
    emit mouseLeave();
}
于 2013-03-16T11:10:23.213 回答