Skip to content

Commit 3a86460

Browse files
committed
cxx-qt-lib: Add binding for QQmlApplicationEngine::singletonInstance
This allows accessing QObject singleton instances registered in the QML engine (using #[qml_singleton]) from the Rust side.
1 parent f5afefe commit 3a86460

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3838
- QObject subclasses can now inherit from other CXX-Qt generated QObject classes
3939
- `BUILD_WASM` CMake option to support WebAssembly builds and a book page for building for WASM
4040
- Add support for cxx_name and rust_name on qproperty attributes which applies to the QProperty generated as well as functions
41+
- Add binding for `singletonInstance` of `QQmlApplicationEngine`, allowing access to singleton instances registered in the QML engine.
4142

4243
### Changed
4344

crates/cxx-qt-lib/include/qml/qqmlapplicationengine.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ qqmlapplicationengineNew();
2222
QQmlEngine&
2323
qqmlapplicationengineAsQQmlEngine(QQmlApplicationEngine&);
2424

25+
#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
26+
void*
27+
qqmlapplicationengineSingletonInstance(QQmlApplicationEngine& engine, QAnyStringView uri, QAnyStringView typeName);
28+
#endif
29+
2530
}
2631
}
2732

crates/cxx-qt-lib/src/qml/qqmlapplicationengine.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,13 @@ qqmlapplicationengineAsQQmlEngine(QQmlApplicationEngine& engine)
2222
return static_cast<QQmlEngine&>(engine);
2323
}
2424

25+
#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
26+
void*
27+
qqmlapplicationengineSingletonInstance(QQmlApplicationEngine& engine, QAnyStringView uri, QAnyStringView typeName)
28+
{
29+
return reinterpret_cast<void*>(engine.singletonInstance<QObject*>(uri, typeName));
30+
}
31+
#endif
32+
2533
}
2634
}

crates/cxx-qt-lib/src/qml/qqmlapplicationengine.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ mod ffi {
6868

6969
#[namespace = "rust::cxxqtlib1"]
7070
unsafe extern "C++" {
71+
include!("cxx-qt-lib/common.h");
72+
type c_void = crate::c_void;
73+
7174
#[doc(hidden)]
7275
#[rust_name = "qqmlapplicationengine_new"]
7376
fn qqmlapplicationengineNew() -> UniquePtr<QQmlApplicationEngine>;
@@ -79,6 +82,20 @@ mod ffi {
7982
) -> Pin<&mut QQmlEngine>;
8083
}
8184

85+
#[cfg(any(cxxqt_qt_version_at_least_7, cxxqt_qt_version_at_least_6_5))]
86+
unsafe extern "C++" {
87+
include!("cxx-qt-lib/qanystringview.h");
88+
type QAnyStringView<'a> = crate::QAnyStringView<'a>;
89+
}
90+
91+
#[namespace = "rust::cxxqtlib1"]
92+
#[cfg(any(cxxqt_qt_version_at_least_7, cxxqt_qt_version_at_least_6_5))]
93+
unsafe extern "C++" {
94+
#[doc(hidden)]
95+
#[rust_name = "qqmlapplicationengine_singleton_instance"]
96+
fn qqmlapplicationengineSingletonInstance(ptr: Pin<&mut QQmlApplicationEngine>, uri: QAnyStringView, typeName: QAnyStringView) -> *mut c_void;
97+
}
98+
8299
// QQmlApplicationEngine is not a trivial to CXX and is not relocatable in Qt
83100
// as the following fails in C++. So we cannot mark it as a trivial type
84101
// and need to use references or pointers.
@@ -87,6 +104,8 @@ mod ffi {
87104
}
88105

89106
use crate::QQmlEngine;
107+
#[cfg(any(cxxqt_qt_version_at_least_7, cxxqt_qt_version_at_least_6_5))]
108+
use crate::QAnyStringView;
90109
use core::pin::Pin;
91110

92111
pub use ffi::QQmlApplicationEngine;
@@ -101,4 +120,17 @@ impl QQmlApplicationEngine {
101120
pub fn new() -> cxx::UniquePtr<Self> {
102121
ffi::qqmlapplicationengine_new()
103122
}
123+
124+
/// Returns the instance of a singleton type named typeName from the module specified by uri.
125+
/// This function was introduced in Qt 6.5.
126+
#[cfg(any(cxxqt_qt_version_at_least_7, cxxqt_qt_version_at_least_6_5))]
127+
pub fn singleton_instance<T>(self: Pin<&mut Self>, uri: QAnyStringView, type_name: QAnyStringView) -> Option<&mut T> {
128+
unsafe {
129+
let ptr = ffi::qqmlapplicationengine_singleton_instance(self, uri, type_name);
130+
if ptr.is_null() {
131+
return None;
132+
}
133+
Some(&mut *(ptr as *mut T))
134+
}
135+
}
104136
}

0 commit comments

Comments
 (0)