Results 1 to 16 of 16

Thread: Change Focus With Enter Key in TextEdit box

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

    Default Change Focus With Enter Key in TextEdit box

    I am using text edit boxes to collect data to store in a SQLite database. In most Windows programs the enter key can be directed to change focus (as in VB). I have not found a way to change focus with the enter key in a text edit box. Trying to capture the key press event has not been particularly successful. There is very little documentation or examples to go by for this particular aspect. The goal is to make a cross platform app (Windows, Vista, Linux, Mac, etc) that works the same on all. I don't want the Windows conventions to dominate but this one is fairly common and expected.

  2. #2
    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: Change Focus With Enter Key in TextEdit box

    Read the docs about event filters.
    You could catch it if you install an event on the widget.

    Regards

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

    Default Re: Change Focus With Enter Key in TextEdit box

    Thanks for the obvious. I did read the docs and I did set a filter. But I was never able to trap the enter key. There is nothing specific to this situation - that is why I posted this problem.

    If there is something not-so-obvious I am missing please enlighten me.

  4. #4
    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: Change Focus With Enter Key in TextEdit box

    First of all, it was not so obvious because you did not mention what method you used to intercept the key press.

    Second of all, what key code were you looking for? Because there is a difference between Qt::Key_Enter( the numpad key) and Qt::Key_Return.

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

    Default Re: Change Focus With Enter Key in TextEdit box

    Marcel if you read my post it says "In most Windows programs the enter key can be directed to change focus". For windows users there is no difference in the "Return" key and the numeric keypad "Enter" key.

    Also, I NOW know that the Qt:Key_Enter and Qt:Key_Return refer to two different hardware keys. I will change the code to look for both and see if it makes a difference.

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

    Default Re: Change Focus With Enter Key in TextEdit box

    I still get no response from my code looking for either key. Here is a snippet:


    bool MainWindow::eventFilter(QObject *object, QEvent *event)
    {
    if (object == this && event->type() == QEvent::KeyPress)
    {
    QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    if (keyEvent->key() == Qt::Key_Enter)
    {
    // Special tab handling
    qDebug("Enter Key Pressed...");
    return true;
    }
    else if (keyEvent->key() == Qt::Key_Return)
    {
    // Special tab handling
    qDebug("Enter Key Pressed...");
    return true;
    return false;
    }
    }
    return false;
    }

    Is this not the correct method?

  7. #7
    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: Change Focus With Enter Key in TextEdit box

    Marcel if you read my post it says "In most Windows programs the enter key can be directed to change focus". For windows users there is no difference in the "Return" key and the numeric keypad "Enter" key.
    ok,ok.

    I see you installed the main window as event filter for itself.
    I have never seen that done, but I tend to believe it is incorrect. What is the point in doing that? Are you sure it is getting called? You might as well override keyPressEvent.

    An event filter typically is a parent widget. Filtering events means catching them before they actually reach the "watched" object. This is not the case with your implementation because the event is already at the widget, since it coincides with the filter.

    Regards

  8. #8
    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: Change Focus With Enter Key in TextEdit box

    If you want to filter events for the main window, then subclass QApplication and override it's event filter. I think it should work.

    Regards

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

    Default Re: Change Focus With Enter Key in TextEdit box

    So are you saying that the FilterObject is not the window? it is the ?what? the text box or is the text box a child widget of the filter - I'm cornfused.

  10. #10
    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: Change Focus With Enter Key in TextEdit box

    bool QObject::eventFilter ( QObject * watched, QEvent * event ) [virtual]
    Filters events if this object has been installed as an event filter for the watched object.
    In your reimplementation of this function, if you want to filter the event out, i.e. stop it being handled further, return true; otherwise return false.
    Example:
    class MainWindow : public QMainWindow
    {
    public:
    MainWindow();
    protected:
    bool eventFilter(QObject *obj, QEvent *ev);
    private:
    QTextEdit *textEdit;
    };
    MainWindow::MainWindow()
    {
    textEdit = new QTextEdit;
    setCentralWidget(textEdit);
    textEdit->installEventFilter(this);
    }
    bool MainWindow::eventFilter(QObject *obj, QEvent *event)
    {
    if (obj == textEdit) {
    if (event->type() == QEvent::KeyPress) {
    QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
    qDebug() << "Ate key press" << keyEvent->key();
    return true;
    } else {
    return false;
    }
    } else {
    // pass the event on to the parent class
    return QMainWindow::eventFilter(obj, event);
    }
    }
    Notice in the example above that unhandled events are passed to the base class's eventFilter() function, since the base class might have reimplemented eventFilter() for its own internal purposes.
    Warning: If you delete the receiver object in this function, be sure to return true. Otherwise, Qt will forward the event to the deleted object and the program might crash.
    See also installEventFilter().
    No, it is the window, but you just don't do it correctly?
    From the code I conclude that you want to filter events for the Main Widnow. Is that so?
    See the code bellow. It is an illustration on how event filters should be used.
    There, the main window is the filter for one of its children, not for itself( that definitely can't work).

    Regards

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

    Default Re: Change Focus With Enter Key in TextEdit box

    Ok, the missing piece you supplied was where to install the event filter. I changed my code and now the enter key fires the event filter. However, none of the widgets on the MainWindow show up. I even moved the installEventFilter() to the end of the constructor and still nothing shows on the window - as though the control with the focus was the only one on the form.

  12. #12
    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: Change Focus With Enter Key in TextEdit box

    You probably don't let any other events pass either.
    Could you post your new event filter?

    Events that do not concern you should be ignored by the filter.

    Regards

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

    Default Re: Change Focus With Enter Key in TextEdit box

    // in constructor
    setCentralWidget(txtName);
    txtName->installEventFilter(this);
    //
    ////////// event filter /////////////
    bool MainWindow::eventFilter(QObject *object, QEvent *event)
    {
    if (object == txtName && event->type() == QEvent::KeyPress)
    {
    QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    if (keyEvent->key() == Qt::Key_Enter)
    {
    // Special tab handling
    qDebug("Enter Key Pressed...");
    return true;
    }
    else if (keyEvent->key() == Qt::Key_Return)
    {
    // Special tab handling
    qDebug("Enter Key Pressed...");
    return true;
    }
    else
    {
    return QMainWindow::eventFilter(object, event);
    }
    }
    else
    {
    return QMainWindow::eventFilter(object, event);
    }
    }
    //

    I will eventually add four other textedit controls to the statements for the same action.

  14. #14
    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: Change Focus With Enter Key in TextEdit box

    The code seems OK.
    You do this:
    Qt Code:
    1. setCentralWidget(txtName);
    To copy to clipboard, switch view to plain text mode 

    What other widgets do you refer to?

    This makes the text edit take over the entire client area in the window.

    To add other widgets:
    Qt Code:
    1. QWidget *w = new QWidget(this);
    2. l->addWidget(some widget);
    3. l->addWidget(some widget);
    4. l->addWidget(some widget);
    5. ...
    6. [B]l->addWidget(txtEdit)[/B];
    7. ...
    8. l->addWidget(some widget);
    9. w->setLayout(l);
    10. this->setCentralWidget(w);
    To copy to clipboard, switch view to plain text mode 


    This is just an example. You can use whatever layout you like.
    The point is that in order to add multiple widgets in a main window client area, you need to have a container for them.

    This is why you don't see any other widgets.

    Regards

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

    Default Re: Change Focus With Enter Key in TextEdit box

    Ok I worked it out. I did not need to set the control as the central widget just installEventFilter(). When I changed the code it all worked. Thanks a million for your help.

  16. #16
    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: Change Focus With Enter Key in TextEdit box

    You're welcome.

Similar Threads

  1. Replies: 3
    Last Post: 26th September 2006, 13:16

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.