PDA

View Full Version : correct event for lose focus in a QGLWidget window



john_god
15th February 2009, 21:08
I have a aplication with a main window. Pressing a button from the toolbar opens a QGLWidget window that displays a timer animation. I want to kill the timer when the window stops from being active, for example when the user clicks the main window or another program, or even if he minimazes the window. So whats the event I should use ?

I use focusOutEvent, but the animation keeps on runing. Also can I kill the event twice, or this can cause a error ?

My code:


void GLGraph3D::focusOutEvent(QFocusEvent * event)
{

// event->WindowDeactivate
if (event->ActivationChange)

if (TimerRotate != 0)
{
killTimer(TimerRotate);
TimerRotate = 0;
}
}

talk2amulya
15th February 2009, 21:16
u can install an event filter for the following events..i think it should work


QEvent::WindowActivate, QEvent::WindowDeactivate, QEvent::WindowStateChange

john_god
15th February 2009, 21:22
Im not sure how to implement a event filter, I will study that , but mean while could you be more specific, or show me a example ?
Thank you

talk2amulya
15th February 2009, 21:26
Installs an event filter filterObj on this object. For example:

monitoredObj->installEventFilter(filterObj);

An event filter is an object that receives all events that are sent to this object. The filter can either stop the event or forward it to this object. The event filter filterObj receives events via its eventFilter() function. The eventFilter() function must return true if the event should be filtered, (i.e. stopped); otherwise it must return false.

If multiple event filters are installed on a single object, the filter that was installed last is activated first.

Here's a KeyPressEater class that eats the key presses of its monitored objects:

class KeyPressEater : public QObject
{
Q_OBJECT
...

protected:
bool eventFilter(QObject *obj, QEvent *event);
};

bool KeyPressEater::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
qDebug("Ate key press %d", keyEvent->key());
return true;
} else {
// standard event processing
return QObject::eventFilter(obj, event);
}
}

And here's how to install it on two widgets:

KeyPressEater *keyPressEater = new KeyPressEater(this);
QPushButton *pushButton = new QPushButton(this);
QListView *listView = new QListView(this);

pushButton->installEventFilter(keyPressEater);
listView->installEventFilter(keyPressEater);

john_god
16th February 2009, 01:34
Thnks for your example talk2amulya, I get the picture.
Also I've been reading Qt4 book and the solution is easier than the event filter, I implemente the event function and compare the event type to WindowDeactivate event, like this:


bool GLGraph3D::event(QEvent *event)
{

if (event->type() == QEvent::WindowDeactivate)
{
if (TimerRotate != 0)
{
killTimer(TimerRotate);
TimerRotate = 0;
}
}

return QWidget::event(event);
}

It Works !!!!!!!!!!!!!!!!!!!! ;)