### Qt Standard Project Setup Handling Source: https://github.com/oclero/qtappinstancemanager/blob/master/CMakeLists.txt Conditional setup for qt_standard_project_setup() based on Qt version. ```cmake # qt_standard_project_setup() is only available in Qt >= 6.3. if(COMMAND qt_standard_project_setup) qt_standard_project_setup() else() set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTORCC ON) endif() ``` -------------------------------- ### CMakeLists.txt for Multiple Instance Example Source: https://github.com/oclero/qtappinstancemanager/blob/master/examples/multiple/CMakeLists.txt This CMakeLists.txt file configures the build for the MultipleInstanceExample, setting up dependencies and target properties. ```cmake find_package(Qt6 REQUIRED COMPONENTS Core) add_executable(MultipleInstanceExample) target_sources(MultipleInstanceExample PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp ) target_link_libraries(MultipleInstanceExample PRIVATE ${PROJECT_NAMESPACE}::${PROJECT_NAME} Qt::Core ) set_target_properties(MultipleInstanceExample PROPERTIES INTERNAL_CONSOLE ON EXCLUDE_FROM_ALL ON FOLDER examples ) ``` -------------------------------- ### Target Installation Source: https://github.com/oclero/qtappinstancemanager/blob/master/src/CMakeLists.txt Installs the QtAppInstanceManager target and its associated files. ```cmake install(TARGETS ${PROJECT_NAME} EXPORT "${PROJECT_NAME}Targets" LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" ) install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include/" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/" ) install(EXPORT "${PROJECT_NAME}Targets" FILE "${PROJECT_NAME}Targets.cmake" NAMESPACE ${PROJECT_NAMESPACE}:: DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" ) install(FILES "${CMAKE_BINARY_DIR}/cmake/${PROJECT_NAME}Config.cmake" "${CMAKE_BINARY_DIR}/cmake/${PROJECT_NAME}ConfigVersion.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" ) ``` -------------------------------- ### Single application example Source: https://github.com/oclero/qtappinstancemanager/blob/master/README.md Example of how to initialize QtAppInstanceManager to force only one instance running and handle messages from secondary instances. ```cpp // Initialize instance manager to force only one instance running. oclero::QtAppInstanceManager instanceManager; instanceManager.setMode(QtAppInstanceManager::Mode::SingleInstance); // When another instance will start, it will immediately quit the app and send its // arguments to the primary instance. QObject::connect(&instanceManager, &oclero::QtAppInstanceManager::secondaryInstanceMessageReceived, &instanceManager, [](const unsigned int id, QByteArray const& data) { // Do what you want: // - Raise the main window. // - Open a file in an another tab of your main window. // - ... qDebug() << "Secondary instance message received: " << data; }); // If you don't want for the app to quit, you can set the manual mode and handle this step by yourself. instanceManager.setAppExitMode(QtAppInstanceManager::AppExitMode::Manual); QObject::connect(&instanceManager, &QtAppInstanceManager::appExitRequested, &singleInstance, []() { // Do what you want. // Usually you should quit the app. qDebug() << "This app should exit"; QCoreApplication::quit(); std::exit(EXIT_SUCCESS); }); ``` -------------------------------- ### CMakeLists.txt for Single Instance Example Source: https://github.com/oclero/qtappinstancemanager/blob/master/examples/single/CMakeLists.txt This CMakeLists.txt file configures the build for a single instance Qt application, linking necessary libraries and setting target properties. ```cmake find_package(Qt6 REQUIRED COMPONENTS Core) add_executable(SingleInstanceExample) target_sources(SingleInstanceExample PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp ) target_link_libraries(SingleInstanceExample PRIVATE ${PROJECT_NAMESPACE}::${PROJECT_NAME} Qt::Core ) set_target_properties(SingleInstanceExample PROPERTIES INTERNAL_CONSOLE ON EXCLUDE_FROM_ALL ON FOLDER examples ) ``` -------------------------------- ### Project Setup Source: https://github.com/oclero/qtappinstancemanager/blob/master/CMakeLists.txt Basic CMake minimum version, parallel build setting, module path, and project definition. ```cmake cmake_minimum_required(VERSION 3.21.0) # Enable parallel build (not enabled by default on Windows). set(CMAKE_BUILD_PARALLEL_LEVEL $ENV{NUMBER_OF_PROCESSORS}) enable_testing() set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") # Set project information. project("QtAppInstanceManager" LANGUAGES CXX VERSION 1.3.2.0 DESCRIPTION "A tool to communicate between the instances of your Qt application." HOMEPAGE_URL "https://github.com/oclero/QtAppInstanceManager" ) set(PROJECT_NAMESPACE "oclero") # Global flags. set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set_property(GLOBAL PROPERTY USE_FOLDERS ON) if(NOT CMAKE_OSX_DEPLOYMENT_TARGET) set(CMAKE_OSX_DEPLOYMENT_TARGET "13.6") endif() set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") ``` -------------------------------- ### Add the library as a dependency in your CMakeLists.txt Source: https://github.com/oclero/qtappinstancemanager/blob/master/README.md Example of how to add the library as a dependency using FetchContent in CMake. ```cmake include(FetchContent) FetchContent_Declare(QtAppInstanceManager G IT_REPOSITORY "https://github.com/oclero/qtappinstancemanager.git" ) FetchContent_MakeAvailable(QtAppInstanceManager) ``` -------------------------------- ### Subdirectories Source: https://github.com/oclero/qtappinstancemanager/blob/master/CMakeLists.txt Adds subdirectories for library, tests, and examples based on defined variables. ```cmake # The library. add_subdirectory(src) # Tests. if(QTAPPINSTANCEMANAGER_TESTS) add_subdirectory(tests) endif() # Examples. if(QTAPPINSTANCEMANAGER_EXAMPLES) add_subdirectory(examples/single) add_subdirectory(examples/multiple) endif() ``` -------------------------------- ### Visual Studio Startup Project Source: https://github.com/oclero/qtappinstancemanager/blob/master/src/CMakeLists.txt Sets the startup project for Visual Studio when targeting Windows. ```cmake # Select correct startup project in Visual Studio. if(WIN32) set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT ${PROJECT_NAME}) endif() ``` -------------------------------- ### Package Configuration File Generation Source: https://github.com/oclero/qtappinstancemanager/blob/master/src/CMakeLists.txt Generates the package configuration file and version file for CMake. ```cmake # Install target configure_package_config_file( "${CMAKE_CURRENT_SOURCE_DIR}/../cmake/config.cmake.in" "${CMAKE_BINARY_DIR}/cmake/${PROJECT_NAME}Config.cmake" INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" ) write_basic_package_version_file( "${CMAKE_BINARY_DIR}/cmake/${PROJECT_NAME}ConfigVersion.cmake" VERSION "${PROJECT_VERSION}" COMPATIBILITY AnyNewerVersion ) ``` -------------------------------- ### Include the necessary header Source: https://github.com/oclero/qtappinstancemanager/blob/master/README.md Include the main header file for QtAppInstanceManager in your C++ code. ```cpp #include ``` -------------------------------- ### CMakeLists.txt Configuration Source: https://github.com/oclero/qtappinstancemanager/blob/master/tests/CMakeLists.txt This snippet shows the CMake configuration for setting up the test executable, including finding Qt packages, defining sources, include directories, and linking libraries. ```cmake set(TESTS_TARGET_NAME ${PROJECT_NAME}Tests) find_package(Qt6 REQUIRED COMPONENTS Core Test) add_executable(${TESTS_TARGET_NAME}) set_target_properties(${TESTS_TARGET_NAME} PROPERTIES CMAKE_AUTOMOC ON CMAKE_AUTORCC ON INTERNAL_CONSOLE ON EXCLUDE_FROM_ALL ON FOLDER tests ) set(TESTS_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/QtAppInstanceManagerTests.hpp ${CMAKE_CURRENT_SOURCE_DIR}/src/QtAppInstanceManagerTests.cpp ) target_sources(${TESTS_TARGET_NAME} PRIVATE ${TESTS_SOURCES} ) target_include_directories(${TESTS_TARGET_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ) target_link_libraries(${TESTS_TARGET_NAME} PRIVATE ${PROJECT_NAMESPACE}::${PROJECT_NAME} Qt::Core Qt::Test ) add_test(NAME ${TESTS_TARGET_NAME} COMMAND $ WORKING_DIRECTORY $) source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${TESTS_SOURCES}) ``` -------------------------------- ### Qt Integration Source: https://github.com/oclero/qtappinstancemanager/blob/master/CMakeLists.txt Includes GNUInstallDirs and finds the Qt6 package with required components. ```cmake # Include GNUInstallDirs for standard installation paths. include(GNUInstallDirs) # Find Qt. find_package(Qt6 REQUIRED COMPONENTS Core Network) ``` -------------------------------- ### Include Directories Configuration Source: https://github.com/oclero/qtappinstancemanager/blob/master/src/CMakeLists.txt Configures public and private include directories for the QtAppInstanceManager library. ```cmake target_include_directories(${PROJECT_NAME} PUBLIC $ PRIVATE $ ) ``` -------------------------------- ### Multiple application instances Source: https://github.com/oclero/qtappinstancemanager/blob/master/README.md This C++ code demonstrates how to use the QtAppInstanceManager to handle multiple application instances, including receiving messages from other instances and reacting to role changes. ```c++ oclero::QtAppInstanceManager instanceManager; // If we are the FIRST instance to launch, // we'll receive messages from other instances that will launch after us. QObject::connect(&instanceManager, &oclero::QtAppInstanceManager::secondaryInstanceMessageReceived, &instanceManager, [](const unsigned int id, QByteArray const& data) { qDebug() << "Message received from secondary instance: " << data; }); // If we are another instance, we are able to receive messages from the primary one. QObject::connect(&instanceManager, &oclero::QtAppInstanceManager::primaryInstanceMessageReceived, &instanceManager, [](QByteArray const& data) { qDebug() << "Message received from primary instance: " << data; }); // If ever the first instance to launch unexpectly shuts down, // one of the secondary instances will immediately take of the role of the primary one. QObject::connect(&instanceManager, &oclero::QtAppInstanceManager::instanceRoleChanged, &instanceManager, [&instanceManager]() { // There is a short period of time before roles are assigned again. if (!instanceManager.isPrimaryInstance() && !instanceManager.isSecondaryInstance()) { qDebug() << "Waiting for new role..."; } else { qDebug() << "New role: " << (instanceManager.isPrimaryInstance() ? "Primary" : "Secondary"); } }); ``` -------------------------------- ### Source Grouping Source: https://github.com/oclero/qtappinstancemanager/blob/master/src/CMakeLists.txt Organizes source files into groups based on their directory structure. ```cmake # Create source groups. source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${HEADERS} ${SOURCES} ) ``` -------------------------------- ### Target Properties Source: https://github.com/oclero/qtappinstancemanager/blob/master/src/CMakeLists.txt Sets various properties for the QtAppInstanceManager target, including output name, versioning, and build-related flags. ```cmake set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME ${PROJECT_NAME} PROJECT_LABEL ${PROJECT_NAME} FOLDER src SOVERSION ${PROJECT_VERSION_MAJOR} VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} DEBUG_POSTFIX _debug CMAKE_AUTORCC ON CMAKE_AUTOMOC ON CMAKE_AUTOUIC ON ) ``` -------------------------------- ### QtAppInstanceManager Library Creation Source: https://github.com/oclero/qtappinstancemanager/blob/master/src/CMakeLists.txt Defines the library target for QtAppInstanceManager, including source and header files, and sets up alias for external use. ```cmake set(HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include/oclero/QtAppInstanceManager.hpp ) set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/source/oclero/QtAppInstanceManager.cpp ${CMAKE_CURRENT_SOURCE_DIR}/source/oclero/LocalEndpoint.cpp ${CMAKE_CURRENT_SOURCE_DIR}/source/oclero/LocalEndpoint.hpp ) # Create target. qt_add_library(${PROJECT_NAME} STATIC ${HEADERS} ${SOURCES} ) add_library(${PROJECT_NAMESPACE}::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) ``` -------------------------------- ### Export Targets Source: https://github.com/oclero/qtappinstancemanager/blob/master/src/CMakeLists.txt Exports the targets for use by other CMake projects. ```cmake export(EXPORT "${PROJECT_NAME}Targets" FILE "${CMAKE_BINARY_DIR}/cmake/${PROJECT_NAME}Targets.cmake" NAMESPACE ${PROJECT_NAMESPACE}:: ) ``` -------------------------------- ### Compile Options Source: https://github.com/oclero/qtappinstancemanager/blob/master/src/CMakeLists.txt Sets compile options for the QtAppInstanceManager library, with specific options for MSVC and other compilers. ```cmake target_compile_options(${PROJECT_NAME} PRIVATE $<$:/MP /WX /W4> $<$>:-Wall -Wextra -Werror> ) ``` -------------------------------- ### Link Libraries Source: https://github.com/oclero/qtappinstancemanager/blob/master/src/CMakeLists.txt Links the QtAppInstanceManager library with Qt Core and Qt Network modules. ```cmake target_link_libraries(${PROJECT_NAME} PRIVATE Qt::Core Qt::Network ) ``` -------------------------------- ### Link with the library in CMake Source: https://github.com/oclero/qtappinstancemanager/blob/master/README.md How to link the QtAppInstanceManager library to your project in CMake. ```cmake target_link_libraries(your_project oclero::QtAppInstanceManager) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.