View Full Version : Mouse Over Event on QPushButtons

6th August 2007, 12:11
Hi all:)

I am working on Qt4.1 on my Intel MAC , I m having 5 buttons on my MainWindow and each button is represented by different Icon(image), What I wanted do is when mouse pointer is over the button it is replaced by the text,

For this I Subclass QPushButton & Use enter and leave events functions ,Its also working but when mouse pointer entered the buttons it replaces the icon of everybutton with the same text , and on leaving it replaces the text of the button with the same icons

But For each button I want the text & icon should be differnt.

How can i do this

How can i differentiate all the buttons.


6th August 2007, 12:38
Sounds to me like you're hardcoding the text and icon in the subclass. But these should be member variables of the object.

Maybe you should post your code if this doesn't help you.

6th August 2007, 14:23
The simplest solution I can think of is this

PushButton::PushButton( const QString & text, const QColor & color,
QWidget * parent, const char* name )
: QPushButton(parent, name)
_color = color;
mCustomToolTip = 0;

QColor& PushButton::getColor()
return _color;

Then in CustomToolTip you can have this

void CustomToolTip::paintEvent ( QPaintEvent * )
QPainter p(this);
p.fillRect( rect(), Qt::transparent );

if( parentWidget() )
p.setBrush( ((PushButton*)parentWidget())->getColor() );
p.drawText(rect(), Qt::AlignCenter, parentWidget()->name());
Hence there is no need for enterEvent and leaveEvent eventhandlers filters