I have an app (not related to any game where the W/A/S/D keys may have special meanings for navigation) where there is a QFrame
. I overrode the keyPressEvent()
to get the text being typed through keyboard while focus in on that QFrame
. This is my code:
void MyFrame::keyPressEvent(QKeyEvent *event)
{
qDebug() << "At least came here" << endl;
QString text = event->text();
qDebug() << "Text: " << text << endl;
}
When I type characters from keyboard one at a time, for all characters and numbers, both statements are logged correctly. But for these four keys neither of the log statements are executed, i.e the event handler is not even firing. What is wrong?
Edit: After going though the examples, I tried to form a minimal working example of my bug. This is what I have got. Same problem here as well with doing it through event filter. Only for those four characters it is not logged.
bool MyWidget::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress)
{
//this never gets printed
qDebug() << "Phew!" << endl;
return true;
}
if (qobject_cast<ChildWidget *>(obj) != nullptr)
{
ChildWidget *option = qobject_cast<ChildWidget *>(obj);
if (event->type() == QEvent::Enter || event->type() == QEvent::MouseMove)
{
//do stuff
return true;
}
if (event->type() == QEvent::Leave)
{
//do stuff
return true;
}
return QWidget::eventFilter(obj, event);
}
else
{
// pass the event on to the parent class
return QWidget::eventFilter(obj, event);
}
}
MyWidget::MyWidget()
{
//do other initialization
this->installEventFilter(this);
}
void MyWidget::keyPressEvent(QKeyEvent *event)
{
qDebug("At least came here");
QString text = event->text();
//this prints out whenever I type any character, excpet W/A/S/D
qDebug() << text;
}