Recently, I wanted that QPushButton
can emit a signal, when the mouse pointer enters. How can I make it?
I know that QPushButton has some already defined signal, such as clicked()
, pressed()
, destory()
and so on. But no signal like hover(), enter(), ...
I looked some information about it: Someone said it can be done by css. I don't understand. Can you give me some advice ? Thank you!
You can use QWidget::enterEvent ( QEvent * event ) for this.
You override this event and send a custom defined signal when ever this event occurs.
First you have to enable mouse tracking for this widget (setMouseTracking(true)
in the constructor for example).
Header file:
class my_button
{
// ...
protected:
virtual void enterEvent( QEvent* e );
public Q_SIGNALS:
void hovered();
// ...
};
Source 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()) );