Results 1 to 4 of 4

Thread: Focus rect not drawing

  1. #1
    Join Date
    Aug 2008
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Focus rect not drawing

    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!

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Focus rect not drawing

    Have you subclassed (ie modified) those widgets?
    What events are you filtering?

    Please give us some code.

  3. #3
    Join Date
    Aug 2008
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Focus rect not drawing

    None of the controls are subclassed. Here is the code of my event filter. Sorry for the excessive length.

    Qt Code:
    1. bool AccessCodePane::eventFilter(QObject* target, QEvent* event)
    2. {
    3. if (target == ui.accessCodeEdit)
    4. {
    5. if (event->type() == QEvent::KeyPress)
    6. {
    7. // From Access Code dialog: left key access as a backspace
    8. QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
    9.  
    10. if (keyEvent->key() == Qt::Key_Left)
    11. {
    12. // Left arrow acts as a backspace key
    13. ui.accessCodeEdit->backspace();
    14. return true; // Event stops here
    15. }
    16. else if (keyEvent->key() == Qt::Key_Down || keyEvent->key() == Qt::Key_Up)
    17. {
    18. // If user hits the up or down button, set focus to the OK button
    19. ui.okButton->setFocus();
    20. return true; // Event stops here
    21. }
    22. }
    23. }
    24. else if (target == ui.exitButton)
    25. {
    26. if (event->type() == QEvent::KeyPress)
    27. {
    28. QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
    29.  
    30. if (keyEvent->key() == Qt::Key_Down || keyEvent->key() == Qt::Key_Up)
    31. {
    32. // If user hits the up or down button, set focus to the access code line edit control
    33. ui.accessCodeEdit->setFocus();
    34. return true; // Event stops here
    35. }
    36. else if (keyEvent->key() == Qt::Key_Left || keyEvent->key() == Qt::Key_Right)
    37. {
    38. // Left/right button toggle between OK and Cancel
    39. ui.okButton->setFocus();
    40. return true;
    41. }
    42. else if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter)
    43. {
    44. ui.exitButton->click();
    45. return true;
    46. }
    47. }
    48. }
    49. else if (target == ui.okButton)
    50. {
    51. if (event->type() == QEvent::KeyPress)
    52. {
    53. QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
    54.  
    55. if (keyEvent->key() == Qt::Key_Down || keyEvent->key() == Qt::Key_Up)
    56. {
    57. // If user hits the up or down button, set focus to the access code line edit control
    58. ui.accessCodeEdit->setFocus();
    59. return true; // Event stops here
    60. }
    61. else if (keyEvent->key() == Qt::Key_Left || keyEvent->key() == Qt::Key_Right)
    62. {
    63. // Left/right button toggle between OK and Cancel
    64. ui.exitButton->setFocus();
    65. return true;
    66. }
    67. else if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter)
    68. {
    69. ui.okButton->click(); // Clicking OK is same as pressing Return in edit control
    70. return true;
    71. }
    72. }
    73. }
    74.  
    75. return false; // Pass event up the chain
    76. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Aug 2008
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Focus rect not drawing

    I just figured it out.

    Using:
    Qt Code:
    1. setFocus(Qt::TabFocusReason);
    To copy to clipboard, switch view to plain text mode 
    instead of just
    Qt Code:
    1. setFocus();
    To copy to clipboard, switch view to plain text mode 
    fixed the problem.

Similar Threads

  1. Problems with QString
    By cyberboy in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2008, 08:18
  2. Widget Focus
    By navi1084 in forum Qt Programming
    Replies: 6
    Last Post: 29th September 2008, 10:22
  3. Force focus to a QTabWidget page's widget
    By thomaspu in forum Qt Programming
    Replies: 1
    Last Post: 2nd January 2008, 06:54
  4. Replies: 3
    Last Post: 7th November 2006, 08:35
  5. Tab/Enter focus problem
    By b1 in forum Qt Programming
    Replies: 4
    Last Post: 23rd October 2006, 23:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.