PDA

View Full Version : Advise sought for implementation of hoverEvent in QWidget



TorAn
1st June 2010, 00:04
I have QWidget-derived class where I am painting custom view. I’d like the user to be able to move the mouse pointer to a certain position within the view boundaries. When the mouse pointer reaches this point and user stops I need to display the dialog.

I don’t think I can use QAction because my view is not a toolbar or a menu. So, I turned to events and this is my current implementation. The problem is that these events are generated all the time. I can’t figure out a way to find the time to actually show my dialog – events are too “small” to detect specific time when the mouse is actually stopped moving.

Your advise will be greatly appreciated.


myView::myView (QWidget *parent): QWidget(parent)
{
this->setAttribute(Qt::WA_Hover);
}

bool myView::event(QEvent *event)
{
if (event->type() == QEvent::HoverMove)
{
QHoverEvent * ke = static_cast<QHoverEvent*>(event);
if (ke->oldPos() == QPoint(-1,-1))
return QWidget::event(event);
QPoint delta = ke->oldPos() - ke->pos();
if ( (std::abs(delta.x()) < 2) && (std::abs(delta.y()) < 2) )
{
//show dialog here
return true;
}

}
return QWidget::event(event);
}

SixDegrees
1st June 2010, 00:21
You'll need to decide what you mean by "stopped moving" - how long does the mouse have to remain motionless to qualify? And is just being within the hot zone sufficient, or must the mouse be completely motionless within that area?

Anyway, one approach would be to use a QTimer set to your timeout interval, and reset the timer with each mouse move. If the timer ever times out, it'll emit a signal which you can connect to your popup dialog.

Personally, I think it would be better - and simpler - to just pop up the dialog when the mouse is within the hot zone, and ignore timing and whether the mouse has "stopped." But this is a rough guide if you decide to proceed.

TorAn
1st June 2010, 00:33
Thank you, SixDegrees. I was considering the timer, but thought that Qt might offer something already built-in which I overlooked. After all, QAction seems to have this functionality already. If there are no other suggestions from the readers of this thread - QTimer will be my choice :)

aamer4yu
1st June 2010, 06:33
Your need seems to be something like showing a dialog instead of tooltip. Hope I get it right.
So you can hack the tooltip implementation and show your dialog.

You will need to check for the QEvent::ToolTip

TorAn
1st June 2010, 12:42
Your need seems to be something like showing a dialog instead of tooltip. Hope I get it right.
So you can hack the tooltip implementation and show your dialog.

You will need to check for the QEvent::ToolTip

You description is correct and your suggestion is exactly what I needed. Added it and got the desired effect. Thank you.

if (event->type() == QEvent::ToolTip)
...