Open file with double click on Mac
-
Hi, I am just reading up on how to open a file with my app on Mac OS X. Looks like it is a bit different from Unix and Windows. Is the answer from the last post still the correct and only way to do that? Or is there a better way that I could implement in my current code?
QCommandLineParser parser; parser.setApplicationDescription(QCoreApplication::applicationName()); parser.addHelpOption(); parser.addVersionOption(); parser.addPositionalArgument("file", "The file to open."); parser.process(app); MainWindow mainWin; if (!parser.positionalArguments().isEmpty()){ QStringList files = parser.positionalArguments(); foreach(QString path, files){ path.replace(QString("\\"), QString("/")); mainWin.addEditor(path); } }else{ qDebug() << "opening with no arguments" << argc << argv; } mainWin.show();
-
HI,
Take a look the QFileOpenEvent documentation, there's a full example on how to achieve what you want on macOS.
-
Hmm, the reason why I was asking is that I already use QtSingleApplication which subclasses QApplication. Currently the single application code is not working on OS X and I hope I do not need to make this extra complicated by adding the event stuff...
Regarding the plist modification. Do I have to do this manually every time or can I tell Qt Creator to a specific plist or add the required lines?
-
What exactly doesn't work on macOS ?
From my experience QtSingleApplication does work on that platform.
Create your own Info.plist and use QMAKE_INFO_PLIST.
You can use an event filter if subclassing is too much of a hassle.
-
@SGaist said in Open file with double click on Mac:
What exactly doesn't work on macOS ?
From my experience QtSingleApplication does work on that platform.
On OSX you can't launch the same application several times, so QtSingleApplication is useless.
Catching the FileOpenEvent is the only way ... -
@SGaist said in Open file with double click on Mac:
@mpergand yes you can even though it's not particularly easy. Note it's also not overly complicated so use of QtSingleApplication is not useless if you want to ensure that one and only one copy of an application is running.
The only way that I know is to make a copy of an application with a different name.
[edit]
It's also possible with the 'open' command from the terminal. (suprisingly !) -
@SGaist thanks again for the help. I modified the QtSingleApplication to look for the open event. It seems to work on Windows and OS X now. I am not sure why the single application class did not work on Mac. At first I had issues that lockfile and lockfile_unix were loaded at the same time with the same functions. Then the app crashed because the signal did not work.
After copying the files forth and back between my Mac and Windows laptop it suddenly worked. I probably made a mistake with cleaning the project and rebuilding it after I copied the files. -