Results 1 to 9 of 9

Thread: keypressEventFilter() to accept Enter and Return Tab

  1. #1
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    77
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default keypressEventFilter() to accept Enter and Return Tab

    I have a keypressEventFilter on a textedit box in order to trap the Return or Enter key press. This part works great. I then wish to continue the event returning a Tab instead.

    The documentation talks about it -
    "(Sometimes in data-entry applications Enter does the same as Tab; this can easily be achieved in Qt by implementing an event filter.) " ,

    but there is no example or other documentation to explain how to change the the key press from Qt::Key_Enter to Qt::Key_Tab. There is only the generic "// special tab handling here" comment where code should be to illustrate what the text has already given as an example.

    I thought I could return the Qt:Key_Tab value but the return must be an event not a value. I don't think issuing another keypress with the QKeyPress or QEvent is right, so what is appropriate for this situation?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: keypressEventFilter() to accept Enter and Return Tab

    Perhaps I overlooked something but I think you might have just found a bug in the docs. The described behaviour is fairly possible with QWidget::focusNextPrevChild() BUT it's protected so actually one needs to reimplement the key press event handler, not to install an event filter:
    Qt Code:
    1. myTextEdit->setTabChangesFocus(true);
    2.  
    3. void MyTextEdit::keyPressEvent(QKeyEvent* event)
    4. {
    5. if (event->key() == Qt::Key_Return)
    6. {
    7. focusNextPrevChild(true);
    8. }
    9. else
    10. {
    11. QTextEdit::keyPressEvent(event);
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    77
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: keypressEventFilter() to accept Enter and Return Tab

    Implementing your code in this way is not the whole story is it? I mean this code:

    void txtName::keyPressEvent(QKeyEvent* event)
    {
    if (event->key() == Qt::Key_Return))
    {
    focusNextPrevChild(true);
    }
    else
    {
    QTextEdit::keyPressEvent(event);
    }
    }


    causes an error because it does not know that txtName is an object on my MainWindow. - "Error:Undeclared first use". Plus the focusNextPrevChild(true) statement draws an error that is similar. What am I missing?

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: keypressEventFilter() to accept Enter and Return Tab

    What you need is a QTextEdit subclass:
    Qt Code:
    1. class MyTextEdit : public QTextEdit
    2. {
    3. public:
    4. ...
    5. protected:
    6. void keyPressEvent(QKeyEvent* event);
    7. ...
    8. };
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    77
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: keypressEventFilter() to accept Enter and Return Tab

    Ok, the TextEdit is subclassed and now it does not show up on my MainForm. It is in a QFrame container. If I issue a show() it is a separate window and the setTabOrder() gives a debug message about two different windows. Whatz up with that?

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: keypressEventFilter() to accept Enter and Return Tab

    I don't know. Probably something wrong in other part of the code. Can you post it?

  7. #7
    Join Date
    Feb 2007
    Posts
    16
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: keypressEventFilter() to accept Enter and Return Tab

    Has anyone considered using the QShortcut class to handle intercepting keyboard shortcuts? I had a table widget that was "stealing" the enter and tab keys from me and I got around the problem by using QShortCut as follows:

    //Intercept the CTRL-TAB key from the table widget
    QShortcut* shortcutTabKey;
    shortcutTabKey = new QShortcut( QKeySequence( tr("Ctrl+TAB") ), m_tableWidget );
    connect( shortcutTabKey, SIGNAL( activated() ), this, SLOT( OnTableShortCutTabKey() ) );

    Hopefully this helps someone, as it seems easier than overriding QWidget::event.

  8. #8
    Join Date
    Apr 2007
    Location
    Toruń, POLAND
    Posts
    24
    Thanks
    7
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: keypressEventFilter() to accept Enter and Return Tab

    Quote Originally Posted by ad5xj View Post
    The documentation talks about it -
    "(Sometimes in data-entry applications Enter does the same as Tab; this can easily be achieved in Qt by implementing an event filter.) " ,
    This is quite opposite to what you want to achieve. In your case you have to kill filter because it is already installed -- read about tab handling, it is a special key for focus switching.

    have a nice day, bye

  9. #9
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: keypressEventFilter() to accept Enter and Return Tab

    It might be as simple as this (event filter function frame taken from the docs):

    Qt Code:
    1. bool FilterObject::eventFilter(QObject *object, QEvent *event)
    2. {
    3. if (object == target && event->type() == QEvent::KeyPress) {
    4. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    5. if (keyEvent->key() == Qt::Key_Enter) {
    6. *keyEvent = QKeyEvent(Qt::Key_Tab);
    7. }
    8. }
    9. return false;
    10. }
    To copy to clipboard, switch view to plain text mode 

    Seems that the QKeyEvent lacks a setKey() function. But I'd think the event parameter is not non-const for nothing.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

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.