Results 1 to 5 of 5

Thread: Detect Arrow Keys

  1. #1
    Join Date
    Jul 2009
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Detect Arrow Keys

    Hello, I am porting some Qt software from Linux to Windows. It went smoothly, except for two problems:

    1) I had to comment out this line of code, that made my QGraphicsView render with OpenGL:
    Qt Code:
    1. gui.graphicsView->setViewport(new QGLWidget);
    To copy to clipboard, switch view to plain text mode 

    If it was enabled, at runtime I would get warning: ASSERT: "dst.depth() == 32" in file qgl.cpp, line 1688

    2) My program no longer detects arrow keystrokes. I can see they're being consumed by the QGraphicsView, because they wrongly cause the user to scroll through the graphics view. In Linux, the problem was solved by adding
    Qt Code:
    1. gui.graphicsView->setFocusPolicy(Qt::NoFocus);
    To copy to clipboard, switch view to plain text mode 
    In Windows, it doesn't help. Could it be because I am no longer using OpenGL? All other keystrokes are still detected normally, like so:
    Qt Code:
    1. void MainWindow::keyPressEvent(QKeyEvent *keyEvent){...}
    To copy to clipboard, switch view to plain text mode 

    What should I do?

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Detect Arrow Keys

    Have you tried installing an event filter?

  3. #3
    Join Date
    Jul 2009
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Talking Re: Detect Arrow Keys

    I have now, with great success. ;) Thank you very much.

    I've seen a few other people have posted about this problem, so here is the solution I used:

    Qt Code:
    1. gui.graphicsView->installEventFilter(this);
    2.  
    3. void MainWindow::keyReleaseEvent(QKeyEvent *keyEvent)
    4. {
    5. ...
    6. }//keyReleaseEvent
    7.  
    8. void MainWindow::keyPressEvent(QKeyEvent *keyEvent)
    9. {
    10. ...
    11. }//keyPressEvent
    12.  
    13. bool MainWindow::eventFilter(QObject *obj,
    14. QEvent *event)
    15. {
    16. QKeyEvent *keyEvent = NULL;//event data, if this is a keystroke event
    17. bool result = false;//return true to consume the keystroke
    18.  
    19. if (event->type() == QEvent::KeyPress)
    20. {
    21. keyEvent = dynamic_cast<QKeyEvent*>(event);
    22. this->keyPressEvent(keyEvent);
    23. result = true;
    24. }//if type()
    25.  
    26. else if (event->type() == QEvent::KeyRelease)
    27. {
    28. keyEvent = dynamic_cast<QKeyEvent*>(event);
    29. this->keyReleaseEvent(keyEvent);
    30. result = true;
    31. }//else if type()
    32.  
    33. //### Standard event processing ###
    34. else
    35. result = QObject::eventFilter(obj, event);
    36.  
    37. return result;
    38. }//eventFilter
    To copy to clipboard, switch view to plain text mode 

    Now arrow keystrokes that were being eaten by the QGraphicsView are being passed to my event handlers, as they should be. If you are having problems detecting arrow keys, they are probably being eaten by another widget, and this solution should work.

  4. #4
    Join Date
    Jan 2006
    Location
    Maui, Hawaii
    Posts
    120
    Thanks
    65
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Detect Arrow Keys

    I tried the eventFilter solution above, when a QComboBox gets focus it never forwards it to the event handler.

  5. #5
    Join Date
    Jan 2006
    Location
    Maui, Hawaii
    Posts
    120
    Thanks
    65
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Detect Arrow Keys

    Ah ... the problem was I tried to install the eventFilter on a widget with a bunch of children who didn't get the installation.

    I brute forced it:

    Qt Code:
    1. for ( int i = 0; i < sub_control->sub_sub_control.children().size(); i++ ) {
    2. sub_control->sub_sub_control.children()[ i ]->installEventFilter( this );
    3. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Implementing Keys : 1, a, b, c in QWSServer::Keypad
    By ramsarvan in forum Qt Programming
    Replies: 1
    Last Post: 18th May 2011, 10:59
  2. Replies: 1
    Last Post: 6th July 2009, 13:48
  3. fn+f1 keypress detect
    By oguzy in forum Qt Programming
    Replies: 1
    Last Post: 16th November 2008, 16:21
  4. How can I associate arrow keys?
    By Mariane in forum Newbie
    Replies: 2
    Last Post: 20th January 2006, 18:31

Tags for this Thread

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.