Results 1 to 3 of 3

Thread: eventFilter ,backspace disable

  1. #1
    Join Date
    Mar 2013
    Location
    Hyderabad,Bangalore,India
    Posts
    70
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default eventFilter ,backspace disable

    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?



    Qt Code:
    1. #define PROMPT "CmdLin>"
    2.  
    3. bool eventFilter(QObject *Obj, QEvent *event)
    4. {
    5. static int count;
    6. int promt_len = strlen(PROMPT);
    7.  
    8. if(event->type() == QEvent::KeyPress){
    9. QKeyEvent *kevent = static_cast<QKeyEvent*>(event);
    10. QString tmp_str= m_pcmdWin->toPlainText();
    11.  
    12. qDebug()<<"len"<<m_pcmdWin->toPlainText().length();
    13. //if( promt_len== 7 && kevent->key()==Qt::Key_Backspace )
    14. //if( kevent->key()==Qt::Key_Backspace )
    15. if(tmp_str.cmp(PROMPT) && kevent->key()==Qt::Key_Backspace )
    16. {
    17. qDebug()<<"backspcae pressed";
    18. //return QObject::eventFilter(Obj,event);
    19. return 0;
    20. }
    21.  
    22. else if (kevent->key()==Qt::Key_Return||kevent->key()==Qt::Key_Enter ) {
    23. str = m_pcmdWin->toPlainText();
    24. QStringList line = str.split(QRegExp("\n"));
    25. QString str2=line[count];
    26. count++;
    27. QByteArray bbyteArray = str2.toUtf8();
    28. const char *st=str2.toUtf8().constData();
    29. read(st);
    30. }
    31. else{
    32. kevent->accept();
    33. str = m_pcmdWin->toPlainText();
    34. }
    35. }else {
    36. return QObject::eventFilter(Obj,event);
    37. }
    38. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: eventFilter ,backspace disable

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

  3. #3
    Join Date
    Aug 2012
    Location
    Loughborough, UK
    Posts
    29
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: eventFilter ,backspace disable

    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.

Similar Threads

  1. QLineEdit: textEdited called on backspace
    By akiross in forum Qt Programming
    Replies: 2
    Last Post: 14th August 2011, 22:47
  2. passing backspace key to Webkit plugin
    By shayz in forum Newbie
    Replies: 0
    Last Post: 15th October 2010, 09:36
  3. QTmobility why Backspace dont run on QTextedit
    By nhs_0702 in forum Qt Programming
    Replies: 1
    Last Post: 3rd June 2010, 02:30
  4. lineEdit->backspace function problem
    By aj2903 in forum Qt Programming
    Replies: 1
    Last Post: 5th June 2009, 08:02
  5. Replies: 2
    Last Post: 8th October 2008, 14:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.