Using Qt 5.5.0 C++, I made a very simple widget-based application example -- just a form with no buttons on it, going with defaults. If I compile it and run it through macdeployqt, then run the application out of the dmg file, it runs just fine. However, if I edit the main.cpp and add this line in...
QApplication::setStyle(QStyleFactory::create("Fusion"));
...before the QApplication a(argc, argv);
line...
...which of course requires that my mainwindow.h have an #include <QtWidgets>
line, then run it through macdeployqt, I get the following error. (Actually, I get a crash, but when I open up the example.app file and find the binary under example.app/Contents/MacOS/example, and run that, I get a terminal window that tells me the following message.)
This application failed to start because it could not find or load the Qt platform plugin "cocoa".
Reinstalling the application may fix this problem.
Abort trap: 6
So, it's having trouble finding the cocoa plugin. However, I have the cocoa plugin in this path:
example.app/Contents/PlugIns/platforms/libqcocoa.dylib
I imagine I need to use the otool
and install_name_tool
commands to fix something that's broke with macdeployqt
, but don't know what the technique is.
Some blogs may mention adding the following style line BEFORE the QApplication a(argc, argv);
line:
QApplication::setStyle(QStyleFactory::create("Fusion"));
If you do that, Qt 5.5 will throw a Cocoa error and the application will crash when you try to deploy it via macdeployqt, but won't crash when running it in Qt Creator.
The fix is simple. The blogs are wrong -- put the style line AFTER the QApplication a(argc, argv);
line, not before it. Now when you deploy through macdeployqt, the Cocoa crash problem goes away.