How to control QT signals?

During debugging, I want to see what my program event loop is waiting for. It is probably flooded, and I want to see by what signals, without (manually) adding a specific log message to each Q_EMIT.

Possible solutions can observe some internal qt-data structure that contains the event queue (is there such a thing? How?)

Or -

Write a log message for each emitted signal (is this possible?).

Any other ideas?

(QT 4.8 on Windows using visual studio 2012)

+5
source share
3 answers

Signals and events are two things that have nothing to do with each other.

I want to see what awaits my program event loop. It is probably flooded.

First of all, let me get the nomenclature directly:

  • an event queue where events are saved until delivery;
  • an event loop is what drains the event queue and delivers events to QObjects ,
  • A flood of events occurs when, on average, several events are added to the queue when each event is delivered.

There are two reasons only why the event queue can be flooded:

  • It takes too much time to process certain events (for example, when blocking code): the speed of the queue stack is lower than the filling speed due to time.
  • You add more than one event to each event sent (on average): the speed of filling the queue is higher than the drain rate due to the multiplication of the event - this is completely unrelated to any time. A suitable name for him would be a storm of events.

To find code in blocks for too long, you can use the tool that I wrote for another answer .

To find out how many events a thread is qGlobalPostedEventsCount() , use the undocumented qGlobalPostedEventsCount() . You add this to the code of the tool associated with it.

+1
source

Not sure if this is enough for you, but you can try setting event filters between QObjects that implement eventFilter () as follows:

 class MyWidget : public QWidget { QGraphicsView myView; MyWidget() { myView->installEventFilter(this); // incoming events to myView are shown on the standard output } }; 

You can get more creative while reading documents.

0
source

The Qt documentation for Events and Filters states:

You can also filter all events for the entire application by setting an event filter in a QApplication or QCoreApplication object. Such global event filters are invoked before object-specific filters. This is very powerful, but also slows down the delivery of events for each individual event throughout the application.

Therefore, you can create an event filter in QApplication or QCoreApplication and control all events by checking their type .

As an alternative, QCoreApplication uses the virtual notify function to deliver events to objects. Overriding QCoreApplication will allow you to see both the event and the QObject with which the event will initially be sent.

* Note that events are propagated to parent objects if the receiving object ignores the event.

If you decide to use notify , be aware of the future direction for this function:

Direction to the future: this function will not be called for objects that live outside the main thread in Qt 6. Applications that need this function must find other solutions for their event verification needs. This change can be expanded to the main thread, as a result of which this function will become obsolete.

0
source

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


All Articles