c++ - QT 应用程序在 OSX 中启动另一个应用程序

标签 c++ macos qmake macdeployqt

我目前正在开发一个代理“Agent.app”,它必须扫描您的 USB 端口,如果它检测到一个 USB 设备,该应用程序会启动一个名为“Filebrowser.app”的应用程序。

我希望 Agent.app 位于 Filebrowser.app 中以避免多个应用程序。

我的第一个应用程序 FileBrowser 是在 QT creator 下用 Qt/c++ 开发的。我还在 Qt Creator 下开发了 Agent.app,但它只是一个与 Qt 无关的 C++ 类。

我想我需要在 FileBrowser.pri 中声明 agent.pri 以确保 FileBrowser.app 将包含它。

我在代理中所做的是:

int main()
{
    PulsUsbDetection *MyUSBDevice = new PulsUsbDetection;

    if(MyUSBDevice->isPulsConnected() == true) {
        char *filepath = "/Applications/puls_connect.app";
        string command = "open " + filepath;
        system(command.c_str());
    }

    return 0;
}

如果应用不在 Application.app 中,这是我能做的最糟糕的事情。

我有两个问题:

  • 当代理在 FileBrowser.app 中时,如何允许 agent.app 启动 FileBrowser.app?
  • 如何确保 agent.app 将包含在 FileBrowser.app 中?

非常感谢

最佳答案

I want the Agent.app located inside the Filebrowser.app to avoid multiple apps.

您是否考虑过只有一个程序包含两个应用程序的功能,并选择(在 main() 内部)基于例如哪个功能来执行?命令行参数?那会更容易实现,并且还会使您的安装包小很多。也就是说……

How to make sure that the agent.app will be included in the FileBrowser.app ?

编译两个程序后,将 agent.app 文件夹移动到 FileBrowser.app/Contents/Resources 文件夹中。您可以使用 shell 命令或 bash 脚本来执行此操作,或者只需右键单击 FileBrowser.app 图标,从菜单中选择“打开包”,然后打开 Contents 和 Resources 子文件夹并将 agent.app 拖入.

How to allow the agent.app to start the FileBrowser.app when the agent is inside the FileBrowser.app ?

您要做的第一件事是找到您的“代理”可执行文件所在目录的路径。幸运的是,Qt 有一个方法可以做到这一点:您可以调用QApplication::applicationDirPath()。 .

因此,例如,如果您的 FileBrowser.app 被放置在/Applications 中,而 agent.app 被放置在 FileBrowser.app/Contents/Resources 中(如上所述),那么上述函数调用将返回类似“/Applications/FileBrowser.app/Contents/Resources/agent.app/Contents/MacOS”。

第二步是从该字符串派生出您的 FileBrowser 应用程序的路径。这应该很简单:

// This code to be run in the agent app, to launch the FileBrowser app...
QString agentExecutableDirPath = QApplication::applicationDirPath();
QString fileBrowserExecutablePath = agentExecutableDirPath + "../../../../MacOS/FileBrowser";
system(QFile::encodeName(fileBrowserExecutablePath).constData());  // run it

请注意,使用 Qt 的 QProcess 启动 FileBrowser 进程可能会获得更好的行为。 API 而不是通过调用 system()——特别是,system() 调用在 FileBrowser 进程退出之前不会返回,这会产生一个副作用,即在此之前卡住您的代理 GUI。

关于c++ - QT 应用程序在 OSX 中启动另一个应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30064432/

相关文章:

c++ - 将 const_iterator 分配给迭代器

xcode - 无法在Mac上恢复购买

linux - debug_and_release 选项不适用于 linux

c++ - MS Windows 中的 QT 和 native OpenGL 支持

android - 无法让 opengl es 2.0 渲染三角形

macos - Apple 是否支持 Java 6?

c++ - 迪尔德 : Library not loaded (image not found) but it's there

linux - 在 Fedora 中构建 KeePass 2.0 Alpha 6

c - 在 Qt Creator 中正确设置 SDL

c# - C# 中的 Marshall C++ 结构