Skip to content

Add support for C++ QML Elements in CxxQtBuilder #1146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions crates/cxx-qt-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ pub struct CxxQtBuilder {
include_prefix: String,
crate_include_root: Option<String>,
additional_include_dirs: Vec<PathBuf>,
qml_qobjects: Vec<(PathBuf, PathBuf)>,
}

impl CxxQtBuilder {
Expand Down Expand Up @@ -418,6 +419,7 @@ impl CxxQtBuilder {
include_prefix: crate_name(),
crate_include_root: Some(String::new()),
additional_include_dirs: vec![],
qml_qobjects: vec![],
}
}

Expand Down Expand Up @@ -588,6 +590,22 @@ impl CxxQtBuilder {
self
}

/// Specify a C++ header and source file containing a Q_OBJECT macro to run [moc](https://doc.qt.io/qt-6/moc.html) on.
/// Unlike [CxxQtBuilder::qobject_header], it includes the generated metatypes.json, so that C++ classes can be included
/// in QML modules.
Comment on lines +594 to +595
Copy link
Preview

Copilot AI Aug 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation should clarify the difference between this method and qobject_header more explicitly. Currently it only mentions that it 'includes the generated metatypes.json' but doesn't explain when to use each method.

Suggested change
/// Unlike [CxxQtBuilder::qobject_header], it includes the generated metatypes.json, so that C++ classes can be included
/// in QML modules.
///
/// This method is intended for integrating C++ QObject subclasses into QML modules. In addition to running `moc` on the provided files,
/// it generates and includes the `metatypes.json` file required for QML registration, allowing the C++ classes to be used directly in QML.
///
/// In contrast, [`CxxQtBuilder::qobject_header`] only runs `moc` on a header file and does **not** generate or include `metatypes.json`.
/// Use `qobject_header` when you only need to integrate a C++ QObject subclass with Qt (for example, for signal/slot support in C++),
/// but do **not** need to expose the class to QML.
///
/// **Use this method (`qml_qobject`) when you want your C++ QObject to be available in QML.**

Copilot uses AI. Check for mistakes.

pub fn qml_qobject(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cpp_qobject ? I guess this could have other applications outside of QML

mut self,
qobject_header: impl AsRef<Path>,
qobject_source: impl AsRef<Path>,
) -> Self {
let qobject_header = qobject_header.as_ref().to_path_buf();
let qobject_source = qobject_source.as_ref().to_path_buf();
println!("cargo::rerun-if-changed={}", qobject_header.display());
println!("cargo::rerun-if-changed={}", qobject_source.display());
self.qml_qobjects.push((qobject_header, qobject_source));
self
}

/// Use a closure to run additional customization on [CxxQtBuilder]'s internal [cc::Build]
/// before calling [CxxQtBuilder::build]. This allows to add extra include paths, compiler flags,
/// or anything else available via [cc::Build]'s API. For example, to add an include path for
Expand Down Expand Up @@ -869,6 +887,22 @@ impl CxxQtBuilder {
}
}

for (qobject_header, qobject_source) in &self.qml_qobjects {
cc_builder.file(qobject_source);
if let Some(dir) = qobject_header.parent() {
moc_include_paths.insert(dir.to_path_buf());
}
let moc_products = qtbuild.moc().compile(
qobject_header,
MocArguments::default().uri(qml_module.uri.clone()),
Copy link
Preview

Copilot AI Aug 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The qml_module.uri is being cloned inside a loop. Consider cloning it once before the loop and reusing the cloned value to avoid repeated allocations.

Suggested change
MocArguments::default().uri(qml_module.uri.clone()),
MocArguments::default().uri(qml_module_uri.clone()),

Copilot uses AI. Check for mistakes.

);
if let Some(dir) = moc_products.cpp.parent() {
moc_include_paths.insert(dir.to_path_buf());
}
cc_builder.file(moc_products.cpp);
qml_metatypes_json.push(moc_products.metatypes_json);
}

let qml_module_registration_files = qtbuild.register_qml_module(
&qml_metatypes_json,
&qml_module.uri,
Expand Down
Loading