在 Qt/Cocoa 中获取 AppleEvent

发布于 2024-12-12 01:03:47 字数 812 浏览 0 评论 0原文

我有一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖 2024-12-19 01:03:47

我有同样的问题。我不确定,但我认为它确实有效。但从不同的侧面来看一下。为什么要保留遗留代码? Carbon API 不是 64 位。迁移此代码应该不难(检查 NSAppleEventManager)。

这是注册方式:

NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
[appleEventManager setEventHandler:self
                       andSelector:@selector(handleEvent:withReplyEvent:)
                     forEventClass:kCoreEventClass
                        andEventID:kAEOpenDocuments];

这是方法

  • (void)handleEvent:(NSAppleEventDescriptor *)event withReplyEvent: (NSAppleEventDescriptor *)replyEvent
    {
    请记住,

您必须将 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:

NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
[appleEventManager setEventHandler:self
                       andSelector:@selector(handleEvent:withReplyEvent:)
                     forEventClass:kCoreEventClass
                        andEventID:kAEOpenDocuments];

And this is method

  • (void)handleEvent:(NSAppleEventDescriptor *)event withReplyEvent: (NSAppleEventDescriptor *)replyEvent
    {
    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文