Results 1 to 4 of 4

Thread: keyPressEvent not being called...

  1. #1
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Smile keyPressEvent not being called...

    I have a class that doesn't seem to override the keyPressEvent properly, or at least, the function isn't getting called.

    It inherits from QDialog

    Qt Code:
    1. class SelectionDialog : public QDialog
    To copy to clipboard, switch view to plain text mode 

    As a public function, it has:

    Qt Code:
    1. void keyPressEvent( QKeyEvent* event );
    To copy to clipboard, switch view to plain text mode 

    Which is defined as:

    Qt Code:
    1. void SelectionDialog::keyPressEvent( QKeyEvent* event )
    2. {
    3. switch( event->key() )
    4. {
    5. case Qt::Key_Up:
    6. {
    7. upClicked();
    8. break;
    9. }
    10. case Qt::Key_Down:
    11. {
    12. downClicked();
    13. break;
    14. }
    15. default:
    16. {
    17. QDialog::keyPressEvent( event );
    18. break;
    19. }
    20. };
    21. }
    To copy to clipboard, switch view to plain text mode 

    In the constructor of my class, I do this

    Qt Code:
    1. setFocusPolicy( Qt::StrongFocus );
    2. setFocus( Qt::PopupFocusReason );
    3. setEnabled( true );
    To copy to clipboard, switch view to plain text mode 

    Yet when this dialog box launches in the program, no amount of key presses gets me into this function.
    What am I missing or doing wrong?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: keyPressEvent not being called...

    The dialog has to get focus when it is shown and not when it is constructed. Only visible components can have focus.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Widget : public QWidget {
    4. public:
    5. Widget() : QWidget() { setFocusPolicy(Qt::StrongFocus); }
    6. protected:
    7. void keyPressEvent(QKeyEvent *ke) {
    8. qDebug() << ke->key();
    9. }
    10. };
    11.  
    12.  
    13. int main(int argc, char **argv) {
    14. QApplication app(argc, argv);
    15. Widget w;
    16. w.show(); // gets focus automatically since is the only widget shown
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    bruceariggs (10th October 2011)

  4. #3
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: keyPressEvent not being called...

    Hey Wysota,

    Thanks for the response, but now I have a question about how that can be done.

    I display my QDialog by calling exec(), which displays the dialog to the screen, how can I call setFocus() on the Dialog if exec() is what both shows, executes, and closes the window after OK or Cancel is clicked?

    Qt Code:
    1. SelectionDialog* dialog = new SelectionDialog();
    2.  
    3. // Too early to set focus?
    4.  
    5. if( dialog->exec() ) // handles the show and execution
    6. {
    7. // Too late to set focus?
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by bruceariggs; 10th October 2011 at 21:38. Reason: spelling corrections

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: keyPressEvent not being called...

    If the dialog doesn't get focus automatically (look out, if there is a default button on the dialog, this button will steal the focus!), you can reimplement showEvent() for the dialog and set focus from within there.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    bruceariggs (10th October 2011)

Similar Threads

  1. keypressevent
    By djwk in forum Newbie
    Replies: 9
    Last Post: 5th July 2010, 02:12
  2. keyPressEvent issue
    By oguzy in forum Qt Programming
    Replies: 1
    Last Post: 7th November 2008, 15:39
  3. keyPressEvent() reimplementation
    By aurelius in forum Newbie
    Replies: 5
    Last Post: 30th October 2008, 16:23
  4. keyPressEvent not being caught
    By vieraci in forum Qt Programming
    Replies: 2
    Last Post: 29th October 2007, 12:53
  5. keypressevent with pyqt4
    By coldlin in forum Qt Programming
    Replies: 3
    Last Post: 3rd October 2007, 16:52

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.