在C++ main()执行之前处理Mac OS X文件打开事件

7

我已经在谷歌上搜索了很多,但仍然无法找到好的解决方案 :/
所以,我正在将一个复杂的Qt5应用程序(某个网络服务的客户端)移植到Mac OS X(10.7.0“Lion”及更高版本)。

我需要处理自定义文件(如*.xyz)和自定义URL协议(如xyz://)。
好的,Qt5有QFileOpenEvent类来处理OS X适当的事件。
但是:此事件仅在应用程序事件循环开始后到达(显然)!

我需要在main函数开始之前“捕获”并处理OS X的文件打开事件,因为程序逻辑仅设计为处理命令行参数处理。

简化的main函数代码:

int main(int argc, char[]* argv)
{
     QApplication app( argc, argv );

     QStringList arguments = app.arguments();
     if( arguments.count() == argc ) arguments.removeFirst();

     Logic appLogic( NULL, &app );
     app.installMessageHandler( &appLogic );

     // The problem:
     // **This function will always called earlier than the any event**
     if( ! appLogic.start( arguments ) ) return 0;

     // Start processing of events
     // Only after this call Logic class get the desired event
     return app.exec();
}

有没有一种方法可以在 C++ 的 main 函数开始之前获取 OS X 文件打开事件或者在 argv 参数中获取“我的”文件/ URL?也许,一些 Objective-C 的黑魔法能够完成这项工作吗?
注意:start 执行了许多复杂的 - 和异步的 - 事情。事件在其执行期间到达,因此当异步任务已经在运行时很难处理它。所以看起来我只需要防止 start 的执行,如果事件将要到达。
如果应用程序已经打开,则没有问题。
1个回答

7

我找到了一种可能有些奇怪的解决方案-使用Qt事件系统。

int main(int argc, char[]* argv)
{
    QApplication app( argc, argv );

    QStringList arguments = app.arguments();
    if( arguments.count() == argc ) arguments.removeFirst();

    Logic appLogic( NULL, &app );
    #ifdef Q_OS_MAC
    app.installMessageHandler( &appLogic );

    // Here we should alreasy get FileOpenEvent, if it occurs
    // NOTE: without this FileOpenEvent will arrive LATER
    //       than the DryRunEvent!
    app.processEvents();

    // If there is no file open event in the queue,
    // we should just open the blank program window
    // NOTE: Qt takes ownership of this event object,
    //       so you should not delete it manually
    DryRunEvent* runEv = new DryRunEvent( p );
    a.postEvent( &l, runEv, Qt::LowEventPriority );
    #endif

    ...

    #ifndef Q_OS_MAC
    if( ! appLogic.start( arguments ) ) return 0;
    #endif

    return app.exec();
}

自定义事件头:

class BaseEvent : public QEvent
{
public:
    BaseEvent( QEvent::Type& eType ) : QEvent( getEventType( eType ) )
    {
    }

    ~BaseEvent() {}

    QEvent::Type getEventType( QEvent::Type& eType )
    {
        if( eType == QEvent::None )
        {
            eType = static_cast<QEvent::Type>( QEvent::registerEventType() );
        }
        return eType;
    }
};

class DryRunEvent : public BaseEvent
{
    QStringList m_params;

    public:
         DryRunEvent( const Parameters& params ) :
             BaseEvent( eventType ), m_params( params )
         {
         }

         ~DryRunEvent(){}

         QStringList GetCmdLineParams() const { return m_params; }

    public:
        static QEvent::Type eventType;
};

定制事件源:
QEvent::Type ViewerDryRunEvent::eventType = QEvent::None;

逻辑类头文件:

class Logic : public QObject
{
    Q_OBJECT
    Q_DISABLE_COPY( Logic )

    public:
        explicit Logic(QObject *parent, QApplication* application);
        virtual ~Logic();

    public slots:
        bool Start( QStringList parameters );
        void ReceiveParameters( QStringList parameters );
        void Stop();

        #ifdef Q_OS_MAC
        bool Logic::WasStarted() const
        {
            ... Determine wether logic was started or not ...
        }
        #endif

    private:
        #ifdef OS_MACOSX
        // Virtual overrided functions
         bool eventFilter( QObject* obj, QEvent* event )
         {
             if( event->type() == QEvent::FileOpen )
             {
                 QFileOpenEvent* fileEvent = static_cast< QFileOpenEvent* >(event);
                 Q_ASSERT( fileEvent != NULL );

                 QString uri;
                 if( fileEvent->file().isEmpty() == false )
                 {
                     uri = fileEvent->file();
                 }
                 else if( fileEvent->url().isEmpty() == false )
                 {
                     uri = fileEvent->url().toString();
                 }

                 if( uri.isEmpty() == false )
                 {
                     if( WasStarted() ) ReceiveParameters( uri );
                     else Start( uri );
                 }

                 return false;
             }
             else if( event->type() == DryRunEvent::eventType )
             {
                 DryRunEvent* myEvent = static_cast< DryRunEvent* >( event );
                 Q_ASSERT( myEvent != NULL );
                 QStringList cmdLineParams = myEvent->GetCmdLineParams();

                 Q_ASSERT( !WasStarted() );
                 if( WasStarted() ) return false;

                 if( !Start( cmdLineParams ) ) m_application->exit( 0 );
                 return false;
             }

             // Standard event processing
             return QObject::eventFilter( obj, event );
        }
        #endif
    };

我希望有人能够发现这些内容有用:)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接