void MyWidget::keyPressEvent(QKeyEvent *event) { if (event->key() == Qt::Key_Return) { // do something when return key is pressed } }
QKeyEvent event(QEvent::KeyPress, Qt::Key_A, Qt::NoModifier); QApplication::sendEvent(this, &event);This code creates a QKeyEvent object for the letter A key with no modifier keys pressed. It then sends this event to the current application's event loop, causing the widget to receive a key press event as if the user had physically pressed the A key. Package library: Qt Overall, QKeyEvent is a useful type for handling and simulating keyboard input in Qt applications.