Results 1 to 9 of 9

Thread: keyPressEvent doesn't work

  1. #1
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default keyPressEvent doesn't work

    Hi

    This is my steps:

    - I created the "Qt Widget Application"
    - I opened the "MainWindow.ui" file and put on it the "mdiArea"
    - I created the "Qt Designer Form Class" and wrote in the "MainWindow.h" and "MainWindow.cpp" files:
    Qt Code:
    1. private:
    2. Ui::MainWindow *ui;
    3. FirstWindow *m_firstWindow;
    4. SecondWindow *m_secondWindow;
    5. ThirdWindow *m_thirdWindow;
    To copy to clipboard, switch view to plain text mode 

    MainWindow.cpp
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. m_firstWindow = new FirstWindow;
    8. m_secondWindow = new SecondWindow;
    9. m_thirdWindow = new ThirdWindow;
    10.  
    11. QMdiSubWindow *w1 = ui->mdiArea->addSubWindow( m_firstWindow );
    12. ui->mdiArea->addSubWindow( m_secondWindow );
    13. ui->mdiArea->addSubWindow( m_thirdWindow );
    14.  
    15. ui->mdiArea->cascadeSubWindows();
    16.  
    17. w1->resize( 500, 500 );
    18. }
    To copy to clipboard, switch view to plain text mode 

    - I opened "FirstWindow.h" and "FirstWindow.cpp" files and wrote:
    FirstWindow.h
    Qt Code:
    1. private:
    2. Ui::FirstWindow *ui;
    3. void keyPressEvent( QKeyEvent *event );
    4. };
    To copy to clipboard, switch view to plain text mode 

    FirstWindow.cpp
    Qt Code:
    1. void FirstWindow::keyPressEvent(QKeyEvent *event)
    2. {
    3. qDebug() << "keyPressEvent";
    4. }
    To copy to clipboard, switch view to plain text mode 

    - I ran the application and pressed on the keys. But I didn't see the text "keyPressEvent" on the "Application Output"

    Thank you!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: keyPressEvent doesn't work

    Does the FirstWindow instance have focus when you press a key?

    Key events are delivered to the widget which has keyboard focus, see QApplication::focusWidget().

    Cheers,
    _

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

    8Observer8 (24th August 2014)

  4. #3
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: keyPressEvent doesn't work

    I clicked on the window with the name "First Window" and press the "Space" key. Nothing happened.

    290.png


    Added after 11 minutes:


    You are right. I added the button on the window:
    Qt Code:
    1. void FirstWindow::on_pushButton_clicked()
    2. {
    3. qDebug() << focusWidget()->windowTitle();
    4. }
    To copy to clipboard, switch view to plain text mode 

    Output:
    ""
    I don't understand why.
    Last edited by 8Observer8; 24th August 2014 at 16:58.

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

    Default Re: keyPressEvent doesn't work

    Window title is not really helpful.
    Check the value of fodusWidget() vs. "this"

    Cheers,
    _

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

    8Observer8 (26th August 2014)

  7. #5
    Join Date
    Mar 2010
    Posts
    86
    Thanks
    11
    Thanked 7 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Smile Re: keyPressEvent doesn't work

    not sure if this helps but
    in your header file I thought event method classes were supposed to be:
    Qt Code:
    1. protected:
    2. void keyPressEvent( QKeyEvent *event );
    3.  
    4. private:
    5.  
    6. public:
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to budda for this useful post:

    8Observer8 (26th August 2014)

  9. #6
    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 doesn't work

    Quote Originally Posted by 8Observer8 View Post
    I clicked on the window with the name "First Window" and press the "Space" key. Nothing happened.
    Did you set QWidget::focusPolicy for the widget appropriately? If widget has Qt::NoFocus then it won't accept focus and thus won't receive key events.
    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.


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

    8Observer8 (26th August 2014)

  11. #7
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: keyPressEvent doesn't work

    I wrote it the constructor "setFocusPolicy( Qt::StrongFocus );"
    Qt Code:
    1. FirstWindow::FirstWindow(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::FirstWindow)
    4. {
    5. ui->setupUi(this);
    6. setFocusPolicy( Qt::StrongFocus );
    7. }
    To copy to clipboard, switch view to plain text mode 

    I added on the window the button:
    Qt Code:
    1. void FirstWindow::on_pushButton_clicked()
    2. {
    3. if ( focusWidget() == this ) {
    4. qDebug() << "focusWidget() == this";
    5. } else {
    6. qDebug() << "focusWidget() != this";
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Output:
    focusWidget() != this

  12. #8
    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 doesn't work

    When you click the button, it is most likely that the button gets the focus. Set the button's focus policy to Qt::NoFocus and try again. Better yet reimplement focusInEvent() and focusOutEvent() for your widget and print debug statements there to see when it receives and loses focus.
    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.


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

    8Observer8 (26th August 2014)

  14. #9
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: keyPressEvent doesn't work

    Yes, I see! I added Qt::NoFocus for the button:
    Qt Code:
    1. FirstWindow::FirstWindow(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::FirstWindow)
    4. {
    5. ui->setupUi(this);
    6. setFocusPolicy( Qt::StrongFocus );
    7. ui->pushButton->setFocusPolicy( Qt::NoFocus );
    8. }
    To copy to clipboard, switch view to plain text mode 

    I added focusInEvent() and focusOutEvent():
    Qt Code:
    1. #include "FirstWindow.h"
    2. #include "ui_FirstWindow.h"
    3. #include "QDebug"
    4.  
    5. FirstWindow::FirstWindow(QWidget *parent) :
    6. QDialog(parent),
    7. ui(new Ui::FirstWindow)
    8. {
    9. ui->setupUi(this);
    10. setFocusPolicy( Qt::StrongFocus );
    11. ui->pushButton->setFocusPolicy( Qt::NoFocus );
    12. }
    13.  
    14. FirstWindow::~FirstWindow()
    15. {
    16. delete ui;
    17. }
    18.  
    19. void FirstWindow::keyPressEvent( QKeyEvent *event )
    20. {
    21. qDebug() << "FirstWindow: keyPressEvent";
    22. }
    23.  
    24. void FirstWindow::focusInEvent( QFocusEvent *event )
    25. {
    26. qDebug() << "FirstWindow: focusInEvent";
    27. }
    28.  
    29. void FirstWindow::focusOutEvent( QFocusEvent *event )
    30. {
    31. qDebug() << "FirstWindow: focusOutEvent";
    32. }
    33.  
    34. void FirstWindow::on_pushButton_clicked()
    35. {
    36. if ( focusWidget() == this ) {
    37. qDebug() << "focusWidget() == this";
    38. } else {
    39. qDebug() << "focusWidget() != this";
    40. }
    41. }
    To copy to clipboard, switch view to plain text mode 

    When I click on the button I see:
    focusWidget() == this
    I will write in the constructor this insruction "setFocusPolicy( Qt::StrongFocus );" for all my MDI subwidnows

Similar Threads

  1. keypressevent dont work when MainWindow is minimized
    By spitty_cash in forum Qt Programming
    Replies: 0
    Last Post: 17th October 2011, 16:13
  2. Screenshot example doesn't work on Mac OSX
    By Damiano in forum Qt Programming
    Replies: 0
    Last Post: 19th January 2011, 11:17
  3. Example in Qt labs doesn't work
    By matko in forum Newbie
    Replies: 1
    Last Post: 19th November 2009, 18:59
  4. QGLWidget, keyPressEvent does not work
    By ricardo in forum Qt Programming
    Replies: 7
    Last Post: 3rd July 2009, 10:28
  5. setTabStopWidth doesn't work
    By discostu in forum Qt Programming
    Replies: 3
    Last Post: 19th November 2007, 08:29

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.