None of the controls are subclassed. Here is the code of my event filter. Sorry for the excessive length.
{
if (target == ui.accessCodeEdit)
{
if (event
->type
() == QEvent::KeyPress) {
// From Access Code dialog: left key access as a backspace
QKeyEvent *keyEvent
= static_cast<QKeyEvent
*>
(event
);
if (keyEvent->key() == Qt::Key_Left)
{
// Left arrow acts as a backspace key
ui.accessCodeEdit->backspace();
return true; // Event stops here
}
else if (keyEvent->key() == Qt::Key_Down || keyEvent->key() == Qt::Key_Up)
{
// If user hits the up or down button, set focus to the OK button
ui.okButton->setFocus();
return true; // Event stops here
}
}
}
else if (target == ui.exitButton)
{
if (event
->type
() == QEvent::KeyPress) {
QKeyEvent *keyEvent
= static_cast<QKeyEvent
*>
(event
);
if (keyEvent->key() == Qt::Key_Down || keyEvent->key() == Qt::Key_Up)
{
// If user hits the up or down button, set focus to the access code line edit control
ui.accessCodeEdit->setFocus();
return true; // Event stops here
}
else if (keyEvent->key() == Qt::Key_Left || keyEvent->key() == Qt::Key_Right)
{
// Left/right button toggle between OK and Cancel
ui.okButton->setFocus();
return true;
}
else if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter)
{
ui.exitButton->click();
return true;
}
}
}
else if (target == ui.okButton)
{
if (event
->type
() == QEvent::KeyPress) {
QKeyEvent *keyEvent
= static_cast<QKeyEvent
*>
(event
);
if (keyEvent->key() == Qt::Key_Down || keyEvent->key() == Qt::Key_Up)
{
// If user hits the up or down button, set focus to the access code line edit control
ui.accessCodeEdit->setFocus();
return true; // Event stops here
}
else if (keyEvent->key() == Qt::Key_Left || keyEvent->key() == Qt::Key_Right)
{
// Left/right button toggle between OK and Cancel
ui.exitButton->setFocus();
return true;
}
else if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter)
{
ui.okButton->click(); // Clicking OK is same as pressing Return in edit control
return true;
}
}
}
return false; // Pass event up the chain
}
bool AccessCodePane::eventFilter(QObject* target, QEvent* event)
{
if (target == ui.accessCodeEdit)
{
if (event->type() == QEvent::KeyPress)
{
// From Access Code dialog: left key access as a backspace
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Left)
{
// Left arrow acts as a backspace key
ui.accessCodeEdit->backspace();
return true; // Event stops here
}
else if (keyEvent->key() == Qt::Key_Down || keyEvent->key() == Qt::Key_Up)
{
// If user hits the up or down button, set focus to the OK button
ui.okButton->setFocus();
return true; // Event stops here
}
}
}
else if (target == ui.exitButton)
{
if (event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Down || keyEvent->key() == Qt::Key_Up)
{
// If user hits the up or down button, set focus to the access code line edit control
ui.accessCodeEdit->setFocus();
return true; // Event stops here
}
else if (keyEvent->key() == Qt::Key_Left || keyEvent->key() == Qt::Key_Right)
{
// Left/right button toggle between OK and Cancel
ui.okButton->setFocus();
return true;
}
else if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter)
{
ui.exitButton->click();
return true;
}
}
}
else if (target == ui.okButton)
{
if (event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Down || keyEvent->key() == Qt::Key_Up)
{
// If user hits the up or down button, set focus to the access code line edit control
ui.accessCodeEdit->setFocus();
return true; // Event stops here
}
else if (keyEvent->key() == Qt::Key_Left || keyEvent->key() == Qt::Key_Right)
{
// Left/right button toggle between OK and Cancel
ui.exitButton->setFocus();
return true;
}
else if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter)
{
ui.okButton->click(); // Clicking OK is same as pressing Return in edit control
return true;
}
}
}
return false; // Pass event up the chain
}
To copy to clipboard, switch view to plain text mode
Bookmarks