Results 1 to 16 of 16

Thread: Change Focus With Enter Key in TextEdit box

Hybrid View

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

    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.

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

    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

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

    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.

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

    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

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

    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.

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

    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

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

    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.

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

    Default Re: Change Focus With Enter Key in TextEdit box

    You're welcome.

Similar Threads

  1. Replies: 3
    Last Post: 26th September 2006, 12: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
  •  
Qt is a trademark of The Qt Company.