PDA

View Full Version : How get signal returnPressed in QTextEdit



Roszko
29th December 2009, 21:31
Hello,
I am new user Qt and I have got a small problem. So, I don't know how can I resize QLineEdit (more lines) or how can I catch signal when enter has been pressed in QTextEdit?
I am writing simple chat program and I want to create message box, but QLineEdit is too small field and I don't know how can I catch pressed enter in QTextEdit.
Thanks for help.
Goodbye

faldzip
29th December 2009, 21:41
Hello,
I am new user Qt and I have got a small problem. So, I don't know how can I resize QLineEdit (more lines)
use QTextEdit

or how can I catch signal when enter has been pressed in QTextEdit?
Either subclass from QTextEdit and add you functionality in keyPressEvent() or install eventFilter without subclassing.

Roszko
29th December 2009, 21:47
Can you get me a simple example with keyPressEvent() using?

elcuco
29th December 2009, 21:52
void my_class::keyPressEvent(QKeyEvent *e)
{
QTextCursor c = textCursor();
switch (e->key()) {
case Qt::Key_Escape:
// whatever
break;

case Qt::Key_Enter:
case Qt::Key_Return:
// whatever
break;
}
}

Roszko
29th December 2009, 22:06
ok, but must I send any connect and what signal must be supported?

squidge
29th December 2009, 23:42
No signal, you subclass the control and override the keyPressEvent. If you want a signal, you'll need to emit it from that method.

Xandareva
29th December 2009, 23:47
I have one question. How can I move cursor to begin of QTextEdit? Why do I want to move cursor? Because if I press Enter, Message will send to server and all is ok, but I have one problem, Cursor has been in second line after pressed Enter ;/ and if I send second message (press Enter), message will send with word of new line. I would cursor will be in first line after pressed enter.
Thanks for reply

squidge
29th December 2009, 23:52
Look at moveCursor method.

faldzip
30th December 2009, 08:15
I have one question. How can I move cursor to begin of QTextEdit? Why do I want to move cursor? Because if I press Enter, Message will send to server and all is ok, but I have one problem, Cursor has been in second line after pressed Enter ;/ and if I send second message (press Enter), message will send with word of new line. I would cursor will be in first line after pressed enter.
Thanks for reply
It seems that when you send a message and pass keyPressEvent further (to the default implementation) so it is then inserting new line. You must "eat" the event and not pass it further: accept it (QEvent::accept()) and not pass it to the base class implementation.

Xandareva
30th December 2009, 09:27
I don't know what is wrong here:


bool SimpleChatClient::eventFilter(QObject *obj, QEvent *event)
{
if (obj == message) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
switch(keyEvent->key()) {
case Qt::Key_Enter:
case Qt::Key_Return:
sendMessage();
break;
}
return false;
} else {
return QObject::eventFilter(obj, event);
}
} else {
return QObject::eventFilter(obj, event);
}
}

And I don't know how to does it remake to work properly.

faldzip
30th December 2009, 09:48
I don't know what is wrong here:


bool SimpleChatClient::eventFilter(QObject *obj, QEvent *event)
{
if (obj == message) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
switch(keyEvent->key()) {
case Qt::Key_Enter:
case Qt::Key_Return:
sendMessage();
break;
}
return false;
} else {
return QObject::eventFilter(obj, event);
}
} else {
return QObject::eventFilter(obj, event);
}
}

And I don't know how to does it remake to work properly.
If you want to "eat" the key you must return true.

P.S. This is explained in Qt docs (QObject::eventFilter()):


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.

Xandareva
30th December 2009, 09:54
but now I can't write in this box.

faldzip
30th December 2009, 09:57
return true in line 10. Only in case you call sendMessage(). In other places you can return QObject::eventFilter(obj, event);

Xandareva
30th December 2009, 09:59
thx! It works.