PDA

View Full Version : Mouse over event



merry
15th May 2007, 07:50
Hi all

I am working on Qt4.2 on my Intel MAC PC.
My Problem is that :
-> using MainWindow having somebuttons on it .
-> what i want is this when i move mouse over any button that is when mouse pointer is over the button it displays the details of that button in the label.

marcel
15th May 2007, 08:18
If you have a layout set for your main window( and you should ), you can use layout()->itemAt() and if the mouse position is inside the items geometry.
You should do this for all the QPushButton items, in a loop.
Anyway, even if it works, this is a wrong solution.

The way to go is to reimplement mouseEnterEvent and mouseLeaveEvent in your buttons and emit signals when these events occur.

Regards

viking
16th May 2007, 05:03
You could also see underMouse(). mouseTracking should be true, obviously. But yes, as Marcel said, reimplementing mouseEnterEvent and mouseLeaveEvent in your buttons would be neat!

marcel
16th May 2007, 05:18
underMouse() would involve additional polling in the main window :).

Actually the events I was talking about are called enterEvent and leaveEvent ( my bad :) ).

Regards

merry
16th May 2007, 05:35
Thanx Marcel

I tried enter event and leave event It works for the main window,Can you explain me it for the QPushButtons with example :)

marcel
16th May 2007, 07:22
class MyButton : public QPushButton
{
Q_OBJECT
public:
MyButton( QWidget* = NULL );
~MyButton();

protected:
virtual void enterEvent( QEvent* );
virtual void leaveEvent( QEvent* );

public signals:
void enter( QString );
void leave( QString );

}

void MyButton::enterEvent( QEvent* e )
{
emit enter( text() );
}

void MyButton::leaveEvent( QEvent* e )
{
emit leave( text() );
}


This is a minimal example of what you can do.
When a button is hovered, it emits the enter signal, with the button name as parameter. You can use the name to display it in your label.

Also, when the mouse leaves the button, the leave signal is emitted.

Regards

wysota
16th May 2007, 09:11
You can achieve a simmilar effect without any hacks. Main windows have a status bar and you can make it that it displays the text you want when a widget is hovered. It's enough to fill the statusTip property for the buttons or their actions. An alternative of course is to use a tooltip.

merry
16th May 2007, 10:21
Nothing Works , Please tell me Is there any other way to do this.

wysota
16th May 2007, 10:23
Maybe you'd like to tell us some more details about this "nothing"?

merry
16th May 2007, 10:41
Yaa , Actuallly I implemented that what Marcel had told me


class MyButton : public QPushButton
{
Q_OBJECT
public:
MyButton( QWidget* = NULL );
~MyButton();

protected:
virtual void enterEvent( QEvent* );
virtual void leaveEvent( QEvent* );

public signals:
void enter( QString );
void leave( QString );

}

void MyButton::enterEvent( QEvent* e )
{
emit enter( text() );
}

void MyButton::leaveEvent( QEvent* e )
{
emit leave( text() );
}


and also what u told
, that Main windows have a status bar and you can make it that it
displays the text you want when a widget is hovered.
But it wont works ,because what i want is this ,I am having 6 Push buttons on my main window,and each works differently ,When mouse cursor is over any button it displays the detail of that button in the label that i used.

marcel
16th May 2007, 10:54
Have you used MyButton instead of QPushButton?
Also have you used the two signals properly?

It is impossible not to
work, therefore you have done something wrong.

wysota
16th May 2007, 10:58
I got this to work without any subclassing, just using a status tip.

merry
16th May 2007, 11:13
Thanx Wysota it works and also thanx to Marcel for helping me so much