Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QWidget::eventFilter() not catching key combinations
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QWidget::eventFilter() not catching key combinations

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 5.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kewljohny
    wrote on last edited by
    #1

    Hello,

    I posted this on qt-center but haven't gotten any takers. I have a multitab application that implements a main widget on each tab that's added. Within each widget is a QTextEdit. For some reason, the QTextEdit is not capturing a "Ctrl+C" key sequence through QAction's shortcut. So I installed an eventFilter in the object's constructor to attach to the QTextEdit. The object's code (simplified) is:

    @
    TabObject::TabObject(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TabObject)
    {
    ui->setupUi(this);
    ui->myTextEdit->installEventFilter(this);
    }

    bool TabObject::eventFilter(QObject *obj, QEvent *event)
    {
        if (obj == ui->myTextEdit) {
     
                     if (event->type() == QEvent::KeyPress) {
                              QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
                              qDebug()<<"key is: "<<keyEvent->key();
                              qDebug()<<"key type is: "<<keyEvent->type();
                               if (keyEvent->matches(QKeySequence::Copy)){
                                          qDebug()<<"copy sequence entered...";
                                           ui->myTextEdit->copy();
                                          return true;
                               }
                              return false;
                      } else {
                               return false;
                              }
            } else {
                  return TabObject::eventFilter(obj, event);
                     }
    

    }
    @

    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:

    @
    TabObject *tabObject = new TabObject;
    mainTabWidget->addTab(tabObject)
    @

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

    @ void MainWindow::keyPressEvent( QKeyEvent* e )
    {
    switch( e->key() )
    {
    case Qt::Key_Enter:
    case Qt::Key_Return:
    // ** Code to open new tab **//
    break;
    default:
    ;
    }
    QMainWindow::keyPressEvent( e );
    }
    @

    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!

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      [quote author="kewljohny" date="1385092711"]

      1. Could this keyPressEvent() reimplementation be intercepting or confusing the eventfilter attached to the QTextEdit in the implemented object?
        [/quote]
        eventFilters() kick in before the event handlers are called. So the handlers cannot interfer the eventFilters.
        [quote author="kewljohny" date="1385092711"]
      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.
        [/quote]
        eventFilter() is defined on QObject and behaves for all instances the same.

      What you can do is to additionally check for the event type QEvent::ShortcutOverride. You can safely add it to your existing check:
      @
      bool TabObject::eventFilter(QObject *obj, QEvent *event)
      {
      if (obj == ui->myTextEdit) {

                       if (event->type() == QEvent::KeyPress || event->type() == QEvent::ShortcutOverride) {
                                QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
                                .....
                        } else {
                                 return false;
                                }
              } else {
                    return TabObject::eventFilter(obj, event);
                       }
      

      }
      @
      This is a quick check if the shortcut interfers with another shortcut in the same scope.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kewljohny
        wrote on last edited by
        #3

        That's amazing. Simply adding the event->type == QEvent::ShortcutOverride statement in there fixed the issue and the CTRL+C hits in the child. I take that OR condition out, and it doesn't hit. Thank you so much for this! It was driving me nuts!!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved