Results 1 to 2 of 2

Thread: Handling keypress event in textedit

  1. #1
    Join Date
    May 2011
    Posts
    120
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Handling keypress event in textedit

    HI all

    I want to handle key event when i type anything in textedit.
    I am doing it likethis

    Qt Code:
    1. bool HomeWindow::eventFilter(QObject *object, QEvent *event) {
    2.  
    3. if (object == condition_text_edit && event->type() == QEvent::KeyPress)
    4. { QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    5. if (keyEvent->key())
    6. {
    7. qDebug("Enter Key Pressed...");
    8. qDebug()<<keyEvent->key();
    9. textedit.setText(keyEvent.text());
    10. return true;
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    It is handling keys one by one but i want to handle event when i typed in textedit so that i can prevent some keys from typing in textedit.

    Please suggest me how should i handle key event.

    Thanks

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Handling keypress event in textedit

    It needs few corrections:
    Qt Code:
    1. bool HomeWindow::eventFilter(QObject *object, QEvent *event) {
    2. if (object == condition_text_edit && event->type() == QEvent::KeyPress)
    3. { QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    4. // key() returns the code of the key that was pressed, so you need to compare it with the keys you want to filter out, for example 'A' character
    5. // see the QKeyEvent::key docs for complete list of key codes
    6. if (keyEvent->key() == Qt::Key_A)
    7. {
    8. qDebug("Enter Key Pressed...");
    9. qDebug()<<keyEvent->key();
    10. // now it will be impossible to type the 'A' char into text edit
    11. return true;
    12. }
    13. }
    14. return QWidget::eventFilter(object,event); // you need to call base class implementation to save "normal" behavior
    15. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to stampede for this useful post:

    Niamita (14th December 2011)

Similar Threads

  1. QEvent::KeyPress problem, when will this event appear?
    By batileon in forum Qt Programming
    Replies: 11
    Last Post: 17th June 2011, 05:23
  2. problem with keypress event and QTableView
    By ranna in forum Qt Programming
    Replies: 4
    Last Post: 24th August 2009, 16:13
  3. how to combine keypress event and mousebuttonpress event?
    By hildebrand in forum Qt Programming
    Replies: 2
    Last Post: 26th May 2009, 23:08
  4. Problem with KeyPress event and QTableView
    By ranna in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2009, 20:01
  5. keypress event
    By vishesh in forum Qt Programming
    Replies: 2
    Last Post: 3rd November 2007, 14:12

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.