If you want the button clicked on enter when it has the focus, you can just call setAutoDefault with true. If you want it clicked when pressing enter while it doesn't have the focus, you will have to intercept events for the QMainWindow rather than the button. You can do this by overriding the event function for QMainWindow.
bool MainWindow
::event(QEvent *event
) {
if (event
->type
() == QEvent::KeyPress) {
QKeyEvent *ke
= static_cast<QKeyEvent
*>
(event
);
if (ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return)
{
qDebug() << "click";
ui->pbSave->click();
return true;
}
}
}
bool MainWindow::event(QEvent *event)
{
if (event->type() == QEvent::KeyPress)
{
QKeyEvent *ke = static_cast<QKeyEvent *>(event);
if (ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return)
{
qDebug() << "click";
ui->pbSave->click();
return true;
}
}
return QMainWindow::event(event);
}
To copy to clipboard, switch view to plain text mode
Bookmarks