Skip to content

Commit 7225e6e

Browse files
committed
Add QML realted conditions
- Add conditional compilation of the QML-related code - Throw fatal error if generating of the QML code is enabled but QML is not found Fixes #239
1 parent c618516 commit 7225e6e

File tree

6 files changed

+32
-7
lines changed

6 files changed

+32
-7
lines changed

cmake/QtProtobufGen.cmake

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ function(qtprotobuf_generate)
3232
set(GENERATION_OPTIONS ${GENERATION_TYPE})
3333

3434
if(qtprotobuf_generate_QML)
35-
message(STATUS "Enabled QML generation for ${GENERATED_TARGET_NAME}")
35+
if(NOT TARGET Qt5::Qml)
36+
message(FATAL_ERROR "Trying to enable QML support for ${GENERATED_TARGET_NAME}, but Qt5::Qml is not a target."
37+
" find_package(Qt5 COMPONENTS Qml) is missing?")
38+
endif()
39+
message(STATUS "Enabling QML generation for ${GENERATED_TARGET_NAME}")
3640
set(GENERATION_OPTIONS "${GENERATION_OPTIONS}:QML")
3741
endif()
3842

src/grpc/qtgrpcglobal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
#endif
5353

5454
#ifndef Q_GRPC_IMPORT_QUICK_PLUGIN
55-
#ifdef QT_PROTOBUF_STATIC
55+
#if defined(QT_PROTOBUF_STATIC) && defined(QT_QML_LIB) // TODO: Check how detect this in Qt6
5656
#include <QtPlugin>
5757
#include <QQmlExtensionPlugin>
5858
#define Q_GRPC_IMPORT_QUICK_PLUGIN() \

src/protobuf/qprotobuflazymessagepointer.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
#pragma once //QProtobufLazyMessagePointer
2727

2828
#include "qtprotobufglobal.h"
29-
#include <QQmlEngine>
3029
#include <QObject>
30+
#if defined(QT_QML_LIB) // TODO: Check how detect this in Qt6
31+
# include <QQmlEngine>
32+
#endif
3133

3234
#include <memory>
3335
#include <type_traits>
@@ -96,7 +98,12 @@ class QProtobufLazyMessagePointer {//TODO: final?
9698

9799
private:
98100
void checkAndRelease() const {
99-
if (m_ptr != nullptr && QQmlEngine::objectOwnership(m_ptr.get()) == QQmlEngine::JavaScriptOwnership) {
101+
#if defined(QT_QML_LIB)
102+
bool qmlCheck = QQmlEngine::objectOwnership(m_ptr.get()) == QQmlEngine::JavaScriptOwnership;
103+
#else
104+
bool qmlCheck = true;
105+
#endif
106+
if (m_ptr != nullptr && qmlCheck) {
100107
m_ptr.release();//In case if object owned by QML it's qml responsibility to clean it up
101108
QObject::disconnect(m_destroyed);
102109
}

src/protobuf/qtprotobufglobal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
* \endcode
7979
*/
8080
#ifndef Q_PROTOBUF_IMPORT_QUICK_PLUGIN
81-
#ifdef QT_PROTOBUF_STATIC
81+
#if defined(QT_PROTOBUF_STATIC) && defined(QT_QML_LIB) // TODO: Check how detect this in Qt6
8282
#include <QtPlugin>
8383
#include <QQmlExtensionPlugin>
8484
#define Q_PROTOBUF_IMPORT_QUICK_PLUGIN() \

src/qttypes/CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,19 @@ qt_protobuf_internal_add_library(ProtobufQtTypes
1919

2020

2121
file(GLOB PROTO_FILE ${CMAKE_CURRENT_SOURCE_DIR}/QtProtobuf/Qt*.proto)
22+
23+
set(qml_enabled "")
24+
if(TARGET Qt5::Qml)
25+
set(qml_enabled QML)
26+
endif()
27+
2228
qtprotobuf_generate(TARGET ProtobufQtTypes
2329
OUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated
2430
PROTO_FILES ${PROTO_FILE}
25-
QML FOLDER ${extra_type_libraries_options})
31+
FOLDER
32+
${qml_enabled}
33+
${extra_type_libraries_options}
34+
)
2635

2736
target_compile_definitions(ProtobufQtTypes PRIVATE QT_BUILD_PROTOBUF_QT_TYPES_LIB)
2837

src/wellknowntypes/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ function(add_wellknowntype type_name)
1515
set(lookup_dirs "${QT_PROTOBUF_SOURCE_DIR}/3rdparty/grpc/third_party/protobuf/src"
1616
${Protobuf_INCLUDE_DIRS}
1717
)
18+
19+
set(qml_enabled "")
20+
if(TARGET Qt5::Qml)
21+
set(qml_enabled QML)
22+
endif()
1823
foreach(dir ${lookup_dirs})
1924
file(GLOB PROTO_FILE "${dir}/google/protobuf/${type_name}.proto")
2025
if(NOT "${PROTO_FILE}" STREQUAL "")
@@ -24,7 +29,7 @@ function(add_wellknowntype type_name)
2429
OUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated"
2530
PROTO_FILES "${PROTO_FILE}"
2631
PROTO_INCLUDES "-I\"${dir}\""
27-
QML
32+
${qml_enabled}
2833
FOLDER
2934
${extra_type_libraries_options}
3035
)

0 commit comments

Comments
 (0)