c ++ – How to change the appearance of a cell in a QTableView by moving the mouse over it?

I'm trying to create a table in which the cells of one of its columns can only be changed through a dialog box that will open when it's up to date. a button is pressed on it. Moreover, I would like this button to be visible only when the mouse passes on this cell.

While reading this, it seems that the idea of ‚Äč‚Äčinserting a custom widget is possible but penalizes performance (according to the comments in SO in English) and is not the right way. This appears in the documentation:

Items displayed in a tabular view, like those in other item views
are rendered and edited with the help of standard delegates. However, for some
tasks, it is sometimes useful to insert widgets into a table
in place. Widgets are defined for particular indexes with the
setIndexWidget (), and later retrieved with indexWidget ().

The most "natural" way is to go through the delegates.
I have this piece of code (fully copied from here) that proposes to use the method

void paint (painter QPainter *, const QStyleOptionViewItem & option, const QModelIndex & index) const;

Draw the button

and

bool editorEvent (event QEvent *, model QAbstractItemModel *, const QStyleOptionViewItem & option, const QModelIndex & index)

To answer the click of the mouse.
In this way (my implementation):

void DelegateFormulasMedicion :: paint (painter QPainter *, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
if (index.isValid ())
{
QStyleOptionButton button;
QRect r = option.rect; // get the rect of the cell
int x, y, w, h;
w = m_ancho_boton;
h = r.height (); // height of the button
x = r.left () + r.width () - w; // the X coordinate
y = r.top (); // the Y coordinate
boton.rect = QRect (x, y, w, h);
boton.text = "...";
button.state = QStyle :: State_MouseOver;
QApplication :: style () -> drawControl (QStyle :: CE_PushButton, & button, painter);
}
d & # 39; other
{
DelegateBase :: paint (painter, option, index);
}
}

and

bool DelegateFormulasMedicion :: editorEvent (event QEvent *, model QAbstractItemModel *, const QStyleOptionViewItem & option, const QModelIndex & index)
{
if (event-> type () == QEvent :: MouseButtonRelease)
{
QMouseEvent * e = event (QMouseEvent *);
int clickX = e-> x ();
int clickY = e-> y ();

QRect r = option.rect; // get the rect of the cell
int x, y, w, h;
x = r.left () + r.width () - m_ancho_boton; // the X coordinate
y = r.top (); // the Y coordinate
w = m_ancho_boton; // width of the button
h = r.height (); // height of the button

if (clickX> x && clickX < x + w )
            if( clickY > y && clickY <y + h)
{
qDebug () << "I open a dialogue" // this is my dialogue
}
return true;
}
returns false;
}

Until now, there seems to be no problem. Now I have this button to draw if the mouse is on the cell, for which I understand that I have to capture the event.

For that I followed these steps.

1.- Create a signal in the table:

signals:
void hoverIndexChanged (bool inside);

2.- Activate this in the constructor of the table:

setMouseTracking (true);
setAttribute (Qt :: WA_Hover);

3.- Capture the position of the mouse

void TableMed :: mouseMoveEvent (QMouseEvent * event)
{
QPoint pos = event-> pos ();
QModelIndex index = indexAt (pos);
if (index.isValid ())
{
if (index.column () == tipoColumnaTMedCert :: FORMULA)
{
// qDebug () << "I'm inside";
emit hoverIndexChanged (true);
}
d & # 39; other
{
// qDebug () << "I'm out";
issue hoverIndexChanged (false);
}
}
TableBase :: mouseMoveEvent (event);
}

4.- Connect the signal and the slot:

connect (this, SIGNAL (hoverIndexChanged (bool)), dlgFM, SLOT (onHoverIndexChanged (bool)));

In this way, I could change a Boolean member of the delegate so that with the relevant changes made to the paint () method above, said button is displayed or not, but I can not capture that signal in the delegate .