如何在QT(C++)中从子进程联系父进程以执行类中的方法?
我有一个问题。我目前正在 QT (C++) 中编写程序,我遇到了这个问题。如果我选中该复选框,它将启动一个方法,其中实现 fork()。父进程继续到最后(以保持未冻结的 GUI)。子进程在无限循环中工作。完成一个序列后,我将必要的数据存储到共享内存中。现在我需要联系父进程从共享内存中读取数据并在 GUI 上打印。我尝试了信号、管道、信号量...我的问题是...我需要联系父进程,我需要让他进入类,其中有一个用于打印 GUI 输出的方法。如果我使用信号处理程序,则没有机会返回类(我需要共享内存的 ID 才能连接)。我希望我的问题可以理解。我需要找到一个解决方案,我可以在其中联系父进程(类似于 QT 中的 connect() )并从类中调用必要的方法。
I have a question. I'm currently working on program in QT (C++) and I have this problem. 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用消息(mq 手册页),也可以进行轮询。如果共享内存中特定偏移处的值被设置,则意味着子进程完成并且共享内存中的数据已准备好。
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.
我可能解决这个问题的方法是让 GUI 在主线程中运行,然后使用 QThread 在选中复选框时启动。该 QThread 可以连续运行,也可以根据需要通过复选框停止并重新启动。
该过程完成后,如果数据量较小,则会发出线程安全信号 可用于将数据传输到 GUI 线程,并且 GUI 线程上的槽可以更新显示。如果有大量数据(正如您在问题中所暗示的那样),请使共享内存可供两者使用(通过将其放在单独的模块中或使用创建 QThread 对象时创建的指针)并保护对具有共享QMutex的内存。这个QMutex可以在GUI线程中创建,然后在创建时传递给工作线程。当数据准备好处理时,可以从工作线程向 GUI 线程发送线程安全信号,GUI 线程中的槽可以锁定(或 tryLock)QMutex,从共享内存中取出数据,然后解锁QMutex(并且可能向工作线程发送回信号以告诉其继续)。
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).