[英]QKeyPress - Simulating key press 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";
}
請提出我想念的東西。
問候,薩彥
在鏈接中,您指示輸入了Enter,因此文本不是必需的,但是如果要發送字母,則必須傳遞該參數:
ui->lineEdit->setFocus();
QKeyEvent *key_press = new QKeyEvent(QKeyEvent::KeyPress, Qt::Key_X, Qt::NoModifier, "X");
// text ─────┘
QApplication::sendEvent(ui->lineEdit, key_press);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.