Your Guide for Deploying Qt Applications to MacOS
In this blog post we'll explore the basic steps to deploy a Qt application for macOS, including assigning an icon for the application and adding assets. It is assumed that you have Qt Creator and Qt version 5.7.1 or later installed and configured on your macOS computer. If not, this blog can be used as a reference point.
Getting Started
The first thing to understand is the structure of a macOS application. MacOS makes use of the concept of bundling. Mac applications are technically folders with .app extensions.
From a normal user's standpoint from the GUI, it is a single executable entity. But from a developer or command-line savvy user’s perspective, it is a folder with the executable and required resources, including libraries, assets and icon files.
Qmake, by default, automatically generates a bundle for the application without the project file explicitly stating this, but it is good practice to do so, i.e.
CONFIG += app_bundle # to remove, do CONFIG -= app_bundle
Icons
Before we compile and deploy, we should look at how to set the icon that is shown on the dock, desktop, and launch folder. Modify your project file to add the following line:
ICON = myIcon.icns
Where myIcons.icns is located within your project’s filetree. Qmake will automatically create the info.plist entry needed and copy the icon into the application’s resource folder (Contents/Resources).
Adding Additional Folders
If you have additional resource files you need to include with your application but that can't be put in a qrc resource file (QML files for example), you can append it to the QMAKE_BUNDLE_DATA section in your project file:
mySetOfExtraFiles.files = $$PWD/imagesFolder
mySetOfExtraFiles.path = Contents/Resources
QMAKE_BUNDLE_DATA += mySetOfExtraFiles
These three lines will tell qmake to copy the folder imagesFolder into the application’s Contents/Resources folder. If you need it to be in the same directory as the executable, you should put the path as Contents/MacOS.
Compiling and Deployment
The first step is to compile your code in release mode and prepare it to be distributable beyond your build directory.
Compiling from the terminal
Navigate to your code folder using the terminal, and compile it:
make clean
qmake -config release
make
If you were able to compile successfully without any errors, you will now see that a <ApplicationName>.app folder exists in your build directory.
Compiling from Qt Creator
To compile in Qt Creator go to Build > Clean Project “ProjectName”. This is equivalent to typing make clean on the command line. In the lower left corner of the Qt Creator interface, there is a monitor like icon, click on it and select your project name, and “release” build. Go to Build > Build project “ProjectName” (equivalent to running make on the command line).
Deployment
To deploy an application you will be making use of macdeployqt, which is a deployment tool included with Qt Creator. It is commonly located at <QTDIR>/bin/macdeployqt. (You can also search for it using the command find / -iname macdeployqt.) Qt Creator does not have this deployment option integrated into its interface so you will need to use it through the command line. macdeployqt is useful when your application has primarily Qt libraries and no custom libraries. If you have custom libraries, you will need to manually copy them into the bundle after it is created.
Assuming you are in the build directory, if you have no QML files, run:
macdeployqt <ApplicationName>.app
If you have QML files, instead use:
macdeployqt <ApplicationName>.app -qmldir=<path to QMLs>
To place the bundle into a disk image for easy distribution, add -dmg to the command. You can also zip or tar the .app folder after running macdeployqt on it.
An important note about the option -qmldir=<path>: the “path” is the absolute path to the QML source files in your project folder. This is necessary for the deployment tool to parse the QML files and include the necessary Qt libraries for those QML files. This is only applicable if you have QML files in your project.
Distributing
If all went well and no fatal errors were reported, you can now test the application by doing one of the following:
- Run the executable within the .app folder (<ApplicationName>.app/Contents/MacOS/<Application>)
- Run the application by navigating to the build folder in Finder and double clicking on the application
- Run the application inside the dmg or zip archive by double clicking on it in the Finder
If the application runs and does not crash, you’re ready to pass along the zip, tar, or dmg file to other macOS machines. Once the other machines receive the archive, just drag and drop the .app folder into the Applications folder and you’re all set.
If the application does crash and you were running it from the Finder, open a terminal and manually run the executable in the application. Any error messages should output to your terminal and give you an idea of what is going wrong.
I hope you've found this information valuable. (If so, please share!) More of our Qt-related technical content is available here.
References
Getting Started with Qt and Qt Creator on MacOS
Qt for MacOS - Deployment