Results 1 to 12 of 12

Thread: Detect when a widget looses focus?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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 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]

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 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

  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?

    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

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 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

  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?

    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

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 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

  7. #7
    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

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,324
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    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
  •  
Qt is a trademark of The Qt Company.