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
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 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

  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

    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.