开发者

How to contact parent process from child in QT (C++) to perfom method from class?

开发者 https://www.devze.com 2023-02-17 04:38 出处:网络
I have a question. I\'m currently working on program in QT (C++) and I have this pro开发者_如何学编程blem. If I check the checkbox, it starts a method, where is implement fork (). Parent process conti

I have a question. I'm currently working on program in QT (C++) and I have this pro开发者_如何学编程blem. If I check the checkbox, it starts a method, where is implement fork (). Parent process continues to the end (to keep unfrozen GUI). A child process works in ifinite loop. After completing a sequence, I store the necessary data into shared memory. Now I need to contact parent process to read data from shared memory and print on the GUI. I tried signals, pipes, semaphors ... My problem is... I need to contact parent proces and I need to get him into class, where is a method for printing an output of the GUI. If I use signal handler, there is no chance to get back into the class (I need ID of a shared memory to connect). I hope that my question is littlebit understandable. I need to find a solution, where I can contact parent process (something like connect() in QT) and call necessary method from class.


You could use messages (mq man pages), or you could do a polling. If a value at specific offset in the shared memory is set, that means the child process finished and data in the shared memory is ready.


The way I would probably approach this problem would be to have the GUI operating in the main thread and then to have a QThread started when the checkbox is checked. This QThread can run continuously or be stopped and restarted with the checkbox depending on the requirements.

Once the process has completed, if the amount of data is small, a thread-safe signal can be used to transmit the data to the GUI thread and a slot on the GUI thread can update the display. If there is a lot of data (as you imply in your question), make the shared memory available to both (either by having it in a separate module or by using the pointer that was created when creating the QThread object) and protect access to the memory with a shared QMutex. This QMutex can be created in the GUI thread and then passed to the worker thread when creating it. When the data is ready for processing, a thread-safe signal can be sent from the worker thread to the GUI thread and the slot in the GUI thread can lock (or tryLock) the QMutex, take the data from the shared memory and then unlock the QMutex (and maybe send a signal back to the worker thread to tell it to continue).

0

精彩评论

暂无评论...
验证码 换一张
取 消