PDA

View Full Version : QKeyPress - Simulating key press



sayan275
3rd October 2018, 06:48
https://www.qtcentre.org/threads/15325-how-to-Simulate-the-keyboard-press


I tried the same approach, but not able to get written on the lineEdit widget


ui->lineEdit->setFocus();
QKeyEvent *key_press = new QKeyEvent(QKeyEvent::KeyPress, Qt::Key_X, Qt::NoModifier);
QApplication::sendEvent(ui->lineEdit, key_press);

Alternately


QApplication::postEvent(ui->lineEdit, key_press);
also didn't succeed.

I tried the below also and didn't get any result.


QKeyEvent key(QEvent::KeyPress, Qt::Key_X, Qt::NoModifier);
QApplication::sendEvent(ui->lineEdit, &key);
if (key.isAccepted()) {
qDebug()<<"everything is ok";
} else {
qDebug()<<"something wrong";
}
Please suggest what am I missing.

Regards,
Sayan

sayan275
3rd October 2018, 10:39
Hi,
got it done.


ui->lineEdit->setFocus();
QKeyEvent *key_press = new QKeyEvent(QKeyEvent::KeyPress, Qt::Key_X, Qt::NoModifier, "x");
QApplication::sendEvent(ui->lineEdit, key_press);

the last parameter ..