Skip to content

Commit

Permalink
feat: Add KDBindings_QT_NO_EMIT for Qt compatibility
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LeonMatthesKDAB committed Jun 14, 2024
1 parent f9d19dc commit 8680ba9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/kdbindings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>
Expand Down
11 changes: 10 additions & 1 deletion src/kdbindings/signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
#include <type_traits>
#include <utility>

#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 <kdbindings/connection_evaluator.h>
#include <kdbindings/genindex_array.h>
#include <kdbindings/utils.h>
Expand All @@ -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.
*
Expand Down

0 comments on commit 8680ba9

Please sign in to comment.