Application Crash if sending very fast to QApplication::instance()->postEvent
-
Hello,
I am using QT in a Multithreaded application. Qt runs in a Sub Thread of my Main Application.
I am using the following code in a class which runs in another thread to post the Events to the QT Application Main Thread part:
QWidget *focus = QApplication::focusWidget(); // get Widget with focus if( focus ) { Qt::Key key= Qt::Key_Enter; QApplication::instance()->postEvent(focus, new QKeyEvent( QEvent::KeyPress, key, Qt::NoModifier)); QApplication::instance()->postEvent(focus, new QKeyEvent( QEvent::KeyRelease, key, Qt::NoModifier)); }
If I now calling this function from the non QT Application part everything seems to work fine until I send many events.
So for example just sending 10 events at once is no problem, but if I send a lot of events the Application crashes after some seconds.
If I comment out the postEvent part the App keeps running, so this has something to to with this function.
Am I using it wrong? Does Qt not queue the events?Any advice how to solve the application crash under high load?
Or am I using it totally wrong? How to call postEvent not from the main thread, to be crash safe?
Regards
-
Hi,
postEvent is a static method so basically you're not calling it correctly. In any case, what it is your use case ?
-
Hi,
I want to send events from Thread 1 which is the Main Application to Thread 2 where QT is executed using exec.
I want to call a function in Thread 1 if this thread receives a XML message over TCP with the action for QT which then sends the regarding Events to thread 2 (QT).
It works already so Display gets changed and so on but if i send too fast, the Application crashes.
is there a limit of sending commands to the focused widget?
Regards
-
Why do you want to send events to focused widget? What if another widget gets focus?
And why don't you use signals/slots?
Are you sure you need multi-threading? You can do TCP communication asynchronously in Qt. -
Hi,
the architecture is needed because we are running multiple applications which communicate with each other.
I also did the same implementation using a signal/slot approach but it also crashes if it gets under high load.The background of this is to simulate user interaction by sending a XML command to the Main Application which then calls the QT part.
If i put a sleep of 1 sec after posting the events it works , so I am wondering why it crashes if it gets fast events.
It seems to process handling crashes on fast calls (50-100 events a sec).
Many Thanks
-
Where exactly does your application crash?
Maybe you just have an synchronisation issue in your code since you use multi-threading. -
Hi,
I narrowed it down that it crash during postEvent call.If i comment the postEvent out all works fine. Also If I set a sleep from 500 ms it works.
Maybe the other application sends too fast the events that qt does not have time to handle them and then crashes?
-
Are you sure it is the postEvent call itself and not the code which is triggered by postEvent?
-
I mean do you have any code which is executed when you call postEvent? So, do you have any event handlers?
Yes it can happen that after if(focus) focus is not valid any more. This is one of the problem you can get in multi-threaded applications. -
Hi,
I just let the GUI handle the event .
For example if a button is focused and it gets the event for pressing the key the screen will change to a different widget. Or if left right is posted by the events the focus will move in the widget to a different button.But the events are handled in the widgets themselves then, so no bypass done here just normal Qt implementation with Signal and Slots.
Regards
-
Since you are running several applications, why not use IPC in your design ? That way you wouldn't need to do hacks to communicate between them