Skip to content

Commit 02a5168

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 02a5168

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ qqmlapplicationengineNew();
2222
QQmlEngine&
2323
qqmlapplicationengineAsQQmlEngine(QQmlApplicationEngine&);
2424

25+
void*
26+
qqmlapplicationengineSingletonInstance(QQmlApplicationEngine& engine, QAnyStringView uri, QAnyStringView typeName);
27+
2528
}
2629
}
2730

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

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

25+
void*
26+
qqmlapplicationengineSingletonInstance(QQmlApplicationEngine& engine, QAnyStringView uri, QAnyStringView typeName)
27+
{
28+
return reinterpret_cast<void*>(engine.singletonInstance<QObject*>(uri, typeName));
29+
}
30+
2531
}
2632
}

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ mod ffi {
1212
type QStringList = crate::QStringList;
1313
include!("cxx-qt-lib/qurl.h");
1414
type QUrl = crate::QUrl;
15+
include!("cxx-qt-lib/qanystringview.h");
16+
type QAnyStringView<'a> = crate::QAnyStringView<'a>;
1517

1618
include!("cxx-qt-lib/qqmlapplicationengine.h");
1719
type QQmlApplicationEngine;
@@ -68,6 +70,9 @@ mod ffi {
6870

6971
#[namespace = "rust::cxxqtlib1"]
7072
unsafe extern "C++" {
73+
include!("cxx-qt-lib/common.h");
74+
type c_void = crate::c_void;
75+
7176
#[doc(hidden)]
7277
#[rust_name = "qqmlapplicationengine_new"]
7378
fn qqmlapplicationengineNew() -> UniquePtr<QQmlApplicationEngine>;
@@ -77,6 +82,10 @@ mod ffi {
7782
fn qqmlapplicationengineAsQQmlEngine(
7883
ptr: Pin<&mut QQmlApplicationEngine>,
7984
) -> Pin<&mut QQmlEngine>;
85+
86+
#[doc(hidden)]
87+
#[rust_name = "qqmlapplicationengine_singleton_instance"]
88+
fn qqmlapplicationengineSingletonInstance(ptr: Pin<&mut QQmlApplicationEngine>, uri: QAnyStringView, typeName: QAnyStringView) -> *mut c_void;
8089
}
8190

8291
// QQmlApplicationEngine is not a trivial to CXX and is not relocatable in Qt
@@ -86,7 +95,7 @@ mod ffi {
8695
impl UniquePtr<QQmlApplicationEngine> {}
8796
}
8897

89-
use crate::QQmlEngine;
98+
use crate::{c_void, QAnyStringView, QQmlEngine};
9099
use core::pin::Pin;
91100

92101
pub use ffi::QQmlApplicationEngine;
@@ -101,4 +110,14 @@ impl QQmlApplicationEngine {
101110
pub fn new() -> cxx::UniquePtr<Self> {
102111
ffi::qqmlapplicationengine_new()
103112
}
113+
114+
pub fn singleton_instance<T>(self: Pin<&mut Self>, uri: QAnyStringView, type_name: QAnyStringView) -> Option<&mut T> {
115+
unsafe {
116+
let ptr = ffi::qqmlapplicationengine_singleton_instance(self, uri, type_name);
117+
if ptr.is_null() {
118+
return None;
119+
}
120+
Some(&mut *(ptr as *mut T))
121+
}
122+
}
104123
}

0 commit comments

Comments
 (0)