Please nominate your Qt Champions for 2021! https://forum.qt.io/topic/132134/looking-for-the-2021-qt-champions

Setting up the application information in CMake



  • How should I set the application information using CMake. For QMake we can use - https://stackoverflow.com/a/11971210/3782963.

    The process name in Activity monitor (macOS) is empty and looks something like this:

    Screen Shot 2021-01-07 at 2.43.45 PM.png



  • I think I found the way to do this. We need win.rc on windows and Info.plist on mac, these files tell their OS's what to name the application.

    Windows

    For example, this is what I have (win.rc):

    IDI_ICON1               ICON    DISCARDABLE     "resources/images/datalogger.ico"
    
    
    1 VERSIONINFO
        FILEVERSION    0,0,0,0
        PRODUCTVERSION 0,0,0,0
    {
        BLOCK "StringFileInfo"
        {
            BLOCK "000004B0"
            {
                VALUE "CompanyName",        "DataLogger\0"
                VALUE "FileDescription",    "DataLogger\0"
                VALUE "FileVersion",        "git\0"
                VALUE "OriginalFilename",   "datalogger.exe\0"
                VALUE "ProductName",        "DataLogger\0"
                VALUE "ProductVersion",     "git\0"
            }
        }
        BLOCK "VarFileInfo"
        {
            VALUE "Translation", 0x0000, 0x04B0
        }
    }
    

    then I added this filename to CMakeLists.txt before find_package(...) as:

    if (WIN32)
        set(APP_ICON_RESOURCE_WINDOWS "${CMAKE_CURRENT_SOURCE_DIR}/win.rc")
    endif ()
    

    Finally add this:

    then add

    set(PROJECT_SOURCES
    
            main.cpp
    
            resources.qrc
    
    +        ${APP_ICON_RESOURCE_WINDOWS}
    
            mainwindow.cpp mainwindow.h mainwindow.ui
    )
    

    macOS

    I have a file called Info.plist in the root

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
        <dict>
            <key>CFBundleDocumentTypes</key>
            <array>
                <dict>
                    <key>CFBundleTypeIconFile</key>
                    <string>datalogger.icns</string>
                    <key>CFBundleTypeName</key>
                    <string>DataLogger</string>
                    <key>CFBundleTypeRole</key>
                    <string>Viewer</string>
                    <key>LSIsAppleDefaultForType</key>
                    <true/>
                </dict>
            </array>
            <key>CFBundleIconFile</key>
            <string>datalogger.icns</string>
            <key>CFBundlePackageType</key>
            <string>APPL</string>
            <key>NSPrincipalClass</key>
            <string>NSApplication</string>
            <key>CFBundleSignature</key>
            <string>????</string>
            <key>CFBundleExecutable</key>
            <string>datalogger</string>
            <key>NSHumanReadableCopyright</key>
            <string>Copyright (c) 2021, Akshay Raj Gollahalli</string>
            <key>CFBundleIdentifier</key>
            <string>datalogger</string>
            <key>CFBundleName</key>
            <string>DataLogger</string>
            <key>CFBundleShortVersionString</key>
            <string>3.1.2</string>
            <key>AppleMagnifiedMode</key>
            <false/>
        </dict>
    </plist>
    

    In your CMakeList.txt before find_package(...), add:

    if (APPLE)
        set(MACOSX_BUNDLE_ICON_FILE datalogger.icns)
        # And the following tells CMake where to find and install the file itself.
        set(app_icon_macos "resources/images/datalogger.icns")
        set_source_files_properties(${app_icon_macos} PROPERTIES
                MACOSX_PACKAGE_LOCATION "Resources")
    endif ()
    

    then add

    set(PROJECT_SOURCES
    +        MACOSX_BUNDLE
    
            main.cpp
    
            resources.qrc
    
    +        ${app_icon_macos}
    
            mainwindow.cpp mainwindow.h mainwindow.ui
    )
    

  • Moderators

    I assume this is Windows? Unfortunately CMake doesn't provide a convenient API for this yet. See the request https://gitlab.kitware.com/cmake/cmake/-/issues/21314 that we filed about this a while ago.

    Meanwhile you can add the information by adding an .rc file manually. For Qt binaries, the logic for this is done in QtCoreMacros.cmake (check out _qt_internal_generate_win32_rc_file function). But this isn't public API, because we feel this should rather be provided upstream, in CMake.



  • @kkoehne No this is Mac. I think in windows it shows the name with .exe.

    OK will give this a try.



  • I think I found the way to do this. We need win.rc on windows and Info.plist on mac, these files tell their OS's what to name the application.

    Windows

    For example, this is what I have (win.rc):

    IDI_ICON1               ICON    DISCARDABLE     "resources/images/datalogger.ico"
    
    
    1 VERSIONINFO
        FILEVERSION    0,0,0,0
        PRODUCTVERSION 0,0,0,0
    {
        BLOCK "StringFileInfo"
        {
            BLOCK "000004B0"
            {
                VALUE "CompanyName",        "DataLogger\0"
                VALUE "FileDescription",    "DataLogger\0"
                VALUE "FileVersion",        "git\0"
                VALUE "OriginalFilename",   "datalogger.exe\0"
                VALUE "ProductName",        "DataLogger\0"
                VALUE "ProductVersion",     "git\0"
            }
        }
        BLOCK "VarFileInfo"
        {
            VALUE "Translation", 0x0000, 0x04B0
        }
    }
    

    then I added this filename to CMakeLists.txt before find_package(...) as:

    if (WIN32)
        set(APP_ICON_RESOURCE_WINDOWS "${CMAKE_CURRENT_SOURCE_DIR}/win.rc")
    endif ()
    

    Finally add this:

    then add

    set(PROJECT_SOURCES
    
            main.cpp
    
            resources.qrc
    
    +        ${APP_ICON_RESOURCE_WINDOWS}
    
            mainwindow.cpp mainwindow.h mainwindow.ui
    )
    

    macOS

    I have a file called Info.plist in the root

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
        <dict>
            <key>CFBundleDocumentTypes</key>
            <array>
                <dict>
                    <key>CFBundleTypeIconFile</key>
                    <string>datalogger.icns</string>
                    <key>CFBundleTypeName</key>
                    <string>DataLogger</string>
                    <key>CFBundleTypeRole</key>
                    <string>Viewer</string>
                    <key>LSIsAppleDefaultForType</key>
                    <true/>
                </dict>
            </array>
            <key>CFBundleIconFile</key>
            <string>datalogger.icns</string>
            <key>CFBundlePackageType</key>
            <string>APPL</string>
            <key>NSPrincipalClass</key>
            <string>NSApplication</string>
            <key>CFBundleSignature</key>
            <string>????</string>
            <key>CFBundleExecutable</key>
            <string>datalogger</string>
            <key>NSHumanReadableCopyright</key>
            <string>Copyright (c) 2021, Akshay Raj Gollahalli</string>
            <key>CFBundleIdentifier</key>
            <string>datalogger</string>
            <key>CFBundleName</key>
            <string>DataLogger</string>
            <key>CFBundleShortVersionString</key>
            <string>3.1.2</string>
            <key>AppleMagnifiedMode</key>
            <false/>
        </dict>
    </plist>
    

    In your CMakeList.txt before find_package(...), add:

    if (APPLE)
        set(MACOSX_BUNDLE_ICON_FILE datalogger.icns)
        # And the following tells CMake where to find and install the file itself.
        set(app_icon_macos "resources/images/datalogger.icns")
        set_source_files_properties(${app_icon_macos} PROPERTIES
                MACOSX_PACKAGE_LOCATION "Resources")
    endif ()
    

    then add

    set(PROJECT_SOURCES
    +        MACOSX_BUNDLE
    
            main.cpp
    
            resources.qrc
    
    +        ${app_icon_macos}
    
            mainwindow.cpp mainwindow.h mainwindow.ui
    )
    

Log in to reply