PDA

View Full Version : Detect when a widget looses focus?



thomaspu
16th August 2007, 16:01
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

wysota
16th August 2007, 16:15
focusOutEvent maybe?

thomaspu
16th August 2007, 16:18
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

jpn
16th August 2007, 16:32
It's not a signal but an event. Launch up Qt Assistant and type in "focusOutEvent" to Index-tab.

thomaspu
16th August 2007, 16:52
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..

class PaulsQTextEdit: public QTextEdit
{
Q_OBJECT
.....
void focusOutEvent(QFocusEvent *e);

public signals:
void lostFocus();

};

void PaulsQTextEdit::focusOutEvent(QFocusEvent *e)
{
if (e->lostFocus() )
emit(lostFocus() );

QTextEdit::focusOutEvent(e);

}

jpn
16th August 2007, 17:02
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 (http://doc.trolltech.com/4.3/qobject.html#installEventFilter). Installing an event filter doesn't require subclassing but in my humble opinion leads to a bit messier code.

thomaspu
16th August 2007, 17:20
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

jpn
16th August 2007, 17:24
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.

thomaspu
16th August 2007, 17:28
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

jpn
16th August 2007, 22:37
Let the example speak for itself:


// main.cpp
#include <QtGui>

class Window : public QWidget
{
Q_OBJECT

public:
Window(QWidget* parent = 0) : QWidget(parent), focused(0)
{
QVBoxLayout* layout = new QVBoxLayout(this);
QPushButton* button = new QPushButton("Paste", this);
connect(button, SIGNAL(clicked()), this, SLOT(paste()));
layout->addWidget(button);

for (int i = 0; i < 5; ++i)
{
QLineEdit* lineEdit = new QLineEdit(this);
lineEdit->installEventFilter(this); // <--
layout->addWidget(lineEdit);
}
}

bool eventFilter(QObject* object, QEvent* event)
{
if (event->type() == QEvent::FocusIn)
{
focused = qobject_cast<QLineEdit*>(object); // <--
}
return false;
}

private slots:
void paste()
{
if (focused)
{
focused->setText(qApp->clipboard()->text());
}
}

private:
QLineEdit* focused;
};

int main(int argc, char* argv[])
{
QApplication a(argc, argv);
Window w;
w.show();
return a.exec();
}

#include "main.moc"

GrayOwl
5th April 2021, 14:38
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..

class PaulsQTextEdit: public QTextEdit
{
Q_OBJECT
.....
void focusOutEvent(QFocusEvent *e);

public signals:
void lostFocus();

};

void PaulsQTextEdit::focusOutEvent(QFocusEvent *e)
{
if (e->lostFocus() )
emit(lostFocus() );

QTextEdit::focusOutEvent(e);

}


An alternative way is to use:


connect(qApp, &QApplication::focusChanged, [this](QWidget *from, QWidget *to) {
auto w = from;

if ( (w != nullptr) && (w == __yourWidget__) )
// send your signal
} );


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

d_stranz
5th April 2021, 16:58
@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.