简体   繁体   English

如何确定分离过程的主窗口是否可见

[英]how to determine if a main window of detached process is visible or not

I have a tool to run main application - both are QApplications . 我有一个运行主应用程序的工具-两者都是QApplications in the tool I start main app as QProcess::startDetached("myapp.exe", QStringList() << "-arg1" << "-arg2", "C:/myFolder/"); 在工具中,我以QProcess::startDetached("myapp.exe", QStringList() << "-arg1" << "-arg2", "C:/myFolder/");启动主应用程序QProcess::startDetached("myapp.exe", QStringList() << "-arg1" << "-arg2", "C:/myFolder/");

then the tool finishes its work and the main app is still working. 然后该工具完成工作,主应用程序仍在工作。

I can start the tool again, and send commands via QSharedMemory . 我可以再次启动该工具,并通过QSharedMemory发送命令。 But if main app has opened modal dialog, it cannot handle new commands. 但是,如果主应用程序已打开模式对话框,则无法处理新命令。

I need to determine from the tool that main app is busy with modal dialog and return some kind of error. 我需要从工具中确定主应用程序正忙于模态对话框,并返回某种错误。

could anyone please tell me what is the simplest way to do it. 谁能告诉我最简单的方法是什么。

I have two ideas, but they really don't know if any of them will work: 我有两个想法,但是它们真的不知道它们是否会起作用:

  1. use QApplication::activeModalWidget() , but how to get QApplication* of main app from the tool? 使用QApplication::activeModalWidget() ,但是如何从工具中获取主应用程序的QApplication*

  2. put information that the main app is busy in shared memory, and the tool'll check it before closing itself. 将主应用程序繁忙的信息存储在共享内存中,然后该工具会在关闭自身之前对其进行检查。 - I'm not sure that the main app'll be able to save something in shared memory while it's busy with modal dialog. -我不确定主应用程序在忙于模态对话框时是否能够将某些内容保存在共享内存中。

Personally, I'd use QLocalServer in the main app and QLocalSocket in the tool. 就个人而言,我会使用QLocalServer在主应用程序和QLocalSocket的工具。

When the main app starts, it starts the local server running, allowing the tool to connect to it and communicate as required. 当主应用程序启动时,它将启动本地服务器运行,从而允许该工具连接到该服务器并根据需要进行通信。

At its most basic level, if the tool can connect to the server, you know the main application is running. 从最基本的角度来看,如果该工具可以连接到服务器,则可以知道主应用程序正在运行。 However I would recommend using this IPC mechanism over QSharedMemory. 但是,我建议在QSharedMemory上使用此IPC机制。

Done properly, the tool can happily disconnect and reconnect without issue. 正确完成后,该工具可以愉快地断开连接并重新连接,而不会出现问题。

Qt provides an example of how to use QLocalServer with QLocalSocket Qt提供了有关如何将QLocalServerQLocalSocket 结合使用的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM