PDA

View Full Version : EventFilter problem



aruval3
17th November 2011, 01:39
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;
}

Oleg
17th November 2011, 06:34
Just check your QEvent::type() first, as by pressing single key you get QEvent::KeyPress, QEvent::KeyRelease and QEvent::ShortcutOverride.

aruval3
17th November 2011, 07:15
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?

Lykurg
17th November 2011, 07:18
Yepp, just check for it: QKeyEvent::modifiers().

aruval3
17th November 2011, 16:37
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?

Oleg
17th November 2011, 17:04
What about line endings, do you send "\n" with your text?

aruval3
17th November 2011, 17:14
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

Oleg
17th November 2011, 17:30
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()'?

aruval3
17th November 2011, 17:44
Return false if it is the enter key that is pressed? Or otherwise?

Oleg
17th November 2011, 17:46
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.

aruval3
17th November 2011, 21:22
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