Results 1 to 11 of 11

Thread: How to write correctly an event handler

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to write correctly an event handler

    Hi to all,
    I would know how to correctly write an event handler for example a keyPressEvent.

    I wrote this event that I would catch when I press the space key:

    Qt Code:
    1. void WaveWidget::keyPressEvent( QKeyEvent* pe )
    2. {
    3. switch( pe->key() )
    4. {
    5. case Qt::Key_Space:
    6. {
    7. pe->accept();
    8. qDebug() << "Space pressed";
    9. m_wave->playSound();
    10. }
    11. break;
    12.  
    13. default:
    14. pe->ignore();
    15. break;
    16. }
    17. QWidget::keyPressEvent(pe);
    18. }
    To copy to clipboard, switch view to plain text mode 

    This is the correct way to write it? Must I accept the event with the right key and ignore in other case? And must I pass the event to the base class?

    I don't know why in my case in doesn't work.

    Best
    Franco Amato

  2. #2
    Join Date
    Sep 2009
    Location
    Tashkent, Uzbekistan
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to write correctly an event handler

    void QWidget::keyPressEvent ( QKeyEvent * event ) [virtual protected]

    This event handler, for event event, can be reimplemented in a subclass to receive key press events for the widget.

    A widget must call setFocusPolicy() to accept focus initially and have focus in order to receive a key press event.

    If you reimplement this handler, it is very important that you call the base class implementation if you do not act upon the key.

    The default implementation closes popup widgets if the user presses Esc. Otherwise the event is ignored, so that the widget's parent can interpret it.

    Note that QKeyEvent starts with isAccepted() == true, so you do not need to call QKeyEvent::accept() - just do not call the base class implementation if you act upon the key.

  3. #3
    Join Date
    Sep 2009
    Location
    Tashkent, Uzbekistan
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to write correctly an event handler

    And this was taken from an example tetrix. You should have it somewhere.

    Qt Code:
    1. void TetrixBoard::keyPressEvent(QKeyEvent *event)
    2. {
    3. if (!isStarted || isPaused || curPiece.shape() == NoShape) {
    4. QFrame::keyPressEvent(event);
    5. return;
    6. }
    7.  
    8. switch (event->key()) {
    9. case Qt::Key_Left:
    10. tryMove(curPiece, curX - 1, curY);
    11. break;
    12. case Qt::Key_Right:
    13. tryMove(curPiece, curX + 1, curY);
    14. break;
    15. case Qt::Key_Down:
    16. tryMove(curPiece.rotatedRight(), curX, curY);
    17. break;
    18. case Qt::Key_Up:
    19. tryMove(curPiece.rotatedLeft(), curX, curY);
    20. break;
    21. case Qt::Key_Space:
    22. dropDown();
    23. break;
    24. case Qt::Key_D:
    25. oneLineDown();
    26. break;
    27. default:
    28. QFrame::keyPressEvent(event);
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to write correctly an event handler

    Quote Originally Posted by Tanuki-no Torigava View Post
    And this was taken from an example tetrix. You should have it somewhere.

    Qt Code:
    1. void TetrixBoard::keyPressEvent(QKeyEvent *event)
    2. {
    3. if (!isStarted || isPaused || curPiece.shape() == NoShape) {
    4. QFrame::keyPressEvent(event);
    5. return;
    6. }
    7.  
    8. switch (event->key()) {
    9. case Qt::Key_Left:
    10. tryMove(curPiece, curX - 1, curY);
    11. break;
    12. case Qt::Key_Right:
    13. tryMove(curPiece, curX + 1, curY);
    14. break;
    15. case Qt::Key_Down:
    16. tryMove(curPiece.rotatedRight(), curX, curY);
    17. break;
    18. case Qt::Key_Up:
    19. tryMove(curPiece.rotatedLeft(), curX, curY);
    20. break;
    21. case Qt::Key_Space:
    22. dropDown();
    23. break;
    24. case Qt::Key_D:
    25. oneLineDown();
    26. break;
    27. default:
    28. QFrame::keyPressEvent(event);
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 
    Hi sorry but I didn't understand.
    My class inherits from QWidget. Where my code is wrong? And why it don't catch
    key event?

    Best
    Franco Amato

  5. #5
    Join Date
    Sep 2009
    Location
    Tashkent, Uzbekistan
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to write correctly an event handler

    Ok. Looks like logic error

    void WaveWidget::keyPressEvent( QKeyEvent* pe )
    {
    switch( pe->key() )
    {
    case Qt::Key_Space:
    {
    pe->accept();
    qDebug() << "Space pressed";
    m_wave->playSound();
    }
    break;

    default:
    pe->ignore(); // Do you ignore everything here?
    break;
    }
    QWidget::keyPressEvent(pe); // so why do you call the handler for all events you receive?
    }
    Just trave the sequence of event.

  6. #6
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to write correctly an event handler

    Quote Originally Posted by Tanuki-no Torigava View Post
    Ok. Looks like logic error



    Just trave the sequence of event.
    So when I have to call the ignore method?
    Franco Amato

  7. #7
    Join Date
    Sep 2009
    Location
    Tashkent, Uzbekistan
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to write correctly an event handler

    I wonder which keys you need to process? If all - just remove the last call to parent handler instance, if not - don't ignore.

  8. #8
    Join Date
    Sep 2009
    Location
    Tashkent, Uzbekistan
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to write correctly an event handler

    And please look at this once again:

    A widget must call setFocusPolicy() to accept focus initially and have focus in order to receive a key press event.
    Did you call setFocusPolicy?

  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to write correctly an event handler

    Quote Originally Posted by Tanuki-no Torigava View Post
    And please look at this once again:



    Did you call setFocusPolicy?

    Guys read the docs carefully! First ignore() and accept() are not necessary! and setFocusPolicy is also not required. The user can activate the widget himself using the mouse or tab...

    So a simple implementation would be:
    Qt Code:
    1. // eats space
    2. void WaveWidget::keyPressEvent( QKeyEvent* pe )
    3. {
    4. switch( pe->key() )
    5. {
    6. case Qt::Key_Space:
    7. {
    8. qDebug() << "Space pressed";
    9. m_wave->playSound();
    10. }
    11. break;
    12.  
    13. default:
    14. QWidget::keyPressEvent(pe);
    15. break;
    16. }
    17. }
    18.  
    19. // just plays a sound whenever space is pressed
    20. void WaveWidget::keyPressEvent( QKeyEvent* pe )
    21. {
    22. switch( pe->key() )
    23. {
    24. case Qt::Key_Space:
    25. {
    26. qDebug() << "Space pressed";
    27. m_wave->playSound();
    28. }
    29. break;
    30.  
    31. default:
    32. break;
    33. }
    34. QWidget::keyPressEvent(pe);
    35. }
    To copy to clipboard, switch view to plain text mode 

    assuming QWidget is your base class. And such problems should be better posted in the Newbie section.

  10. #10
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to write correctly an event handler

    Quote Originally Posted by Lykurg View Post
    Guys read the docs carefully! First ignore() and accept() are not necessary! and setFocusPolicy is also not required. The user can activate the widget himself using the mouse or tab...

    So a simple implementation would be:
    Qt Code:
    1. // eats space
    2. void WaveWidget::keyPressEvent( QKeyEvent* pe )
    3. {
    4. switch( pe->key() )
    5. {
    6. case Qt::Key_Space:
    7. {
    8. qDebug() << "Space pressed";
    9. m_wave->playSound();
    10. }
    11. break;
    12.  
    13. default:
    14. QWidget::keyPressEvent(pe);
    15. break;
    16. }
    17. }
    18.  
    19. // just plays a sound whenever space is pressed
    20. void WaveWidget::keyPressEvent( QKeyEvent* pe )
    21. {
    22. switch( pe->key() )
    23. {
    24. case Qt::Key_Space:
    25. {
    26. qDebug() << "Space pressed";
    27. m_wave->playSound();
    28. }
    29. break;
    30.  
    31. default:
    32. break;
    33. }
    34. QWidget::keyPressEvent(pe);
    35. }
    To copy to clipboard, switch view to plain text mode 

    assuming QWidget is your base class. And such problems should be better posted in the Newbie section.
    Sorry but in the second case I always pass the event to the base class and in the first one only in the default case...which is the difference?
    Franco Amato

  11. #11
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to write correctly an event handler

    Quote Originally Posted by franco.amato View Post
    Sorry but in the second case I always pass the event to the base class and in the first one only in the default case...which is the difference?
    If you have a QWidget as base class: No difference. But e.g. your base class is a QLineEdit, then in the first case the user can not write a space in the line edit. Which one you use depends on your requirements I don't know.

Similar Threads

  1. Key Event handler
    By jano_alex_es in forum Newbie
    Replies: 2
    Last Post: 5th December 2009, 10:32
  2. Deleting objects in their event handler
    By drhex in forum Qt Programming
    Replies: 7
    Last Post: 6th May 2009, 16:08
  3. Replies: 4
    Last Post: 19th February 2009, 11:10
  4. Changing default event handler name
    By mabeeh in forum Newbie
    Replies: 1
    Last Post: 21st April 2008, 15:18
  5. event handler
    By mattia in forum Newbie
    Replies: 10
    Last Post: 8th November 2007, 12:54

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.