Results 1 to 3 of 3

Thread: QWidget::eventFilter() not catching key combinations

  1. #1
    Join Date
    Sep 2006
    Posts
    38
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default QWidget::eventFilter() not catching key combinations

    Hello,

    I have a multitab application that implements a main object on each tab that's added. Within each object is a QTextEdit. For some reason, the QTextEdit is not capturing a "Ctrl+C" key sequence. So I installed an eventFilter in the object's constructor to attach to the QTextEdit. The object's code (simplified) is:

    Qt Code:
    1. TabObject::TabObject(QWidget *parent) :
    2. QWidget(parent),
    3. ui(new Ui::TabObject)
    4. {
    5. ui->setupUi(this);
    6. ui->myTextEdit->installEventFilter(this);
    7. }
    8.  
    9. bool TabObject::eventFilter(QObject *obj, QEvent *event)
    10. {
    11.  
    12. if (obj == ui->myTextEdit) {
    13.  
    14. if (event->type() == QEvent::KeyPress) {
    15. QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
    16. qDebug()<<"key is: "<<keyEvent->key();
    17. qDebug()<<"key type is: "<<keyEvent->type();
    18. if (keyEvent->matches(QKeySequence::Copy)){
    19. qDebug()<<"copy sequence entered...";
    20. ui->myTextEdit->copy();
    21. return true;
    22. }
    23. return false;
    24. } else {
    25. return false;
    26. }
    27. } else {
    28. return TabObject::eventFilter(obj, event);
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

    What happens with the qDebug() output is that it picks up on the CTRL key being pressed but, while CTRL is being held down, it doesn't pick up on C, so the "copy sequence entered..." is not being outputted. I have this same eventfilter procedure installed on a popup window that inherits QMainWindow and that debug output doesn't pick up on CTRL, but picks up on C and the CTRL+C key sequence hits appropriately.

    Now, as I said, this is a multitab app built using QMainWindow that, when a new tab is to be created, executes:
    Qt Code:
    1. TabObject *tabObject = new TabObject;
    2. mainTabWidget->addTab(tabObject)
    To copy to clipboard, switch view to plain text mode 

    The MainWindow reimplements QMainWindow::keyPressEvent() as follows, so that when the user presses ENTER or RETURN, a new tab is created:

    Qt Code:
    1. void MainWindow::keyPressEvent( QKeyEvent* e )
    2. {
    3. switch( e->key() )
    4. {
    5. case Qt::Key_Enter:
    6. case Qt::Key_Return:
    7. // ** Code to open new tab **//
    8. break;
    9. default:
    10. ;
    11. }
    12. QMainWindow::keyPressEvent( e );
    13. }
    To copy to clipboard, switch view to plain text mode 

    So I really have a couple of questions:
    1) Could this keyPressEvent() reimplementation be intercepting or confusing the eventfilter attached to the QTextEdit in the implemented object?

    2) Does the eventFilter for QMainWindow and QWidget behave differently? As I mentioned above, it seems to be working for a popup window that inherits QMainWindow, and ignores the Control key, but seems to be the opposite for the QObject.

    Any guidance you can provide would be very helpful and appreciated.

    Thanks,
    --Derrick

  2. #2
    Join Date
    Sep 2006
    Posts
    38
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QWidget::eventFilter() not catching key combinations

    ***bump***

  3. #3
    Join Date
    Sep 2006
    Posts
    38
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QWidget::eventFilter() not catching key combinations

    This was answered in a different forum:

    http://qt-project.org/forums/viewthread/35164/

    Amazingly enough, the suggestion to add in the additional check of
    Qt Code:
    1. if (event->type() == QEvent::KeyPress || event->type() == QEvent::ShortcutOverride)
    To copy to clipboard, switch view to plain text mode 
    Fixed the issue. I'm not exactly sure how, but taking out the QEvent::ShortcutOverride check disables the Ctrl+C from being captured.

Similar Threads

  1. Using Qt to generate stringlist combinations
    By kalma in forum Qt Programming
    Replies: 4
    Last Post: 22nd March 2012, 04:33
  2. Keyboard combinations in QTableWidget
    By enricong in forum Qt Programming
    Replies: 2
    Last Post: 18th October 2011, 13:29
  3. Replies: 26
    Last Post: 12th April 2011, 14:06
  4. Capture key & key combinations from keyboard
    By aikidorb in forum Qt Programming
    Replies: 3
    Last Post: 5th June 2010, 13:13
  5. Replies: 7
    Last Post: 10th December 2007, 06:59

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.