【问题标题】:Getting AppleEvent's in Qt/Cocoa在 Qt/Cocoa 中获取 AppleEvent
【发布时间】:2011-10-25 16:39:00
【问题描述】:

我有一些 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?或者有没有更优雅的方法来拦截用户双击与应用程序关联的文档而没有特定于平台的代码?

【问题讨论】:

    标签: macos cocoa qt qt4


    【解决方案1】:

    我有同样的问题。我不确定,但我认为它确实有效。但是从不同的角度来看它。为什么要保留遗留代码? 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 代码而不会出现任何问题。

    【讨论】:

    • 谢谢,我试试看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-28
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多