Focus event when clicking on a focused widget

I have the following behavior.
I am overloading the focusOutEvent() handler for my widget class. But the handler is called not only when the focus of the widget is focused, but also when you click on it. Just like this, it will lose focus and then restore it.

Question one: Is this a mistake?
Question two: If not, is there a reasonable way to handle focus loss? - The slot of all focusChange() signals does not look attractive.

I am using Qt5.1

+4
source share
1 answer

Name your widgets.

 myWidget->setObjectName("MyWidget 1"); myWidget2->setObjectName("MyWidget 2"); 

Then at the top of your focusOutEvent() and your focusInEvent() put some useful but debugging code:

 qDebug() << Q_FUNC_INFO << "called by" << qPrintable(this->objectName()); 

Now you can see what causes your problem.

You can also drop the object tree so that you can see that there are several instances around you.

 QObject::dumpObjectTree(); 

But call it from your QMainWindow or any other window - this is your outermost window.

Hope this helps.

0
source

Source: https://habr.com/ru/post/1483167/


All Articles