我的Qt5应用程序如何以编程方式知道哪些窗口和对话框正在运行

How can my Qt5 app know programmatically what windows and dialogs are running?

本文关键字:窗口 对话框 运行 应用程序 Qt5 方式知 编程 我的      更新时间:2023-10-16

我需要以编程方式知道什么窗口/对话框/小部件是打开的,因为我使用FileOpenEvent来加载文件,如果我的QMainWindow是唯一打开的,我只想加载文件。

我发现遍历所有QWindow s并检查isExposed() == true多个处理我正在做的一切,除了本机Mac对话框(另存为,打印和打开文件)。因此,这似乎足以满足我的需求:

  bool found_exposed_window = false;
  foreach (QWindow *window, qApp->allWindows()) {
    if (window->isExposed()) {
      if (found_exposed_window) {
        // This is the second exposed window, meaning the user has something
        //  open in addition to the main form.
        // So, don't try to load a file.
        return;
      }
      found_exposed_window = true;
    }
  }