[Solved] Where to intercept (mac os) QFileOpenEvent ?



  • I'm getting my Qt app working on Mac OS now (it's running nicely on Windows). So, I want to open documents which the user double-clicks on in the Finder, which means responding to QFileOpen events. All fine.
    I've read the QCoreApplication docs (http://qt-project.org/doc/qt-5/qcoreapplication.html#notify) and I suppose an override of QApplication::notify() should be the preferred option. However, I don't currently have a sub-class of QApplication in my project, so I'm thinking to implement QObject::event() for my QMainWindow sub-class. Yes, basically I'm lazy*.
    So, the question I have really, is, does anyone have any suggestion as to where best to intercept these events ? Since I don't really understand the event handling routes within Qt, I'm not sure if this would be a lot less efficient** than implementing a QApplication sub-class. I'm sure it will make very little difference in terms of the structure of the classes in my design. But if it saves a few ops if I do a bit more typing, it all goes to save the planet…

    Tim

    • I like to think, efficient
      ** I don't see why my apps cannot be lazy too :-)

  • Lifetime Qt Champion

    Hi,

    Long time since I played with that but IIRC, you can simply handle it in the event function of e.g. your QMainWindow subclass



  • bq. handle it in the event function of e.g. your QMainWindow subclass

    Apparently not, it seems. At least, that's what I tried, but the event never gets that far. In the docs for QFileOpenEvent, it specifically states that the event is sent to the QApplication::instance(). And I guess it stops there...
    So, I bit the bullet, sub-classed QApplication, and overrode event(), sending it directly to my QMainWIndow. It's not pretty :-
    @bool PGazetteApp::event( QEvent * a_pEvent)
    {
    // Intercept FileOpen events
    if( a_pEvent && a_pEvent->type()==QEvent::FileOpen )
    { // Send file open events to the Main Window
    QWindowList WinList( topLevelWindows() );
    QWindowList::iterator pIt = WinList.begin();
    if( pIt == WinList.end() )
    return false;

    QWindow * pMainWin( *pIt );
    return sendEvent( pMainWin, a_pEvent );
    }
    return QApplication::event( a_pEvent );
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.