View Full Version : eventFilter ,backspace disable

1st July 2013, 09:12
I am trying to avoid backspace from removing a character from
QTextEdit if my QTextEdit has only prompt.
I tried in few ways, but eventFilter is not working as expected.
some times my keys are not even getting dislpayed while typing..
Is there any function to to disable the backspcae when only prompt is available
in QTextEdit?

#define PROMPT "CmdLin>"

bool eventFilter(QObject *Obj, QEvent *event)
static int count;
int promt_len = strlen(PROMPT);

if(event->type() == QEvent::KeyPress){
QKeyEvent *kevent = static_cast<QKeyEvent*>(event);
QString tmp_str= m_pcmdWin->toPlainText();

//if( promt_len== 7 && kevent->key()==Qt::Key_Backspace )
//if( kevent->key()==Qt::Key_Backspace )
if(tmp_str.cmp(PROMPT) && kevent->key()==Qt::Key_Backspace )
qDebug()<<"backspcae pressed";
//return QObject::eventFilter(Obj,event);
return 0;

else if (kevent->key()==Qt::Key_Return||kevent->key()==Qt::Key_Enter ) {
str = m_pcmdWin->toPlainText();
QStringList line = str.split(QRegExp("\n"));
QString str2=line[count];
QByteArray bbyteArray = str2.toUtf8();
const char *st=str2.toUtf8().constData();
str = m_pcmdWin->toPlainText();
}else {
return QObject::eventFilter(Obj,event);

1st July 2013, 09:52
The method seems to miss several returns, it returns 0 in one branch while the result type is bool, etc.

Try to do it step by step. First create the event filter and just pass the event on to the base class.
Then check for the backspace key.
Then check if you want the event to reach the widget and if it shouldn't return true.


15th July 2013, 12:00
if you do 'return 0;' or return false as it should be then the event is unhandled and underneath the 'delete' event will still take place. If you want to prevent the event you have to return true - i.e. it's been handled.