Results 1 to 2 of 2

Thread: Forcing a QWidget to lose focus

  1. #1
    Join Date
    May 2010
    Posts
    41
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Forcing a QWidget to lose focus

    Hi,

    I have a class which derives from QSpinBox. In it, I've overriden the following event functions:

    focusInEvent
    focusOutEvent
    keyPressEvent

    The functionality I've implemented makes it so that the value of the spinbox has only actually 'changed' when the user has pressed the enter key or lost focus (i.e. tabbed or clicked away). How I do so doesn't really matter for this question.

    I maintain a variable PrevValue which holds the original value of the spinbox before the user is finished editting it. I'd like to add the use of the ESC key to return the value to the PrevValue as well as lose focus of the spinbox. My code:

    Qt Code:
    1. void MySpinBox::focusInEvent(QFocusEvent *a_pEvent)
    2. {
    3. setStyleSheet(QString::fromUtf8("font: bold;"));
    4. QAbstractSpinBox::focusInEvent(a_pEvent);
    5. }
    6. void MySpinBox::focusOutEvent(QFocusEvent *a_pEvent)
    7. {
    8. setStyleSheet(QString::fromUtf8("font: normal;"));
    9. QAbstractSpinBox::focusOutEvent(a_pEvent);
    10. }
    11. void MySpinBox::keyPressEvent(QKeyEvent *a_pEvent)
    12. {
    13. if( a_pEvent->key() == Qt::Key_Escape )
    14. {
    15. setValue(PrevValue);
    16. this->focusOutEvent(new QFocusEvent(QEvent::FocusOut, Qt::MouseFocusReason));
    17. }
    18. else
    19. QAbstractSpinBox::keyPressEvent(a_pEvent);
    20. }
    To copy to clipboard, switch view to plain text mode 

    As you can see, the focus events simply change the style of the spinbox, nothing more, and the keyPressEvent simply sets the value, and then forces a focus out event.

    This works fine, the only problem is that once it's lost focus this way, nothing else in my form has gained focus. So if I press ESC and then click the spinbox again, the focusInEvent no longer works. I tried using Qt::MouseFocusReason above, as a test, to hopefully make it think it had clicked elsewhere...but of course, there is still no object with focus so it doesn't work.

    Is there a simpler way to do this, or do I need to make some kind of focus dummy?

    Thanks!
    Last edited by Polnareff; 4th November 2010 at 17:02.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Forcing a QWidget to lose focus

    Without really to have read your code, in principal however, if all you want is to lose focus from this widget but retain focus in the application, why not just move the focus to the next tab index widget?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. forcing dialog focus?
    By jajdoo in forum Newbie
    Replies: 1
    Last Post: 3rd September 2010, 20:17
  2. QCompleter causes table to lose focus
    By knack in forum Qt Programming
    Replies: 3
    Last Post: 29th August 2010, 22:22
  3. correct event for lose focus in a QGLWidget window
    By john_god in forum Qt Programming
    Replies: 4
    Last Post: 16th February 2009, 01:34
  4. i want to lose the focus...
    By jrodway in forum Qt Programming
    Replies: 7
    Last Post: 14th July 2007, 10:04
  5. QCanvas automatically lose its focus.
    By Cutey in forum Qt Programming
    Replies: 1
    Last Post: 19th January 2007, 11:29

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.