Skip to content

Commit 7c76ab4

Browse files
committed
Add wrapper around qmlRegisterModule function (#220)
This is a rarely used feature, but is still needed in some corner-cases. Closes #219
1 parent 62ccad7 commit 7c76ab4

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

qmetaobject/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ pub mod webengine;
216216
pub mod prelude {
217217
#[cfg(qt_5_8)]
218218
pub use crate::qtdeclarative::qml_register_enum;
219+
#[cfg(qt_5_9)]
220+
pub use crate::qtdeclarative::qml_register_module;
219221
pub use crate::{
220222
qml_register_type, qrc, qt_base_class, qt_method, qt_plugin, qt_property, qt_signal,
221223
QAbstractListModel, QByteArray, QColor, QDate, QDateTime, QEnum, QModelIndex, QObject,

qmetaobject/src/qtdeclarative.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,32 @@ pub fn qml_register_type<T: QObject + Default + Sized>(
502502
})
503503
}
504504

505+
/// Wrapper around [`void qmlRegisterModule(const char *uri, int versionMajor, int versionMinor)`][qt] function.
506+
///
507+
/// [qt]: https://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterModule
508+
#[cfg(qt_5_9)]
509+
pub fn qml_register_module(
510+
uri: &CStr,
511+
version_major: u32,
512+
version_minor: u32,
513+
) {
514+
let uri_ptr = uri.as_ptr();
515+
516+
cpp!(unsafe [
517+
uri_ptr as "const char *",
518+
version_major as "int",
519+
version_minor as "int"
520+
] {
521+
#if QT_VERSION >= QT_VERSION_CHECK(5,9,0)
522+
qmlRegisterModule(
523+
uri_ptr,
524+
version_major,
525+
version_minor
526+
);
527+
#endif
528+
});
529+
}
530+
505531
/// Alias for type of `QQmlPrivate::RegisterSingletonType::qobjectApi` callback
506532
/// and its C++ counterpart.
507533
type QmlRegisterSingletonTypeCallback =

qmetaobject/tests/tests.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,26 @@ fn register_type() {
200200
));
201201
}
202202

203+
#[test]
204+
#[cfg(qt_5_9)]
205+
fn register_module() {
206+
qml_register_module(CStr::from_bytes_with_nul(b"TestEmptyModule\0").unwrap(), 1, 2);
207+
208+
let obj = MyObject::default(); // not used but needed for do_test
209+
assert!(do_test(
210+
obj,
211+
r"
212+
import TestEmptyModule 1.2 as TM
213+
214+
QtObject {
215+
function doTest() {
216+
return typeof TM === 'object';
217+
}
218+
}
219+
"
220+
));
221+
}
222+
203223
#[derive(Default, QObject)]
204224
struct RegisterSingletonInstanceObj {
205225
base: qt_base_class!(trait QObject),

0 commit comments

Comments
 (0)