PDA

View Full Version : QEvent::Enter



incapacitant
21st March 2006, 18:40
I try with no success to activate a slot when the mouse enters a widget's boundary.
Once activated I would like to change the cursor shape (hope i find that by myself!!)

This, below, does not do anything. I thought when hovering over the widget the window would close as a proof the event occured.



connect( pbDestin, SIGNAL( QEvent::Enter ), this, SLOT( close() ) );


But it does not close the window...

jacek
21st March 2006, 19:04
Events are not signals. You must reimplement QWidget::enterEvent() or use event filter.

jpn
21st March 2006, 19:04
There is a difference between events and signals/slots. You should study these concepts a bit:
Signals and Slots (http://doc.trolltech.com/4.1/signalsandslots.html)
Events and Event Filters (http://doc.trolltech.com/qtopia4.1/html/eventsandfilters.html)

You can connect signals to slots, but for catching events you need to subclass or use an event filter.

incapacitant
21st March 2006, 20:14
Following your good advice, I set up an event filter that works. I was just wondering whether there is a better way than what I implemented.

For each Widget that I want to react to QEvent::Enter or QEvent::Leave I added :


anyWidget->installEventFilter( this );


Then as filter :


bool ApplicationWindow::eventFilter(QObject *target, QEvent *event)
{
if (event->type() == QEvent::Enter)
QApplication::setOverrideCursor( QCursor(Qt::PointingHandCursor) );
if (event->type() == QEvent::Leave )
QApplication::restoreOverrideCursor();

return QMainWindow::eventFilter(target, event);
}



Is this the right way to do it ?

zlatko
22nd March 2006, 06:27
Its your choice..but you also can use next way



///// *.h

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

//// *.cpp

void ApplicationWindow::enterEvent(QEvent*ev)
{
QApplication::setOverrideCursor( QCursor(Qt::PointingHandCursor) );
}
void ApplicationWindow::leaveEvent(QEvent*ev)
{
QApplication::restoreOverrideCursor();
}

incapacitant
22nd March 2006, 07:54
hi,

I tried this other method you propose but it does not work for me. The following happens, the events are triggered as soon as the cursor enters the application window and not for the buttons only. Maybe this is because of QT4.

jpn
22nd March 2006, 08:07
the events are triggered as soon as the cursor enters the application window and not for the buttons only.
By this approach you would need to subclass the buttons to get their enter and leave events.
If you subclass the main window, you'll get the enter and leave events for the main window.
By using an event filter you are able to catch events going to buttons without subclassing them.