PDA

View Full Version : Focus rect not drawing



draftpunk
11th November 2008, 01:32
Hello,

I'm trying to change focus from a QLineEdit to a QPushButton, using ui.myButton->setFocus() inside an event filter, but when I do so, the button's focus rectangle does not draw. I know that the button has the focus, because if I hit Return, the program acts as if the button was clicked.

If I use the tab key to change focus, then the button's focus rect is drawn correctly.

So, why doesn't the focus rect draw if I use setFocus()? (I tried using releaseKeyboard() from the QLineEdit, but that didn't help.)

Any help would be greatly appreciated.

Thanks!

caduel
11th November 2008, 08:06
Have you subclassed (ie modified) those widgets?
What events are you filtering?

Please give us some code.

draftpunk
11th November 2008, 15:40
None of the controls are subclassed. Here is the code of my event filter. Sorry for the excessive length.


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
}

draftpunk
11th November 2008, 16:21
I just figured it out.

Using:
setFocus(Qt::TabFocusReason); instead of just
setFocus(); fixed the problem.