PDA

View Full Version : eventFilter ,backspace disable



PstdEr
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();

qDebug()<<"len"<<m_pcmdWin->toPlainText().length();
//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];
count++;
QByteArray bbyteArray = str2.toUtf8();
const char *st=str2.toUtf8().constData();
read(st);
}
else{
kevent->accept();
str = m_pcmdWin->toPlainText();
}
}else {
return QObject::eventFilter(Obj,event);
}
}

anda_skoa
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.

Cheers,
_

rockdemon
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.