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
Re: Detect when a widget looses focus?
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
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.
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..
Code:
{
Q_OBJECT
.....
public signals:
void lostFocus();
};
{
if (e->lostFocus() )
emit(lostFocus() );
}
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.
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
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.
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
Re: Detect when a widget looses focus?
Let the example speak for itself:
Code:
// main.cpp
#include <QtGui>
{
Q_OBJECT
public:
{
connect(button, SIGNAL(clicked()), this, SLOT(paste()));
layout->addWidget(button);
for (int i = 0; i < 5; ++i)
{
lineEdit->installEventFilter(this); // <--
layout->addWidget(lineEdit);
}
}
{
if (event
->type
() == QEvent::FocusIn) {
focused = qobject_cast<QLineEdit*>(object); // <--
}
return false;
}
private slots:
void paste()
{
if (focused)
{
focused->setText(qApp->clipboard()->text());
}
}
private:
};
int main(int argc, char* argv[])
{
Window w;
w.show();
return a.exec();
}
#include "main.moc"
Re: Detect when a widget looses focus?
Quote:
Originally Posted by
thomaspu
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..
Code:
{
Q_OBJECT
.....
public signals:
void lostFocus();
};
{
if (e->lostFocus() )
emit(lostFocus() );
}
An alternative way is to use:
Code:
connect(qApp,
&QApplication
::focusChanged,
[this](QWidget *from,
QWidget *to
) { auto w = from;
if ( (w != nullptr) && (w == [B]__yourWidget__[/B]) )
// send your signal
} );
Doc: https://doc.qt.io/qt-5/qapplication.html#focusChanged
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.