Results 1 to 5 of 5

Thread: keyPressEvent problem

  1. #1
    Join Date
    Mar 2006
    Posts
    19
    Thanks
    1

    Default keyPressEvent problem

    hi,

    i m trying to capture the key press event. Now, this works for all other keys except the Tab key. What could be the problem?? Here is the code

    Qt Code:
    1. Image_check::Image_check(QWidget *parent, Qt::WFlags flags)
    2. : QWidget(parent, flags)
    3. {
    4. ui.setupUi(this);
    5. ui.pushButton->setFocus();
    6. /* ui.pushButton->setFocusPolicy(Qt::StrongFocus );
    7.   ui.pushButton_2->setFocusPolicy(Qt::StrongFocus );*/
    8.  
    9. }
    10. void Image_check::keyPressEvent(QKeyEvent *e)
    11. {
    12. if(e->key() == Qt::Key_Tab)
    13. {
    14. if(sender() == ui.pushButton)
    15. {
    16. QMessageBox::information(this, "Application name",
    17. "first"
    18. "The factory default will be used instead.");
    19. }
    20. else
    21. {
    22. QMessageBox::information(this, "Application name",
    23. "second"
    24. "The factory default will be used instead.");
    25. }
    26. QWidget::keyPressEvent(e);
    27. }
    28. else
    29. QWidget::keyPressEvent(e);
    30. }
    To copy to clipboard, switch view to plain text mode 
    I need to capture the Tab key..and that only is not working
    Please tell ur suggestions on this..Thanx

    Amulya

  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 problem

    You can't catch the tab key in keyPressEvent. Override event() instead.

    Qt Code:
    1. bool MyClass:event( QEvent *evt ) {
    2. if ( evt->type() == QEvent::KeyPress ) {
    3. QKeyEvent *ke = (QKeyEvent *)evt;
    4. if ( ke->key() == Key_Tab ) {
    5. // special tab handling here
    6. ke->accept();
    7. return TRUE;
    8. }
    9. }
    10. return QWidget::event( evt );
    11. }
    To copy to clipboard, switch view to plain text mode 

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

    joshlareau (23rd July 2007)

  4. #3
    Join Date
    Jan 2008
    Posts
    91
    Thanks
    8

    Default Re: keyPressEvent problem

    " QKeyEvent *ke = (QKeyEvent *)evt; "

    I couldn't do this conversion. IDE always appers this message "unused variable 'ke' ".I think there is some thing wrong here.

    deneme.cpp:27: warning: unused variable 'ke'
    deneme.cpp:28: error: `ke' undeclared (first use this function)

  5. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: keyPressEvent problem

    There must be some typing mistake,,, can u post the code part which is giving error ??

  6. #5
    Join Date
    Jan 2008
    Posts
    91
    Thanks
    8

    Default Re: keyPressEvent problem

    okay i solve this problem.
    Last edited by anafor2004; 22nd January 2008 at 13:22.

Similar Threads

  1. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  2. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.