PDA

View Full Version : QWidget::eventFilter() not catching key combinations



derrickbj
12th November 2013, 22:10
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:



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,
--Derrick

derrickbj
20th November 2013, 22:24
***bump***

derrickbj
23rd November 2013, 04:54
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
if (event->type() == QEvent::KeyPress || event->type() == QEvent::ShortcutOverride) Fixed the issue. I'm not exactly sure how, but taking out the QEvent::ShortcutOverride check disables the Ctrl+C from being captured.