1

I'm creating a project in Qt Creator.

I wanna add hovered event for a QPushButton which is created in design mode.

But when I'm clicking go to slot option (which shows available events) i can't see something like hovered(). that's what I can see

When I was searching stackoverflow for this problem I found this (source):

QPushButton#pushButton:hover {
background-color: rgb(224, 255, 0);
}

And my question:

How to implement this on Qt Creator (when the QPushButton is created in design mode)?

Thanks in advance, have a great day :)

1 Answers1

0

you should use event filter:

look at this example, I add one push button in mainwindow.ui and add eventFilter virtual function.

Don't forget that you should installEventFilter in your pushButton

in mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui
{
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow: public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);

    ~MainWindow();

    // QObject interface

public:
    bool  eventFilter(QObject *watched, QEvent *event);

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

and in mainwindow.cpp :

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QEvent>

MainWindow::MainWindow(QWidget *parent):
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->pushButton->installEventFilter(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

bool  MainWindow::eventFilter(QObject *watched, QEvent *event)
{
    if ((watched == ui->pushButton) && (event->type() == QEvent::HoverEnter))
    {
        qDebug() << "Button is hovered !";
    }

    return false;
}

the result is :

enter image description here

Parisa.H.R
  • 3,303
  • 3
  • 19
  • 38