init_from_engine now returns the object.

Also added method to get the underlying c++ object. Not sure how well it
actually works since not much of KLocalizedContext is actually supposed
to be used from C++.
This commit is contained in:
Ayush Singh 2021-11-07 12:17:33 +05:30
parent dd88341c4d
commit 6f74d864e3

View file

@ -39,6 +39,8 @@
//! engine.exec(); //! engine.exec();
//! } //! }
//! ``` //! ```
use std::ffi::c_void;
use cpp::{cpp, cpp_class}; use cpp::{cpp, cpp_class};
use qmetaobject::prelude::*; use qmetaobject::prelude::*;
@ -67,12 +69,31 @@ impl KLocalizedContext {
/// use ki18n_rs::KLocalizedContext; /// use ki18n_rs::KLocalizedContext;
/// ///
/// let mut engine = QmlEngine::new(); /// let mut engine = QmlEngine::new();
/// KLocalizedContext::init_from_engine(&engine); /// let context = KLocalizedContext::init_from_engine(&engine);
/// ``` /// ```
pub fn init_from_engine(engine: &QmlEngine) { pub fn init_from_engine(engine: &QmlEngine) -> Self {
let engine_ptr = engine.cpp_ptr(); let engine_ptr = engine.cpp_ptr();
cpp!(unsafe [engine_ptr as "QQmlEngine*"] { cpp!(unsafe [engine_ptr as "QQmlEngine*"] -> KLocalizedContext as "KLocalizedContextHolder" {
engine_ptr->rootContext()->setContextObject(new KLocalizedContext(engine_ptr)); auto klocalized = new KLocalizedContext(engine_ptr);
}); auto klocalizedholder = KLocalizedContextHolder(klocalized);
engine_ptr->rootContext()->setContextObject(klocalized);
return klocalizedholder;
})
}
/// Returns a pointer to the C++ object. The pointer is of the type `KLocalizedContext *` in C++.
/// ```rust
/// use qmetaobject::prelude::*;
/// use ki18n_rs::KLocalizedContext;
///
/// let mut engine = QmlEngine::new();
/// let context = KLocalizedContext::init_from_engine(&engine);
///
/// let context_ptr = context.cpp_ptr();
/// ```
pub fn cpp_ptr(&self) -> *mut c_void {
cpp!(unsafe [self as "KLocalizedContextHolder *"] -> *mut c_void as "KLocalizedContext *" {
return self->klocalized.get();
})
} }
} }