Results 1 to 11 of 11

Thread: EventFilter problem

  1. #1
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default EventFilter problem

    To sum up I'm working on a chat application...

    In my chat window I installed a filter to my QTextEdit box where a user type's their input. below is the code.
    The below code is detecting the event correctly but it is doing qDebug()<<"Here!" 3 times when I only pressed the enter button once. What could be the problem? If so can you provide a potential fix.

    Thanks

    bool chatWindow::eventFilter(QObject *ob, QEvent *e)
    {

    QKeyEvent *keyEvent = static_cast<QKeyEvent*>(e);

    if(ob == ui->inputWindow)
    {
    if(keyEvent->key() == Qt::Key_Enter )
    {
    qDebug()<< "Here!";
    return true;
    }

    return false;
    }
    return false;
    }

  2. #2
    Join Date
    Oct 2007
    Location
    Lake Forest, CA, USA
    Posts
    132
    Thanks
    10
    Thanked 27 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: EventFilter problem

    Just check your QEvent::type() first, as by pressing single key you get QEvent::KeyPress, QEvent::KeyRelease and QEvent::ShortcutOverride.
    Oleg Shparber

  3. The following user says thank you to Oleg for this useful post:

    aruval3 (17th November 2011)

  4. #3
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: EventFilter problem

    Dope... did not think of that... thanks! And also how would I add the part where I hold shift and enter? Seems like I add another if statement to check for modifiers?

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: EventFilter problem

    Yepp, just check for it: QKeyEvent::modifiers().

  6. #5
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: EventFilter problem

    So I got to detect Shift + enter thanks...Now I'm having a different problem... after pressing enter(and only enter).. the chat message is sent and outputted to the chat display but the cursor does not move back to first line.. after typing a message after the first one... first one moves back to position fine.. but anything beyond that makes the text cursor start at the second line of the inputWindow QTextEdit


    ui->inputWindow->clear();
    QTextCursor temp = ui->inputWindow->textCursor();
    temp.movePosition(QTextCursor::Up,QTextCursor::Mov eAnchor);
    ui->inputWindow->setTextCursor(temp);

    Seems like movePosition is returning false.. any reason/fix?

  7. #6
    Join Date
    Oct 2007
    Location
    Lake Forest, CA, USA
    Posts
    132
    Thanks
    10
    Thanked 27 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: EventFilter problem

    What about line endings, do you send "\n" with your text?
    Oleg Shparber

  8. #7
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: EventFilter problem

    Not that I know of unless when appending to outputWindow/chat display, the append function add a new line character.. but I don't understand how this would affect the inputWindow

  9. #8
    Join Date
    Oct 2007
    Location
    Lake Forest, CA, USA
    Posts
    132
    Thanks
    10
    Thanked 27 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: EventFilter problem

    Sorry, I've misread your previous message. If I understand correctly, then the problem is that handling Enter key prevents your inputWindow from getting CR/LF inside it. Do you return 'false' from 'eventFilter()'?
    Oleg Shparber

  10. #9
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: EventFilter problem

    Return false if it is the enter key that is pressed? Or otherwise?

  11. #10
    Join Date
    Oct 2007
    Location
    Lake Forest, CA, USA
    Posts
    132
    Thanks
    10
    Thanked 27 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: EventFilter problem

    Here's quote from documentation:
    In your reimplementation of this function, if you want to filter the event out, i.e. stop it being handled further, return true; otherwise return false.
    Oleg Shparber

  12. #11
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: EventFilter problem

    Oh boy... I was returning no boolean values... I somehow erased the return true part... now it works... thank's for all the help Oleg and co. Problem solved

Similar Threads

  1. Yet another eventFilter question
    By ls4f in forum Qt Programming
    Replies: 8
    Last Post: 14th September 2011, 14:50
  2. Autorepeat within eventfilter
    By Patrick_Bao in forum Qt Programming
    Replies: 0
    Last Post: 1st December 2010, 08:37
  3. QTreeWidget and eventFilter
    By luoyes in forum Qt Programming
    Replies: 2
    Last Post: 5th November 2009, 07:38
  4. problem in eventFilter of QGraphicsView()
    By wagmare in forum Qt Programming
    Replies: 7
    Last Post: 20th July 2009, 13:28
  5. eventFilter in Console App
    By KaptainKarl in forum Newbie
    Replies: 3
    Last Post: 20th December 2008, 01:27

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.