Problem when I try to use components of a form created with Qt Designer in C++ with hover elements like enterEvent or leaveEvent.
-
Hi,
My problem is when I try to use components of a form created with Qt Designer in C++ with hover elements like enterEvent or leaveEvent.
The form automatically created in Qt Designer is called "MainWindow".
MainWindow has been instanciated for a multiple Inherance mode, so I do not use the "ui->".
This form contains some QWidgets like QPushButtons and QLabels.
One QPushButton is called "bouton".I can get or modify properties of bouton, for example :
@bouton->setStyleSheet{"background-color: yellow"};@But what I want is to change the background-color of bouton when bouton is hovered by the mouse.
(bouton is a .png image with transparency).I should write something in my mainwindow.cpp like:
@void bouton::enterEvent(QEvent *event)
{
if(event->type() == QEvent::Enter) {
bouton->setStyleSheet("background-color: rgb(240, 230, 140)");
}
}void bouton::leaveEvent(QEvent *event)
{
if(event->type() == QEvent::Leave) {
bouton->setStyleSheet("background-color: transparent");
}
}@But bouton is not an object qualified by a class. It does not work.
And I cannot create myself a class "bouton" because the compiler will consider a conflict with the mainwindow.uiThe syntax for capturing the mouse event seems to be correct because changing void bouton by void MainWindow works, but naturally for the whole window ...
So, I would appreciate any help !
Thanks in advance. -
Hi,
you can install an "event filter":http://qt-project.org/doc/qt-4.8/eventsandfilters.html#event-filters in your bouton object, but if all you want is a different background when the button is hovered then it can be easily done using the stylesheet "pseudo states":http://qt-project.org/doc/qt-4.8/stylesheet-syntax.html#pseudo-states
I hope it helps,
H. -
Hi Arnaut
Thanks.
Yes it helps : writing in MainWindow.cpp :@ bouton->setStyleSheet(":hover {background-color: rgb(240, 230, 140)}");@
Works exactly as I want !
I was working on another way of solution, much more complicated, consisting in creating a class "Boutons", adding to it new properties, and then "promoting":http://qt-project.org/doc/qt-4.8/designer-using-custom-widgets.html bouton to the class Boutons.
But your solution is more efficient for what I want to do.
F.