I have this code:
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (event->type() == QEvent::KeyPress)
{
if (keyEvent->key() == Qt::Key_Tab)
// do something
}
}
Now, I am typing in a QTextEdit. So say I hit the tab key. Then a tab will occur in the QTextEdit. But what if I want to prevent that from happening? For an analogy, if you are familiar with emacs: when in the right environment (say c++ mode), you can hit tab, and the line jumps to the right position (indenting). After hitting tab again, nothing happens. This is because the line of code is in the right position.
Anybody here knows how to do this? I guess I can let the tab event to be displayed in QTextEdit, and then delete previous char (or whatever it is defined as).
Using the return value of your event filter function will let you control which events your QTextEdit receives (if you really don't want to just subclass it):
if (keyEvent->key() == Qt::Key_Tab)
return true;
Returning true indicates that the event should be filtered.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With