【发布时间】:2018-10-03 04:44:42
【问题描述】:
How can I simulate user interaction (key press event) in Qt?
我尝试了相同的方法,但无法在 lineEdit 小部件上写入
ui->lineEdit->setFocus();
QKeyEvent *key_press = new QKeyEvent(QKeyEvent::KeyPress, Qt::Key_X, Qt::NoModifier);
QApplication::sendEvent(ui->lineEdit, key_press);
交替
QApplication::postEvent(ui->lineEdit, key_press);
也没有成功。
我也尝试了以下方法,但没有得到任何结果。
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";
}
请建议我缺少什么。
问候, 萨彦
【问题讨论】: