Results 1 to 5 of 5

Thread: correct event for lose focus in a QGLWidget window

  1. #1
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default correct event for lose focus in a QGLWidget window

    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:

    Qt Code:
    1. void GLGraph3D::focusOutEvent(QFocusEvent * event)
    2. {
    3.  
    4. // event->WindowDeactivate
    5. if (event->ActivationChange)
    6.  
    7. if (TimerRotate != 0)
    8. {
    9. killTimer(TimerRotate);
    10. TimerRotate = 0;
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: correct event for lose focus in a QGLWidget window

    u can install an event filter for the following events..i think it should work

    Qt Code:
    1. QEvent::WindowActivate, QEvent::WindowDeactivate, QEvent::WindowStateChange
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: correct event for lose focus in a QGLWidget window

    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

  4. #4
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: correct event for lose focus in a QGLWidget window

    Qt Code:
    1. Installs an event filter filterObj on this object. For example:
    2.  
    3. monitoredObj->installEventFilter(filterObj);
    4.  
    5. 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.
    6.  
    7. If multiple event filters are installed on a single object, the filter that was installed last is activated first.
    8.  
    9. Here's a KeyPressEater class that eats the key presses of its monitored objects:
    10.  
    11. class KeyPressEater : public QObject
    12. {
    13. Q_OBJECT
    14. ...
    15.  
    16. protected:
    17. bool eventFilter(QObject *obj, QEvent *event);
    18. };
    19.  
    20. bool KeyPressEater::eventFilter(QObject *obj, QEvent *event)
    21. {
    22. if (event->type() == QEvent::KeyPress) {
    23. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    24. qDebug("Ate key press %d", keyEvent->key());
    25. return true;
    26. } else {
    27. // standard event processing
    28. return QObject::eventFilter(obj, event);
    29. }
    30. }
    31.  
    32. And here's how to install it on two widgets:
    33.  
    34. KeyPressEater *keyPressEater = new KeyPressEater(this);
    35. QPushButton *pushButton = new QPushButton(this);
    36. QListView *listView = new QListView(this);
    37.  
    38. pushButton->installEventFilter(keyPressEater);
    39. listView->installEventFilter(keyPressEater);
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to talk2amulya for this useful post:

    john_god (16th February 2009)

  6. #5
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: correct event for lose focus in a QGLWidget window

    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:

    Qt Code:
    1. bool GLGraph3D::event(QEvent *event)
    2. {
    3.  
    4. if (event->type() == QEvent::WindowDeactivate)
    5. {
    6. if (TimerRotate != 0)
    7. {
    8. killTimer(TimerRotate);
    9. TimerRotate = 0;
    10. }
    11. }
    12.  
    13. return QWidget::event(event);
    14. }
    To copy to clipboard, switch view to plain text mode 

    It Works !!!!!!!!!!!!!!!!!!!!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.