Results 1 to 5 of 5

Thread: Enter causes cursor movement like in Tab Order

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2010
    Posts
    62
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Enter causes cursor movement like in Tab Order

    Hello,

    In a dialog there are some edit fields e.g. QLineEdit. It is possible to set the Tab order, so that the cursor moves from one field to another when the Tab button is pressed.
    Is it possible to achieve the same effect, when the return button is pressed?

    Thx for your hints

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Enter causes cursor movement like in Tab Order

    For the first question the answer is
    "use QWidget::setTabOrder" or Designer

    For the second you have to reimplement the QWidget::keyPressEvent and customize the default behaviour in case of Qt::Key_Enter or Qt::Key_Return

    for example

    Qt Code:
    1. void MyDialog::keyPressEvent(QKeyEvent *e)
    2. {
    3. switch (e->key()) {
    4. case Qt::Key_Return:
    5. case Qt::Key_Enter:
    6. {
    7. QWidget* w = focusWidget();
    8. QWidget* next = 0;
    9. if (w == ui->lineEdit) {
    10. next = ui->lineEdit_2;
    11. }
    12. else if (w == ui->lineEdit_2) {
    13. next = ui->lineEdit_3;
    14. }
    15. else if (w == ui->lineEdit_3) {
    16. next = ui->buttonBox;
    17. }
    18. if (next)
    19. next->setFocus();
    20. }
    21. break;
    22.  
    23. default:
    24. QDialog::keyPressEvent(e);
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by mcosta; 19th July 2011 at 08:43. Reason: updated contents
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Sep 2010
    Posts
    62
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Enter causes cursor movement like in Tab Order

    Quote Originally Posted by mcosta View Post
    For the first question the answer is
    "use QWidget::setTabOrder" or Designer

    For the second you have to reimplement the QWidget::keyPressEvent and customize the default behaviour in case of Qt::Key_Enter or Qt::Key_Return

    for example

    Qt Code:
    1. void MyDialog::keyPressEvent(QKeyEvent *e)
    2. {
    3. switch (e->key()) {
    4. case Qt::Key_Return:
    5. case Qt::Key_Enter:
    6. {
    7. QWidget* w = focusWidget();
    8. QWidget* next = 0;
    9. if (w == ui->lineEdit) {
    10. next = ui->lineEdit_2;
    11. }
    12. else if (w == ui->lineEdit_2) {
    13. next = ui->lineEdit_3;
    14. }
    15. else if (w == ui->lineEdit_3) {
    16. next = ui->buttonBox;
    17. }
    18. if (next)
    19. next->setFocus();
    20. }
    21. break;
    22.  
    23. default:
    24. QDialog::keyPressEvent(e);
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 
    Thank you for the answer, but I am afraid it's not that easy with the keyPressEvent.
    What about disabled fields (in different combinations)? The setTabOrder solves this automatically.
    Of course I can imagine how to solve this, but I am looking for a QT mechanism that helps to fix such an issue.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Enter causes cursor movement like in Tab Order

    As an alternate solution, you can capture the KeyPressEvent, either using the above method or using a event filter and send a tab key event to the parent widget (i.e. converting a enter key event on a widget into a tab key event on the parent widget). This solution will respect the tab order, and is scalable solution, i.e. will work even if you changed tab order, or add more widgets in future.

    As always, there are many ways to do a given job

  5. #5
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Enter causes cursor movement like in Tab Order

    You can also use QWidget::focusNextPrevChild like

    Qt Code:
    1. void MyDialog::keyPressEvent(QKeyEvent *e)
    2. {
    3. switch (e->key ()) {
    4. case Qt::Key_Return:
    5. case Qt::Key_Enter:
    6. focusNextPrevChild (true);
    7. break;
    8.  
    9. default:
    10. QDialog::keyPressEvent (e);
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    or

    Qt Code:
    1. void MyDialog::keyPressEvent(QKeyEvent *e)
    2. {
    3. switch (e->key ()) {
    4. case Qt::Key_Return:
    5. case Qt::Key_Enter:
    6. {
    7. QKeyEvent* newEvent = new QKeyEvent (QEvent::KeyPress, Qt::Key_Tab, e->modifiers ());
    8. qApp->postEvent (this, newEvent, 0);
    9. }
    10. break;
    11.  
    12. default:
    13. QDialog::keyPressEvent (e);
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by mcosta; 20th July 2011 at 13:38. Reason: updated contents
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. QWidget movement
    By fakefish in forum Qt Programming
    Replies: 8
    Last Post: 19th April 2011, 12:02
  2. how to set Cursor with ms excel cursor like style?
    By xiongxiongchuan in forum Qt Programming
    Replies: 2
    Last Post: 10th August 2010, 02:37
  3. Replies: 2
    Last Post: 2nd October 2009, 15:32
  4. Replies: 1
    Last Post: 14th September 2008, 23:05
  5. How to synchronize text cursor and cursor in QTextEdit
    By kennyxing in forum Qt Programming
    Replies: 6
    Last Post: 18th February 2007, 09:14

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
  •  
Qt is a trademark of The Qt Company.