[ACCEPTED]-How to emit a signal from a QPushButton when the mouse hovers over it?-mousehover

Accepted answer
Score: 13

Although @Exa has answered this question, I 4 want to show another solution which does 3 not need to subclass QPushButton and is 2 flexible in use! ( That's what I need in 1 my project)

Step 1/2 : Overriding eventFilter.

LoginWindow.h:

// LoginWindow is where you placed your QPushButton 
//(= most probably your application windows)

class LoginWindow: public QWidget
{
public:
      bool eventFilter(QObject *obj, QEvent *event);
..
};

LoginWindow.cpp:

bool LoginWindow::eventFilter(QObject *obj, QEvent *event)
{
    // This function repeatedly call for those QObjects
    // which have installed eventFilter (Step 2)

    if (obj == (QObject*)targetPushButton) {
        if (event->type() == QEvent::Enter)
        {
        // Whatever you want to do when mouse goes over targetPushButton
        }
        return true;
    }else {
        // pass the event on to the parent class
        return QWidget::eventFilter(obj, event);
    }
}

Step 2/2 : Installing eventFilter on target widgets.

LoginWindow::LoginWindow()
{
    ...
    targetPushButton->installEventFilter(this);
    ...
}
Score: 11

You can use QWidget::enterEvent ( QEvent * event ) for this.

You override this 5 event and send a custom defined signal when 4 ever this event occurs.

First you have to 3 enable mouse tracking for this widget (setMouseTracking(true) in 2 the constructor for example).

Header file:

class my_button
{
    // ...

protected:
    virtual void enterEvent( QEvent* e );

public Q_SIGNALS:
    void hovered();

    // ...
};

Source 1 file:

void my_button::enterEvent( QEvent* e )
{
    Q_EMIT hovered();

    // don't forget to forward the event
    QWidget::enterEvent( e );
}

Where you use your button:

connect( one_of_my_button, SIGNAL(hovered()), this, SLOT(do_something_when_button_hovered()) );
Score: 3

Make sure to add ':' after the public keyword

public: Q_SIGNALS:
    void hovered();

0

Score: 1

If I remember correctly, you need to enable 6 mouse tracking for the button (Qt documentation) and override 5 QWidget::onEnter() and QWidget::onLeave().

You will need to create a custom 4 button class inheriting from QPushButton. You 3 can define signals for mouseEnter and mouseLeave 2 in your custom class and emit them from 1 the onEnter() and onLeave() methods that you need to override.

Score: 1

So QT deal with mouse hovering using the 15 "event" enterEvent (https://doc.qt.io/qt-5/qevent.html look for 14 "QEvent::Enter"). This isn't about 13 the Signal/Slot functionality (https://doc.qt.io/qt-5/signalsandslots.html), this is 12 about Events (https://doc.qt.io/qt-5/eventsandfilters.html) We find enterEvent as a 11 protected method at QWidget class (https://doc.qt.io/qt-5/qwidget.html) which 10 is a base class for QPushButton class (https://doc.qt.io/qt-5/qpushbutton.html).

So 9 what you have to do: to create a new class 8 derived from QPushButton and override the 7 protected method "enterEvent" that 6 QPushButton inherited from QWidget.

Creating 5 the new class:
QT Creator - File - New File 4 or Project...
File and Classes - C++
C++ Class
Choose...
Base 3 class - Custom - QPushButton
Next
define 2 a name for your new class like MyPushButton

In 1 mypushbutton.h:

#ifndef MYPUSHBUTTON_H
#define MYPUSHBUTTON_H

#include <QPushButton>

class MyPushButton: public QPushButton
{
    Q_OBJECT
public:
    using QPushButton::QPushButton;  //inherits the QPushButton constructors

signals:
    void myPushButtonMouseHover();

protected:
    void enterEvent(QEvent *event);
};

#endif // MYPUSHBUTTON_H

In mypushbutton.cpp:

#include "mypushbutton.h"
#include <QMessageBox>

void MyPushButton::enterEvent(QEvent *event)
{
    QMessageBox::warning(this, "Mouse hover", "Mouse hovered MyPushButton"); //popping a message box

    emit myPushButtonMouseHover();  //emitting signal 

    QPushButton::QWidget::enterEvent(event);  //calling the "natural" enterEvent

}

More Related questions