0

I want to register a event that gets fired every time a new widget or dialog gets created. I don't know if such a signal exists and where to connect it? In the QApplication?

Bluscream
  • 255
  • 1
  • 3
  • 18
  • There is a [`QEvent::Create`](https://code.woboq.org/qt5/qtbase/src/corelib/kernel/qcoreevent.h.html#QEvent::Type::Create) event, but what are you trying to do with this? – thuga Dec 08 '16 at 10:00
  • The application i'm scripting for is creating this annoying popup dialog: https://i.imgur.com/4sE7Zpq.png and i want to .close() it everytime it gets created. – Bluscream Dec 08 '16 at 10:04
  • Well you cannot close it before it is shown. You could listen for a `QEvent::Show` event in an event filter and close it there. Though I'm not sure if this would be the best solution. – thuga Dec 08 '16 at 10:28
  • And how to connect such a QEvent? `QApplication.instance().connect('Show()', self.onShow)`? – Bluscream Dec 08 '16 at 11:07
  • 1
    You can install an [event filter](http://doc.qt.io/qt-5/eventsandfilters.html#event-filters) on your `QApplication` instance. – thuga Dec 08 '16 at 11:27
  • @thuga There being an event doesn't mean much. In the code browser you linked, you can right-click on the event type and see where it's used. The only relevant use is inside of [`QWidgetPrivate::init`](https://code.woboq.org/qt5/qtbase/src/widgets/kernel/qwidget.cpp.html#1226). The event is delivered to the widget itself, not to the application. – Kuba hasn't forgotten Monica Dec 08 '16 at 15:22
  • @KubaOber I'm not sure what you mean. You can install an event filter on the `QApplication` object and intercept these events. – thuga Dec 09 '16 at 07:34
  • 1
    @thuga An event filter on `QApplication`, which is-a `QObject`, will intercept the events being delivered to the application object itself. It won't catch events delivered to other objects, such as widgets. Perhaps what you meant was to *override* the `QCoreApplication::notify`. That will do the job, but has the overhead of being called for events everywhere. In Qt 6 it will change and `notify` will only work for events in the main thread, so there's hope :) – Kuba hasn't forgotten Monica Dec 09 '16 at 13:32
  • @KubaOber No no, it will catch all events. I made a simple test and it caught `QEvent::Create` events as well. The [docs also state that installing an event filter on `QApplication` object will catch *all* events](http://doc.qt.io/qt-5/eventsandfilters.html#event-filters). The downside is like you said, you get some overhead so the event delivery will be slowed down. – thuga Dec 09 '16 at 14:00
  • @thuga Woohoo, I learned something new today. Thank you! Going to refactor a couple projects where I overrode `notify` for that reason. And I've probably read that documentation section more than ten times over the years. Somehow never caught that detail. Thanks again. – Kuba hasn't forgotten Monica Dec 09 '16 at 14:14
  • @KubaOber Haha, you're welcome! Yeah it's much easier to just install an event filter, than to override `notify`. By the way, application event filter only applies to objects that live in the main thread. This is mentioned in the [`notify` docs](http://doc.qt.io/qt-5/qcoreapplication.html#notify). – thuga Dec 09 '16 at 14:28

0 Answers0