Results 1 to 7 of 7

Thread: QDialog keypress event

  1. #1
    Join Date
    Jan 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default QDialog keypress event

    I am trying to capture the keypress event in a QDialog.

    In my constructor, I have this as the last line:
    QObject::installEventFilter(this);


    Then I have this method:
    Qt Code:
    1. bool frmFileDialog::eventFilter(QObject *obj, QEvent *event)
    2. {
    3. QKeyEvent *keyEvent;
    4.  
    5. if(event->type() == QEvent::KeyPress){
    6. keyEvent = static_cast<QKeyEvent *>(event);
    7. }
    8. else{
    9. return QObject::eventFilter(obj, event);
    10. }
    11. if(keyEvent->isAutoRepeat()){
    12. return QObject::eventFilter(obj, event);
    13. }
    14.  
    15. if(keyEvent->key() == Qt::Key_1){
    16. keyEvent->accept();
    17. this->close();
    18. return true;
    19. }
    20. else{
    21. return QObject::eventFilter(obj, event);
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    The issue I am having is if any of regular keys are typed on the keyboard (i.e. the 1 key), the method is not entered at all. If "F1" or the tab key are pressed, it is entered. What do I need to capture any key that is pressed in this Dialog?

    Thanks,
    Kevin
    Last edited by anda_skoa; 2nd February 2015 at 09:22. Reason: missing [code] tags

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QDialog keypress event

    What do I need to capture any key that is pressed in this Dialog?
    Simply reimplement QDialog::keyPressEvent.

  3. #3
    Join Date
    Jan 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QDialog keypress event

    Actually I figured out the problem, but I am not sure how to fix it. I have a QTableView on the form. It is catching keystrokes first. I disabled the QTableView, then my keyPressEvent works. Can I disable the QTableView processing of keypresses and implement them in my form?
    Last edited by bear35645; 3rd February 2015 at 22:29. Reason: updated contents

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QDialog keypress event

    You can install an event filter on the treeview and only let events pass that you don't need to handle yourself.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Thumbs up Re: QDialog keypress event

    I added a call to the grabKeyboard() method in the constructor, then when the proper key is pressed to close the form, I added releaseKeyBoard(). I realize I may have to handle the navigation in the table view, but that is Ok.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,014
    Thanks
    280
    Thanked 816 Times in 805 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QDialog keypress event

    I added a call to the grabKeyboard()
    That seems a rather extreme solution when simply adding an event filter to the table view would do it. Any time you grab control of an input device, there is always the chance that some corner case will lead to a condition where your app is locked up and there is no way to get back to the widget that has grabbed the inputs.

    I realize I may have to handle the navigation in the table view
    Which is exactly what the event filter would let you easily do - as anda_skoa said, you let events pass that you don't need to handle yourself.

  7. #7
    Join Date
    Oct 2014
    Posts
    81
    Thanks
    20
    Thanked 9 Times in 9 Posts
    Qt products
    Qt5
    Platforms
    Windows
    Wiki edits
    7

    Default Re: QDialog keypress event

    Quote Originally Posted by d_stranz View Post
    Which is exactly what the event filter would let you easily do - you let events pass that you don't need to handle yourself.
    Or even let all events pass, if you just need to use the filter as a means to act on some event(s) before the target widget does.

Similar Threads

  1. Handling keypress event in textedit
    By Niamita in forum Qt Programming
    Replies: 1
    Last Post: 14th December 2011, 10:07
  2. QEvent::KeyPress problem, when will this event appear?
    By batileon in forum Qt Programming
    Replies: 11
    Last Post: 17th June 2011, 05:23
  3. problem with keypress event and QTableView
    By ranna in forum Qt Programming
    Replies: 4
    Last Post: 24th August 2009, 16:13
  4. how to combine keypress event and mousebuttonpress event?
    By hildebrand in forum Qt Programming
    Replies: 2
    Last Post: 26th May 2009, 23:08
  5. keypress event
    By vishesh in forum Qt Programming
    Replies: 2
    Last Post: 3rd November 2007, 14:12

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.