From 8680ba9b7cb78b83700a364006b8b6a06922d1ef Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Fri, 14 Jun 2024 14:34:49 +0200 Subject: [PATCH] feat: Add KDBindings_QT_NO_EMIT for Qt compatibility As mentioned in #79, KDBindings currently has issues in combination with Qt's `emit` keyword. So warn the user and add an option to set QT_NO_EMIT, which should solve this issue for most users. --- CMakeLists.txt | 1 + README.md | 10 ++++++++++ src/kdbindings/CMakeLists.txt | 3 +++ src/kdbindings/signal.h | 11 ++++++++++- 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f57d74..0acf546 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,7 @@ option(${PROJECT_NAME}_TESTS "Build the tests" ON) option(${PROJECT_NAME}_EXAMPLES "Build the examples" ON) option(${PROJECT_NAME}_DOCS "Build the API documentation" OFF) option(${PROJECT_NAME}_ERROR_ON_WARNING "Enable all compiler warnings and treat them as errors" OFF) +option(${PROJECT_NAME}_QT_NO_EMIT "Qt Compatibility: Disable Qt's `emit` keyword" OFF) list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/ECM/modules) diff --git a/README.md b/README.md index c2d6cf8..c3c9c98 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,16 @@ Find more information at: * [detailed browsable API reference](https://docs.kdab.com/kdbindings/latest/getting-started/) * [our assorted example programs](https://github.com/KDAB/KDBindings/blob/main/examples) +Compatibility with Qt +===================== +Because Qt defines `emit` as a keyword, Qt is by default incompatible with KDBindings, which uses `emit` as a function name. + +This can only be fixed by defining `QT_NO_EMIT` or `QT_NO_KEYWORDS` and simply omitting `emit` in your Qt code. + +To make this easy, KDBindings includes a CMake option: `KDBindings_QT_NO_EMIT`, which defines this for all dependents of KDBindings. + +See also: [#79](https://github.com/KDAB/KDBindings/issues/79) + Contact ======= * Visit us on GitHub: https://github.com/KDAB/KDBindings diff --git a/src/kdbindings/CMakeLists.txt b/src/kdbindings/CMakeLists.txt index 42d4acf..5c5a42e 100644 --- a/src/kdbindings/CMakeLists.txt +++ b/src/kdbindings/CMakeLists.txt @@ -40,6 +40,9 @@ set_target_properties(KDBindings PROPERTIES if(KDBINDINGS_ENABLE_WARN_UNUSED) target_compile_definitions(KDBindings INTERFACE KDBINDINGS_ENABLE_WARN_UNUSED=1) endif() +if(KDBindings_QT_NO_EMIT) + target_compile_definitions(KDBindings INTERFACE QT_NO_EMIT) +endif() target_include_directories(KDBindings INTERFACE $ diff --git a/src/kdbindings/signal.h b/src/kdbindings/signal.h index c006f43..19d079d 100644 --- a/src/kdbindings/signal.h +++ b/src/kdbindings/signal.h @@ -17,6 +17,16 @@ #include #include +#ifdef emit +static_assert(false, "KDBindings is not compatible with Qt's 'emit' keyword.\n" + "To use KDBindings with Qt, please define QT_NO_EMIT to disable Qt's 'emit' keyword.\n" + "If you're using CMake you can set KDBindings_QT_NO_EMIT to ON to add this."); + +// Undefine emit to suppress more compiler errors after the static_assert failure. +// Otherwise the compiler would drown our custom error message with more errors. +#undef emit +#endif + #include #include #include @@ -29,7 +39,6 @@ * All public parts of KDBindings are members of this namespace. */ namespace KDBindings { - /** * @brief A Signal provides a mechanism for communication between objects. *