Results 1 to 12 of 12

Thread: Detect when a widget looses focus?

  1. #1
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Detect when a widget looses focus?

    I'm using qt 4.3.0 and I'm trying to figure out a way to detect when a widget looses focus. I've got two widgets I'm trying to keep an eye on, a QTextEdit and a QLineEdit. Theres a button that I have that when I click on it, I want it to insert some text in either the text edit or line edit based on which one had focus last.

    *edit*
    I found the QLineEdit has a finishedEditing signal that is thrown when it looses focus. Now I'm just stuck on how to detect when QTextEdit looses focus.

    Any ideas?
    Paul
    Last edited by thomaspu; 16th August 2007 at 15:10.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Detect when a widget looses focus?

    focusOutEvent maybe?

  3. #3
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Detect when a widget looses focus?

    hmm... I didn't see any useful signals in the QTextEdit class for detecting when focus is lost. Didn't see anything like focusOutEvent either ;/

    Paul

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Detect when a widget looses focus?

    It's not a signal but an event. Launch up Qt Assistant and type in "focusOutEvent" to Index-tab.
    J-P Nurmi

  5. #5
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Detect when a widget looses focus?

    This might be a stupid question, but I'll ask it anyway. I need to inherit the QTextEdit and reimpliment the focusOutEvent to be able to be able to send a signal that it lost focus right? Or is there a better way to do it?

    Something like..
    Qt Code:
    1. class PaulsQTextEdit: public QTextEdit
    2. {
    3. Q_OBJECT
    4. .....
    5. void focusOutEvent(QFocusEvent *e);
    6.  
    7. public signals:
    8. void lostFocus();
    9.  
    10. };
    11.  
    12. void PaulsQTextEdit::focusOutEvent(QFocusEvent *e)
    13. {
    14. if (e->lostFocus() )
    15. emit(lostFocus() );
    16.  
    17. QTextEdit::focusOutEvent(e);
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 16th August 2007 at 23:12. Reason: changed [qtclass] to [code]

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Detect when a widget looses focus?

    Yes, something like that. You can drop the "public" keyword in front of signals declaration, though.

    By the way, there is another way to catch events too, namely event filter. Installing an event filter doesn't require subclassing but in my humble opinion leads to a bit messier code.
    J-P Nurmi

  7. #7
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Detect when a widget looses focus?

    I really have no idea how to implement that as an event filter.

    Wait, if the event filter that I implement sends out, say, a lostFocus signal and that event filter is installed on the QTextEdit, does the QTextEdit essentially now have a lostFocus signal that I can connect too?

    Paul

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Detect when a widget looses focus?

    Perhaps you wouldn't need a signal at all if you used an event filter. The object who must know when a QTextEdit loses focus could be the filtering object itself.
    J-P Nurmi

  9. #9
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Detect when a widget looses focus?

    I'm not sure I follow you. Like in my scenario I have two widgets, textEdit and LineEdit and a button that inserts text. I want the button click to insert text in the widget that had focus lost. So are you saying that the button could some way filter the textEdit's loosing focus event? Or am I looking at this wrong?

    Paul

  10. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Detect when a widget looses focus?

    Let the example speak for itself:
    Qt Code:
    1. // main.cpp
    2. #include <QtGui>
    3.  
    4. class Window : public QWidget
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. Window(QWidget* parent = 0) : QWidget(parent), focused(0)
    10. {
    11. QVBoxLayout* layout = new QVBoxLayout(this);
    12. QPushButton* button = new QPushButton("Paste", this);
    13. connect(button, SIGNAL(clicked()), this, SLOT(paste()));
    14. layout->addWidget(button);
    15.  
    16. for (int i = 0; i < 5; ++i)
    17. {
    18. QLineEdit* lineEdit = new QLineEdit(this);
    19. lineEdit->installEventFilter(this); // <--
    20. layout->addWidget(lineEdit);
    21. }
    22. }
    23.  
    24. bool eventFilter(QObject* object, QEvent* event)
    25. {
    26. if (event->type() == QEvent::FocusIn)
    27. {
    28. focused = qobject_cast<QLineEdit*>(object); // <--
    29. }
    30. return false;
    31. }
    32.  
    33. private slots:
    34. void paste()
    35. {
    36. if (focused)
    37. {
    38. focused->setText(qApp->clipboard()->text());
    39. }
    40. }
    41.  
    42. private:
    43. QLineEdit* focused;
    44. };
    45.  
    46. int main(int argc, char* argv[])
    47. {
    48. QApplication a(argc, argv);
    49. Window w;
    50. w.show();
    51. return a.exec();
    52. }
    53.  
    54. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  11. #11
    Join Date
    Mar 2021
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Detect when a widget looses focus?

    Quote Originally Posted by thomaspu View Post
    This might be a stupid question, but I'll ask it anyway. I need to inherit the QTextEdit and reimpliment the focusOutEvent to be able to be able to send a signal that it lost focus right? Or is there a better way to do it?

    Something like..
    Qt Code:
    1. class PaulsQTextEdit: public QTextEdit
    2. {
    3. Q_OBJECT
    4. .....
    5. void focusOutEvent(QFocusEvent *e);
    6.  
    7. public signals:
    8. void lostFocus();
    9.  
    10. };
    11.  
    12. void PaulsQTextEdit::focusOutEvent(QFocusEvent *e)
    13. {
    14. if (e->lostFocus() )
    15. emit(lostFocus() );
    16.  
    17. QTextEdit::focusOutEvent(e);
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 
    An alternative way is to use:
    Qt Code:
    1. connect(qApp, &QApplication::focusChanged, [this](QWidget *from, QWidget *to) {
    2. auto w = from;
    3.  
    4. if ( (w != nullptr) && (w == [B]__yourWidget__[/B]) )
    5. // send your signal
    6. } );
    To copy to clipboard, switch view to plain text mode 

    Doc: https://doc.qt.io/qt-5/qapplication.html#focusChanged

  12. #12
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,884
    Thanks
    274
    Thanked 794 Times in 784 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Detect when a widget looses focus?

    @GrayOwl - you do realize that this thread is almost 14 years old, right? Please don't pull old threads out of their graves if all you are adding is a link to the documentation.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. How to get focus event in child widget
    By YuriyRusinov in forum Qt Programming
    Replies: 4
    Last Post: 17th October 2006, 07:53

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.