diff --git a/crates/cxx-qt-build/src/lib.rs b/crates/cxx-qt-build/src/lib.rs index bb0b60153..90b020a38 100644 --- a/crates/cxx-qt-build/src/lib.rs +++ b/crates/cxx-qt-build/src/lib.rs @@ -256,7 +256,6 @@ impl CxxQtBuilder { pub fn new() -> Self { let mut qt_modules = HashSet::new(); qt_modules.insert("Core".to_owned()); - qt_modules.insert("Gui".to_owned()); Self { rust_sources: vec![], qobject_headers: vec![], @@ -304,7 +303,7 @@ impl CxxQtBuilder { /// Link additional [Qt modules](https://doc.qt.io/qt-6/qtmodules.html). /// Specify their names without the `Qt` prefix, for example `"Widgets"`. - /// The Core and Gui modules are linked automatically; there is no need to specify them. + /// The Core module is linked automatically; there is no need to specify it. pub fn qt_modules(mut self, modules: &[&str]) -> Self { self.qt_modules .extend(modules.iter().cloned().map(String::from)); @@ -351,6 +350,11 @@ impl CxxQtBuilder { // GCC + Clang self.cc_builder.flag_if_supported("-std=c++17"); + // Enable Qt Gui in C++ if the feature is enabled + if self.qt_modules.contains("Gui") { + self.cc_builder.define("CXX_QT_GUI_FEATURE", None); + } + let mut qtbuild = qt_build_utils::QtBuild::new(self.qt_modules.into_iter().collect()) .expect("Could not find Qt installation"); qtbuild.cargo_link_libraries(); diff --git a/crates/cxx-qt-lib-headers/Cargo.toml b/crates/cxx-qt-lib-headers/Cargo.toml index a414ee51f..5584abfd1 100644 --- a/crates/cxx-qt-lib-headers/Cargo.toml +++ b/crates/cxx-qt-lib-headers/Cargo.toml @@ -10,3 +10,7 @@ edition.workspace = true license.workspace = true description = "A small crate for cxx-qt-lib and cxx-qt-build to share cxx-qt-lib's C++ headers" repository.workspace = true + +[features] +default = [] +qt_gui = [] diff --git a/crates/cxx-qt-lib-headers/include/qvariant.h b/crates/cxx-qt-lib-headers/include/qvariant.h index 21f5c1d4c..4157c6af1 100644 --- a/crates/cxx-qt-lib-headers/include/qvariant.h +++ b/crates/cxx-qt-lib-headers/include/qvariant.h @@ -23,7 +23,9 @@ #include #include +#ifdef CXX_QT_GUI_FEATURE #include +#endif #include "rust/cxx.h" @@ -62,7 +64,9 @@ CXX_QT_QVARIANT_CAN_CONVERT(I8) CXX_QT_QVARIANT_CAN_CONVERT(I16) CXX_QT_QVARIANT_CAN_CONVERT(I32) CXX_QT_QVARIANT_CAN_CONVERT(I64) +#ifdef CXX_QT_GUI_FEATURE CXX_QT_QVARIANT_CAN_CONVERT(QColor) +#endif CXX_QT_QVARIANT_CAN_CONVERT(QDate) CXX_QT_QVARIANT_CAN_CONVERT(QDateTime) CXX_QT_QVARIANT_CAN_CONVERT(QPoint) diff --git a/crates/cxx-qt-lib-headers/include/qvector.h b/crates/cxx-qt-lib-headers/include/qvector.h index 6a0869329..9615a51f4 100644 --- a/crates/cxx-qt-lib-headers/include/qvector.h +++ b/crates/cxx-qt-lib-headers/include/qvector.h @@ -22,7 +22,10 @@ #include #include #include + +#ifdef CXX_QT_GUI_FEATURE #include +#endif #include "rust/cxx.h" @@ -116,7 +119,9 @@ using QVector_i8 = QVector<::std::int8_t>; using QVector_i16 = QVector<::std::int16_t>; using QVector_i32 = QVector<::std::int32_t>; using QVector_i64 = QVector<::std::int64_t>; +#ifdef CXX_QT_GUI_FEATURE using QVector_QColor = QVector<::QColor>; +#endif using QVector_QDate = QVector<::QDate>; using QVector_QDateTime = QVector<::QDateTime>; using QVector_QPoint = QVector<::QPoint>; diff --git a/crates/cxx-qt-lib-headers/src/lib.rs b/crates/cxx-qt-lib-headers/src/lib.rs index 797f0a2de..e40376cd2 100644 --- a/crates/cxx-qt-lib-headers/src/lib.rs +++ b/crates/cxx-qt-lib-headers/src/lib.rs @@ -19,6 +19,7 @@ pub fn write_headers(directory: impl AsRef) { (include_str!("../include/convert.h"), "convert.h"), (include_str!("../include/cxxqt_thread.h"), "cxxqt_thread.h"), (include_str!("../include/qbytearray.h"), "qbytearray.h"), + #[cfg(feature = "qt_gui")] (include_str!("../include/qcolor.h"), "qcolor.h"), (include_str!("../include/qdate.h"), "qdate.h"), (include_str!("../include/qdatetime.h"), "qdatetime.h"), diff --git a/crates/cxx-qt-lib/Cargo.toml b/crates/cxx-qt-lib/Cargo.toml index 054eff09f..8de837ffa 100644 --- a/crates/cxx-qt-lib/Cargo.toml +++ b/crates/cxx-qt-lib/Cargo.toml @@ -22,3 +22,7 @@ cxx.workspace = true cxx-build.workspace = true cxx-qt-lib-headers.workspace = true qt-build-utils.workspace = true + +[features] +default = ["qt_gui"] +qt_gui = ["cxx-qt-lib-headers/qt_gui"] diff --git a/crates/cxx-qt-lib/build.rs b/crates/cxx-qt-lib/build.rs index 415c220d3..4ee4831b9 100644 --- a/crates/cxx-qt-lib/build.rs +++ b/crates/cxx-qt-lib/build.rs @@ -4,10 +4,14 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 fn main() { - let qt_modules = vec!["Core", "Gui"] - .iter() - .map(|m| String::from(*m)) - .collect(); + let qt_modules = vec![ + "Core", + #[cfg(feature = "qt_gui")] + "Gui", + ] + .iter() + .map(|m| String::from(*m)) + .collect(); let qtbuild = qt_build_utils::QtBuild::new(qt_modules).expect("Could not find Qt installation"); qtbuild.cargo_link_libraries(); @@ -20,6 +24,7 @@ fn main() { let rust_bridges = [ "qbytearray", + #[cfg(feature = "qt_gui")] "qcolor", "qdate", "qdatetime", @@ -62,6 +67,7 @@ fn main() { "qvariant/qvariant_i16", "qvariant/qvariant_i32", "qvariant/qvariant_i64", + #[cfg(feature = "qt_gui")] "qvariant/qvariant_qcolor", "qvariant/qvariant_qdate", "qvariant/qvariant_qdatetime", @@ -85,6 +91,7 @@ fn main() { "qvector/qvector_i16", "qvector/qvector_i32", "qvector/qvector_i64", + #[cfg(feature = "qt_gui")] "qvector/qvector_qcolor", "qvector/qvector_qdate", "qvector/qvector_qdatetime", @@ -121,6 +128,7 @@ fn main() { let cpp_files = [ "qbytearray", + #[cfg(feature = "qt_gui")] "qcolor", "qdate", "qdatetime", @@ -154,6 +162,10 @@ fn main() { cxx_qt_lib_headers::write_headers(format!("{}/cxx-qt-lib", out_dir)); builder.include(out_dir); + // Enable Qt Gui in C++ if the feature is enabled + #[cfg(feature = "qt_gui")] + builder.define("CXX_QT_GUI_FEATURE", None); + // MSVC builder.flag_if_supported("/std:c++17"); builder.flag_if_supported("/Zc:__cplusplus"); diff --git a/crates/cxx-qt-lib/src/types/mod.rs b/crates/cxx-qt-lib/src/types/mod.rs index ca90f65af..c74c1c5ef 100644 --- a/crates/cxx-qt-lib/src/types/mod.rs +++ b/crates/cxx-qt-lib/src/types/mod.rs @@ -6,7 +6,9 @@ mod qbytearray; pub use qbytearray::QByteArray; +#[cfg(feature = "qt_gui")] mod qcolor; +#[cfg(feature = "qt_gui")] pub use qcolor::QColor; mod qdate; diff --git a/crates/cxx-qt-lib/src/types/qvariant/mod.rs b/crates/cxx-qt-lib/src/types/qvariant/mod.rs index c16732969..a634969ef 100644 --- a/crates/cxx-qt-lib/src/types/qvariant/mod.rs +++ b/crates/cxx-qt-lib/src/types/qvariant/mod.rs @@ -6,8 +6,10 @@ use cxx::{type_id, ExternType}; use std::mem::MaybeUninit; +#[cfg(feature = "qt_gui")] +use crate::QColor; use crate::{ - QColor, QDate, QDateTime, QPoint, QPointF, QRect, QRectF, QSize, QSizeF, QString, QTime, QUrl, + QDate, QDateTime, QPoint, QPointF, QRect, QRectF, QSize, QSizeF, QString, QTime, QUrl, }; #[cxx::bridge] @@ -143,6 +145,7 @@ impl_qvariant_value!(i8, qvariant_i8); impl_qvariant_value!(i16, qvariant_i16); impl_qvariant_value!(i32, qvariant_i32); impl_qvariant_value!(i64, qvariant_i64); +#[cfg(feature = "qt_gui")] impl_qvariant_value!(QColor, qvariant_qcolor); impl_qvariant_value!(QDate, qvariant_qdate); impl_qvariant_value!(QDateTime, qvariant_qdatetime); diff --git a/crates/cxx-qt-lib/src/types/qvariant/qvariant.cpp b/crates/cxx-qt-lib/src/types/qvariant/qvariant.cpp index 758a9a211..65c561ecf 100644 --- a/crates/cxx-qt-lib/src/types/qvariant/qvariant.cpp +++ b/crates/cxx-qt-lib/src/types/qvariant/qvariant.cpp @@ -61,7 +61,9 @@ CXX_QT_QVARIANT_CAN_CONVERT_IMPL(::std::int8_t, I8) CXX_QT_QVARIANT_CAN_CONVERT_IMPL(::std::int16_t, I16) CXX_QT_QVARIANT_CAN_CONVERT_IMPL(::std::int32_t, I32) CXX_QT_QVARIANT_CAN_CONVERT_IMPL(::std::int64_t, I64) +#ifdef CXX_QT_GUI_FEATURE CXX_QT_QVARIANT_CAN_CONVERT_IMPL(QColor, QColor) +#endif CXX_QT_QVARIANT_CAN_CONVERT_IMPL(QDate, QDate) CXX_QT_QVARIANT_CAN_CONVERT_IMPL(QDateTime, QDateTime) CXX_QT_QVARIANT_CAN_CONVERT_IMPL(QPoint, QPoint) diff --git a/crates/cxx-qt-lib/src/types/qvector/mod.rs b/crates/cxx-qt-lib/src/types/qvector/mod.rs index 5ef592545..fdcd574f4 100644 --- a/crates/cxx-qt-lib/src/types/qvector/mod.rs +++ b/crates/cxx-qt-lib/src/types/qvector/mod.rs @@ -2,9 +2,11 @@ // SPDX-FileContributor: Andrew Hayzen // // SPDX-License-Identifier: MIT OR Apache-2.0 + +#[cfg(feature = "qt_gui")] +use crate::QColor; use crate::{ - QColor, QDate, QDateTime, QPoint, QPointF, QRect, QRectF, QSize, QSizeF, QString, QTime, QUrl, - QVariant, + QDate, QDateTime, QPoint, QPointF, QRect, QRectF, QSize, QSizeF, QString, QTime, QUrl, QVariant, }; use core::{marker::PhantomData, mem::MaybeUninit}; use cxx::{type_id, ExternType}; @@ -16,6 +18,7 @@ mod qvector_i16; mod qvector_i32; mod qvector_i64; mod qvector_i8; +#[cfg(feature = "qt_gui")] mod qvector_qcolor; mod qvector_qdate; mod qvector_qdatetime; @@ -313,6 +316,7 @@ impl_qvector_element!(i8, qvector_i8, "QVector_i8"); impl_qvector_element!(i16, qvector_i16, "QVector_i16"); impl_qvector_element!(i32, qvector_i32, "QVector_i32"); impl_qvector_element!(i64, qvector_i64, "QVector_i64"); +#[cfg(feature = "qt_gui")] impl_qvector_element!(QColor, qvector_qcolor, "QVector_QColor"); impl_qvector_element!(QDate, qvector_qdate, "QVector_QDate"); impl_qvector_element!(QDateTime, qvector_qdatetime, "QVector_QDateTime"); diff --git a/crates/cxx-qt-lib/src/types/qvector/qvector.cpp b/crates/cxx-qt-lib/src/types/qvector/qvector.cpp index e621379fa..a14132d75 100644 --- a/crates/cxx-qt-lib/src/types/qvector/qvector.cpp +++ b/crates/cxx-qt-lib/src/types/qvector/qvector.cpp @@ -38,7 +38,9 @@ CXX_QT_QVECTOR_ASSERTS(::std::int8_t, i8); CXX_QT_QVECTOR_ASSERTS(::std::int16_t, i16); CXX_QT_QVECTOR_ASSERTS(::std::int32_t, i32); CXX_QT_QVECTOR_ASSERTS(::std::int64_t, i64); +#ifdef CXX_QT_GUI_FEATURE CXX_QT_QVECTOR_ASSERTS(::QColor, QColor); +#endif CXX_QT_QVECTOR_ASSERTS(::QDate, QDate); CXX_QT_QVECTOR_ASSERTS(::QDateTime, QDateTime); CXX_QT_QVECTOR_ASSERTS(::QPoint, QPoint); diff --git a/examples/cargo_without_cmake/build.rs b/examples/cargo_without_cmake/build.rs index b46e2701b..55f855bba 100644 --- a/examples/cargo_without_cmake/build.rs +++ b/examples/cargo_without_cmake/build.rs @@ -15,9 +15,9 @@ fn command_help_output(command: &str) -> std::io::Result { fn main() { CxxQtBuilder::new() - // Link Qt's Qml and Network libraries. Qt Core and Gui are always - // linked, so there is no need to specify them here. - .qt_modules(&["Qml", "Network"]) + // Link Qt's Gui, Qml and Network libraries. Qt Core is always + // linked, so there is no need to specify it here. + .qt_modules(&["Gui", "Qml", "Network"]) // Generate C++ from the `#[cxx_qt::bridge]` module .file("src/cxxqt_object.rs") // Generate C++ code from the .qrc file with the rcc tool diff --git a/examples/qml_features/rust/build.rs b/examples/qml_features/rust/build.rs index d782af462..b9be34b52 100644 --- a/examples/qml_features/rust/build.rs +++ b/examples/qml_features/rust/build.rs @@ -12,6 +12,7 @@ fn main() { .cc_builder(|cc| { cc.include("../cpp"); }) + .qt_modules(&["Gui"]) .file("src/containers.rs") .file("src/custom_base_class.rs") .file("src/invokables.rs") diff --git a/tests/qt_types_standalone/rust/build.rs b/tests/qt_types_standalone/rust/build.rs index cc8caa090..6f5da0d4f 100644 --- a/tests/qt_types_standalone/rust/build.rs +++ b/tests/qt_types_standalone/rust/build.rs @@ -7,6 +7,7 @@ use cxx_qt_build::CxxQtBuilder; fn main() { CxxQtBuilder::new() + .qt_modules(&["Gui"]) .file("src/qbytearray.rs") .file("src/qcolor.rs") .file("src/qdate.rs")