Results 1 to 8 of 8

Thread: QLineEdit & keyPressEvent(QKeyEvent *)

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QLineEdit & keyPressEvent(QKeyEvent *)

    Hi,

    I have reimplemented the keyPressEvent of QLineEdit ( Qt4). Following is the code
    Qt Code:
    1. void LineEdit::keyPressEvent(QKeyEvent *e)
    2. {
    3. if(e->key() == Qt::Key_Backtab){
    4. //Do something
    5. }
    6. else if(e->key() == Qt::Key_Return || e->key() == Qt::Key_Tab || e->key() == Qt::Key_Enter){
    7. //Do something
    8. }
    9. else{
    10. QLineEdit::keyPressEvent(e);
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    1. When I press the tab key this part of code is not executed. Another widget which is the part of my application gets the focus (Which should have happened if I wouldn't have reimplemented the keyPressEvent). How can i override this behaviour?

    2. Is Qt::KeyBacktab same as Shift+Tab?

    3. Can some please show me how to use modifiers. I am trying to use it in the following way :
    Qt Code:
    1. if(e->key() == Qt::Key_Return && e->modifiers() == Qt::ShiftModifier){
    2.  
    3. }
    To copy to clipboard, switch view to plain text mode 
    Is it the correct way of using modifiers?

    Thanks a lot.

  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: QLineEdit & keyPressEvent(QKeyEvent *)

    AFAIR tab keys are not handled by keyPressEvents. You have to implement event() to catch them.

    It's explained in the docs.

  3. #3
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit & keyPressEvent(QKeyEvent *)

    Thanks for replying

    Qt Code:
    1. bool LineEdit::event(QEvent *e)
    2. {
    3. if(e->type() == QEvent::KeyPress){
    4. QKeyEvent *ke = static_cast<QKeyEvent *>(e);
    5. if(ke->modifiers() & Qt::ShiftModifier){
    6. if (ke->key() == Qt::Key_Tab) {
    7. editMode->focusNextWidget();
    8. return true;
    9. }
    10. }
    11. else if (ke->key() == Qt::Key_Tab) {
    12. editMode->focusNextWidget();
    13. return true;
    14. }
    15. }
    16. return QLineEdit::event(e);
    17. }
    To copy to clipboard, switch view to plain text mode 

    In the above code I am trying to achieve the same functionality for both tab and shift+tab.
    Tab works perfectly whereas shift+tab is not working.

    Pls help
    Thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit & keyPressEvent(QKeyEvent *)

    What ke->modifiers() return ?
    Maiby try use QAccel?
    a life without programming is like an empty bottle

  5. #5
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit & keyPressEvent(QKeyEvent *)

    When i debug the program pointer gets into the first if condition but it is not getting into the second one

    Qt Code:
    1. if(ke->modifiers() & Qt::ShiftModifier){
    2. if (ke->key() == Qt::Key_Tab) {
    3. editMode->focusNextWidget();
    4. return true;
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 

  6. #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: QLineEdit & keyPressEvent(QKeyEvent *)

    Shift+Tab is BackTab. Try it instead of that modifier.

    BTW. Why are you reimplementing Tab to do the same it does by its own?

  7. #7
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit & keyPressEvent(QKeyEvent *)

    Oh yes...it becouse first pressing its Shift. So you must catch 2 events : frist for Shift and second for Tab
    a life without programming is like an empty bottle

  8. #8
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit & keyPressEvent(QKeyEvent *)

    Quote Originally Posted by wysota
    BTW. Why are you reimplementing Tab to do the same it does by its own?
    I am yet to write the code for backtab. I just wanted to see how modifiers work before implementing the functionality, that is why i am calling the same function in the same place.

    Thanks a lot

  9. The following user says thank you to munna for this useful post:

    :db:sStrong (19th May 2006)

Similar Threads

  1. Replies: 10
    Last Post: 12th February 2009, 07:23
  2. problem with QLineEdit and QPalette
    By impeteperry in forum Qt Programming
    Replies: 4
    Last Post: 15th October 2008, 17:05
  3. QLineEdit
    By rick_st3 in forum Newbie
    Replies: 1
    Last Post: 14th June 2008, 09:05
  4. Pointer Question related to QLineEdit
    By ChrisReath in forum Qt Programming
    Replies: 1
    Last Post: 23rd May 2008, 15:13
  5. QValidator, regular expressions and QLineEdit
    By hvengel in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2007, 01:25

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.