在MacOS X下使用Qt在前台启动应用程序 [英] Launching app in foreground with Qt under MacOS X

查看:69
本文介绍了在MacOS X下使用Qt在前台启动应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Mac OS X 10.8下有一些Qt 5.0.1程序的问题. (我尚未测试任何其他平台.)

I am having problems with a little Qt 5.0.1 program under Mac OS X 10.8. (I have not tested any other platforms yet.)

我正在使用以下代码启动外部Mac OS X程序:

I am launching an external Mac OS X program with this line of code:

QDesktopServices::openUrl(QUrl::fromLocalFile(fullpath));

fullpath包含类似于以下内容的应用程序的路径:

Where fullpath contains a path to an application like:

/Users/schube/QTWorkspace/HelloWorld-build-Desktop_Qt_5_0_1_clang_64bit-Debug/HelloWorld.app/Contents/MacOS/../../../Aptus.app

(Aptus.app是我选择的随机应用,可以是任何应用.出于测试目的,我将其放置在此路径中.)

(Aptus.app is a random app I've chosen, could be any app. I placed it in this path for testing purposes).

应用程序正确启动,但始终在后台中;或至少在Finder窗口后面.真的很奇怪!

The application starts correctly but always in background; or at least, behind a Finder window. Really strange!

如何强制将新启动的应用发送到前景?

How can I force the new launched app to be sent to the foreground?

推荐答案

请改用QProcess,但请确保不要将可执行文件的路径用作要运行的对象,而应将其作为参数传递给open命令.像这样:-

Use QProcess instead, but make sure that rather than using the path to the executable as the object to run, pass it to the open command as an argument. Something like this: -

QString cmd = QString("open %1").arg(fullpath); // may need QUrl::fromLocalFile(fullpath)
QProcess::startDetached(cmd);

不使用打开"功能,它也会在其他应用程序后面打开.

Without using 'open', it will also open up behind other applications.

请注意,如果要等待程序完成,也可以使用execute函数.

Note that you could also use the execute function, if you want to wait for the program to finish.

另外,使用open命令,我认为您只需要将路径传递到应用程序捆绑包,而不是传递到Contents/MacOS中其可执行文件的完整路径.无论哪种都可以.

Also, with the open command, I think you only need to pass the path to the app bundle, rather than full path to its executable in Contents/MacOS. Either should work.

这篇关于在MacOS X下使用Qt在前台启动应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆