Solved Can't catch Enter (Return)
-
Hello,
I have tried the following code to qDebug something when I click the Enter Button, but nothing happens:#include <QKeyEvent> bool MainWindow::eventFilter(QObject* obj, QEvent* event) { if (event->type()==QEvent::KeyPress) { QKeyEvent* key = static_cast<QKeyEvent*>(event); if ( (key->key()==Qt::Key_Enter) || (key->key()==Qt::Key_Return) ) { qDebug()<< "pressed"; } else { return QObject::eventFilter(obj, event); } return true; } else { return QObject::eventFilter(obj, event); } return false; } private slots: bool eventFilter(QObject* obj, QEvent* event);
-
@Chaki
Where is the focus when you click the Enter key? Is it on a widget, or do you have a blank main window? -
@JonB I am sorry, but I did not quite understand what you mean.
Coul you explain it please? -
@Chaki said in Can't catch Enter (Return):
when I click the Enter Button
Do you mean: when I press the enter key?
Or are you using virtual keyboard? -
@jsulm i mean the enter key
-
I have solved it:
connect(ui->line_Edit, SIGNAL(returnPressed()),ui->btn_Button,SIGNAL(clicked()));
-
@Chaki
Which is what I suggested: you are pressing the Enter key on aQLineEdit
, but you did not tell us that, it helps if you do! :) That line edit has the focus, so the key press will not get sent to the main window.... -
@JonB Yes, you're right sir. Thanks you your reply