在 Qt/Cocoa 中获取 AppleEvent
我有一些 Qt 4.4/Carbon 代码,可以很好地获取 Mac OS X 文档打开事件(例如,用户双击与应用程序关联的文档):
#ifdef Q_OS_MACX
#include <Carbon/Carbon.h>
#endif
MyApplication::MyApplication( int& argc, char** argv )
:QApplication( argc, argv )
{
#ifdef Q_OS_MACX
AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments,
TPApplication::appleEventHandler, 0, false );
#endif
}
MyApplication::~MyApplication()
{
#ifdef Q_OS_MACX
AERemoveEventHandler( kCoreEventClass, kAEOpenDocuments,
TPApplication::appleEventHandler, false );
#endif
}
#ifdef Q_OS_MACX
OSErr
TPApplication::appleEventHandler( const AppleEvent* ae, AppleEvent*, long )
{
// process events
}
#endif
我想将此代码移植到 Qt 4.7/Cocoa。是否仍然可以从基于 Cocoa 的 Qt 应用程序调用此代码,或者我是否必须调用 Cocoa API?或者是否有更优雅的方法来拦截用户双击与应用程序关联的文档,而无需特定于平台的代码?
I have some Qt 4.4/Carbon code that works fine for getting Mac OS X document open events (e.g. user double clicks on document associated with app):
#ifdef Q_OS_MACX
#include <Carbon/Carbon.h>
#endif
MyApplication::MyApplication( int& argc, char** argv )
:QApplication( argc, argv )
{
#ifdef Q_OS_MACX
AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments,
TPApplication::appleEventHandler, 0, false );
#endif
}
MyApplication::~MyApplication()
{
#ifdef Q_OS_MACX
AERemoveEventHandler( kCoreEventClass, kAEOpenDocuments,
TPApplication::appleEventHandler, false );
#endif
}
#ifdef Q_OS_MACX
OSErr
TPApplication::appleEventHandler( const AppleEvent* ae, AppleEvent*, long )
{
// process events
}
#endif
I want to port this code to Qt 4.7/Cocoa. Is it still possible to call this code from a Cocoa based Qt app, or do I have to call the Cocoa API instead? Or is there are more elegant way to intercept the user double clicking on a document associated with the app without platform specific code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有同样的问题。我不确定,但我认为它确实有效。但从不同的侧面来看一下。为什么要保留遗留代码? Carbon API 不是 64 位。迁移此代码应该不难(检查 NSAppleEventManager)。
这是注册方式:
这是方法
{
请记住,
您必须将 Cocoa 代码导出到另一个文件(最好是 .mm),但您仍然可以在该文件中使用 C++/Qt 代码,没有任何问题。
I had same problem. I'm not sure but i think it did work. But take a look at it from different side. Why keep legacy code? Carbon API is not 64 bit. Migration of this code shouldn't be hard (check NSAppleEventManager).
This is how you register:
And this is method
{
}
Remember that you will have to export Cocoa code to another file (best would be .mm), but still you can use your C++/Qt code inside of that file without any problem.