Results 1 to 3 of 3

Thread: keyPressEvent

  1. #1
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default keyPressEvent

    I would like to catch the Space key in my app.
    The header:

    Qt Code:
    1. #pragma once
    2. #include <QWidget>
    3. #include <iitem.hpp>
    4. #include <ui_invpg.h>
    5.  
    6.  
    7. class InvPage : public QWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public :
    12.  
    13. InvPage();
    14. virtual ~InvPage();
    15.  
    16. void clear();
    17. void ClearSel();
    18. void MakeList();
    19. void MakeInv();
    20. void LoadData();
    21.  
    22. IItem *theitem;
    23. bool addit;
    24.  
    25. protected :
    26.  
    27. virtual void keyPressEvent( QKeyEvent *event );
    28.  
    29. private :
    30.  
    31. Ui::InvPage ui;
    32.  
    33. private slots :
    34.  
    35. void ItemClicked( QListWidgetItem *item );
    36. };
    To copy to clipboard, switch view to plain text mode 

    The ctor contains only ui.setupUi(this). The (current) implementation should test whether the handler works:

    Qt Code:
    1. #include <QWidget>
    2. #include <QKeyEvent>
    3. #include <myapp.hpp>
    4. #include <myframe.hpp>
    5. #include <invpage.hpp>
    6. #include <iitem.hpp>
    7. #include <data.hpp>
    8.  
    9.  
    10. void InvPage::keyPressEvent( QKeyEvent *event )
    11. {
    12. if( event->key() == Qt::Key_Space )
    13. {
    14. char str[40];
    15.  
    16. sprintf(str,"%s",(addit ? "addit = true" : "addit = false"));
    17. TheApp->frame->statusBar()->showMessage(str,2000);
    18. }
    19.  
    20. QWidget::keyPressEvent(event);
    21. }
    To copy to clipboard, switch view to plain text mode 

    It does not work. Putting a breakpoint at the "if" shows that the handler isn't called at all. What kind of blunder am I making? (Note: the statusbar works fine.)

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,216
    Thanks
    300
    Thanked 862 Times in 849 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: keyPressEvent

    Where is this widget hosted? Does it have input focus? Is the parent widget 'eating' the keystrokes? Does some child widget have the input focus and is it eating the keystrokes?

    Add an QObject::eventFilter() to this class, and install it on each of the child widgets. (QObject::installEventFilter()) In the event filter, add a clause to detect KeyPress events. Set a breakpoint there. If it breaks, then you can examine the QObject passed in the filter to determine which child widget (if any) is handling those events.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: keyPressEvent

    That;s it, most likely.

    The widget contains 2 QListWidgets and one QPushButton. I wanted the list boxes to react on the space key but the event handler belongs to the "surroundings" (the widget itself), which do not have the focus when I pressed the space key. One of the list boxes has. Never mind, I added another push button which does what the space key should have done and it works now. I was too lazy to redirect the events

Similar Threads

  1. KeyPressEvent()
    By Knogger in forum Newbie
    Replies: 2
    Last Post: 6th November 2017, 17:02
  2. keypressevent
    By djwk in forum Newbie
    Replies: 9
    Last Post: 5th July 2010, 02:12
  3. prob with keyPressEvent()
    By Askar in forum Qt Programming
    Replies: 3
    Last Post: 14th December 2009, 07:47
  4. QMdiSubWindow keyPressEvent
    By M. in forum Newbie
    Replies: 0
    Last Post: 4th December 2009, 23:05
  5. How to block keyPressEvent ?
    By richardander in forum Qt Programming
    Replies: 1
    Last Post: 24th January 2009, 09:14

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.