QEventLoop: get time when an event was scheduled - qt

Is it possible to get the time when an event has been scheduled to a QEventLoop (e.g. the QCoreApplication event loop)?
I have a situation where the main event loop is paused. When it's reactivated the events are fired and I am interested in the time when the events where added to the queue. The events are not custom events but system (and other) events.
Regards,

It mainly depends on what are the system events you are interested in, for you have already the timestamp in some cases.
As an example, QInputEvent (base class for events that describe user input, like QMouseEvent, QKeyEvent, and so on) has the member method timestamp that:
Returns the window system's timestamp for this event.
In other terms, a timestamp close to the time when it has been pushed into the event loop.

Related

Axon: Deadline vs EventScheduler

Hello I'm struggling to come up with the distinction between the DeadlineManager implementation of scheduling future events and EventScheduler and what is the best use case for either.
Say I need to schedule a task to be performed in 24 hours based on a command that happened today. Between now and then another event or command could occur that makes the scheduled event obsolete so now I need to cancel the scheduled event.
Can I use either interchangeably? If not - in this scenario what is the best choice or is there not enough information? What would inform my decision to use one over the other?
The main difference between scheduling an event or a deadline is what you want to happen when your scheduled time has passed.
When you schedule an event , that event will always be added to the eventstore after the scheduled time has passed.
When you schedule a deadline, no event will be added directly, but instead a DeadlineHandler annotated function will be called, in which you can then decide based on the current state of the aggregate or saga, what you want to do (if anything). So unless you apply an event yourself in the deadline handler, there will be no interaction with the eventstore.
Note that both can also be cancelled before the scheduled time has ended, using the ScheduleToken returned when scheduling an event, or using the deadineId and its name in the case of a deadline.
Some further information can be found in the reference guide:
https://docs.axoniq.io/reference-guide/configuring-infrastructure-components/deadlines
https://docs.axoniq.io/reference-guide/implementing-domain-logic/complex-business-transactions/deadline-handling

Running a Timer on Server

I have to code a timer mechanisam.
The idea is to have it countdown in sec on all clients screen & give the clients an option to increase the countdown period. The timer being common , I assume we need to run it on server.
A few ideas with sample code on Meteor Publish would be great.
Detect a future time ad get the timestamp
Save that timestamp on a collection
Create a simple timer; at each second, get he difference between the current time and the future date and display it (properly converted)
Create an event that increase the stored future timestamp by N milliseconds; the collection update should be propagated across all clients

How do events work in Qt?

I have some questions about the general usage of Qt events. I'm new to Qt, and I am going to try out making a custom event. The questions I ask are related to this article: Qt 4.8: The Event System
When does an event “occur”? In Qt's built in events, mouse clicks and key presses are put into a queue and then the functions are executed at the next frame. I’m especially wondering about this for custom events, because I’m not sure if it always occurs when a mouse is clicked. For custom events, is it when you send the event into the queue and then waits to be processed by the event() function at the next frame?
To create a custom event, must you make a class that inherits from QEvent and register an event with registerEventType() function? Is this the standard process, or can an event simply be a class? How does this function, registerEventType(), work?
Do custom events have a QEvent::Type? Is this the number between 1000 and 6563 that is given when using the registerEventType() function?
What is the recipient of an event? It seems to be in functions' parameters like postEvent(), sendEvent(), etc. However, I am not sure what this object is for.
Where is the actual code that is executed when an event is fired?
Is it a function, or is it in the event() function of QObjects.
Also any working examples of Qt events (both built-in or custom) in action would be helpful.
You should look at Qt Examples online or in QtSDK, there are tons of them.
IMO you didn't search hard.
Ad.1. It occurs after you fire it with QApplication::postEvent() or QApplication::sendEvent(). Not immediately of course, because it has to go through the main loop, etc. The order of events should be preserved, though.
Ad.2. Look at this, second anwser.
Ad.3. Go to Ad.2.
Ad.4. This object will receive this custom event in QObject::customEvent() handler.
Ad.5. Go to Ad.4.

debugging Flex event flow

I'm stepping through my code to figure out why a certain function takes more time to run the first time it gets called than on successive calls. The code flow for each function call is the same up to when a dispatchEvent gets called. I'm pretty sure it's different afterwards, as that call takes a lot more time the first time around. Unfortunately, I have no idea which other parts of the code chew on this specific event and thus cannot step through the handling of such event.
The question: is there a way to either figure out who handles such events or magically step through the handling code without explicitly setting breakpoints there?
thank you!
Not in an easy fashion, no. It's a pro and con of Flash (or any event based framework). You don't know when it's fired, where it's fired from (think bubbling), or who is listening for it. But at the same time, anyone could listen for any event, from anywhere (so long as it's within the display tree).
Normally what I do is just do a workspace search (ctrl+H in Flash Builder) and search for that specific event (you should be using static constants for dispatching/listening event types) and see who's doing what.

Preventing timers from running when something is already busy (using Qt)

In my mathematical application I am using timers to regularly perform certain actions. These actions can also be configured by my users. Now I don't want these actions to be executed if there is already another action busy.
E.g. if the user just started a complex calculation by selecting a menu entry, I don't want to execute the actions behind my timers.
Problem is that the user can execute an action via a lot of different ways (via the menu, by clicking somewhere, via popup menu, via drag-and-drop, ...). What I effectively want is to prevent the timers from going off if the application is currently not in the main event loop.
I will give a more concrete example to make it clearer:
At startup I create the timers
If a timer goes off, I execute some actions which, in practice, could access almost every bit in may application's data structure.
Now suppose the user starts a mathematical algorithm (via the menu, by clicking or by dragging elements on the screen, it doesn't matter how he started it).
The algorithm will perform lots of calculations (in the main thread). Since they are executed in the main thread, the timer events will not go off.
Now the algorithm shows a message box (could be a warning or a question).
While the message box is open, events are processed again, including my timer events, which could possibly perform incorrect calculations because there is already another algorithm running.
Reworking my application so that I move logic to a separate worker thread, or adding checks to all of my actions isn't possible at this moment. So please don't suggest to completely rework my application.
What I tried so far is the following:
Using postEvent to send an event, hoping that this event would only be executed in the main event loop. Unfortunately, also the message box's event loop seems to process posted events.
Using the QEvent::WindowBlocked and QEvent::WindowUnblocked events to see when a modal dialog was opened. In my timer-event-logic I can check whether we are between QEvent::WindowBlocked-QEvent::WindowUnblocked calls or not. Unfortunately, these events only work for modal dialogs created by Qt itself, not for other dialogs (e.g. the Windows MessageBox, or the system's printer configuration dialog). Also, this trick would not help if there would be other event loops created by sub routines.
What I actually need to solve my problem is a simple function, that:
If the application is handling an event in the main event loop returns true
If the application is handling an event in another [sub] event loop, it returns false
An alternative could be to return a level that indicates the 'depth' of the handled event.
Anyone suggestions?
You could hook into the event loop of your main thread/application using QAbstractEventDispatcher. Conditionaly filter out QTimer-events based on your application state.

Resources