简体   繁体   English

有没有办法使用Qt来获取进程的所有打开文件的列表?

[英]Is there a way to get a list of all opened files for a process using Qt?

Our application is cross-platform and thus written in Qt. 我们的应用程序是跨平台的,因此使用Qt编写。 The target platforms are Windows and Mac. 目标平台是Windows和Mac。 In our project we need to determine a list of all opened files for a separate process. 在我们的项目中,我们需要确定一个单独过程的所有打开文件的列表。 For now we do it in a platform dependent way using WinAPI and corresponding Mac OsX APIs. 目前,我们使用WinAPI和相应的Mac OsX API以平台相关的方式进行操作。 This approach has already caused many problems as long as we need to maintain two unrelated versions of the program. 只要我们需要维护程序的两个不相关的版本,这种方法就已经引起了很多问题。

If it can be any helpful, we need to determine a list of opened audio files for such programs as Traktor and Serato (Dj Intro and Scratch Live). 如果有帮助,我们需要为Traktor和Serato(Dj Intro和Scratch Live)等程序确定打开的音频文件的列表。 And the version of Qt we are using is 4.8.0. 我们使用的Qt版本是4.8.0。

So, the question will be, is there any platform-independent way to get a list of opened files? 因此,问题是,是否有任何与平台无关的方式来获取已打开文件的列表? Or maybe at least a some kind of workaround using some third party libraries or utils? 还是至少使用某些第三方库或实用程序的某种解决方法?

Even Qt is not platform independant ;) It just encapsulates the platform specifics (like WIN32 API vs. X11 API) under a common API. 即使Qt也不是平台无关的;)它只是将平台细节(如WIN3​​2 API与X11 API)封装在一个通用API下。 I would to the same in your case: design a (Qt based) class which provides the API you need in your application, and then use your already existing (platform specific) code to provide the functionality you need. 在您的情况下,我也是这样:设计一个(基于Qt的)类,该类在您的应用程序中提供您所需的API,然后使用您已经存在的(特定于平台的)代码来提供所需的功能。 In your Application, you would only use the Qt based API, and you do not need to worry about platform specifics in your application code. 在您的应用程序中,您将只使用基于Qt的API,而您无需担心应用程序代码中的平台细节。 Also, you can write any number of unit tests against your API to make sure it behaves correctly. 另外,您可以针对自己的API编写任意数量的单元测试,以确保其行为正常。

Example (not 100% complete code!): 示例(不是100%完整的代码!):

In your header file: 在头文件中:

class QMySpecificAPI : public QObject {
    Q_OBJECT    // only required when you need signals/slots

public:
    QList<QString> getOpenFiles();
}

In your cpp file: 在您的cpp文件中:

#include "QMySpecificAPI.h"

#ifdef Q_WS_WIN
QList<QString> QMySpecificAPI::getOpenFiles() {
   // Use WIN32 API to retrieve the file list
}
#endif

#ifdef Q_WS_MAC
QList<QString> QMySpecificAPI::getOpenFiles() {
    // Use MACOSX API to retrieve the file list
}
#endif

Especially since you say that you already have a lot of issues with your existing code, I strongly suggest to implement unit test cases and have them run regularly to automatically check your code, like 特别是由于您说现有代码已经存在很多问题,因此我强烈建议实施单元测试用例并定期运行它们以自动检查您的代码,例如

...
QMySpecificAPI api;
QList<QString> fileList = api.getOpenFiles();
ASSERT(fileList.count() == 5);   // or whatever you expect
...

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

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