Skip to content
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

cxx-qt-lib: use cxx_qt::bridge and #[qsignal] #840

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions crates/cxx-qt-lib-headers/include/qml/qqmlapplicationengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
#include <memory>

#include <QtQml/QQmlApplicationEngine>
#include <QtQml/QQmlEngine>

namespace rust {
namespace cxxqtlib1 {

::std::unique_ptr<QQmlApplicationEngine>
qqmlapplicationengineNew();

QQmlEngine&
qqmlapplicationengineAsQQmlEngine(QQmlApplicationEngine&);

}
}

Expand Down
6 changes: 6 additions & 0 deletions crates/cxx-qt-lib/src/qml/qqmlapplicationengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,11 @@ qqmlapplicationengineNew()
return ::std::make_unique<QQmlApplicationEngine>();
}

QQmlEngine&
qqmlapplicationengineAsQQmlEngine(QQmlApplicationEngine& engine)
{
return static_cast<QQmlEngine&>(engine);
}

}
}
19 changes: 19 additions & 0 deletions crates/cxx-qt-lib/src/qml/qqmlapplicationengine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,22 @@ mod ffi {
fn setOfflineStoragePath(self: Pin<&mut QQmlApplicationEngine>, dir: &QString);
}

unsafe extern "C++" {
include!("cxx-qt-lib/qqmlengine.h");
type QQmlEngine = crate::QQmlEngine;
}

#[namespace = "rust::cxxqtlib1"]
unsafe extern "C++" {
#[doc(hidden)]
#[rust_name = "qqmlapplicationengine_new"]
fn qqmlapplicationengineNew() -> UniquePtr<QQmlApplicationEngine>;

#[doc(hidden)]
#[rust_name = "qqmlapplicationengine_as_qqmlengine"]
fn qqmlapplicationengineAsQQmlEngine(
ptr: Pin<&mut QQmlApplicationEngine>,
) -> Pin<&mut QQmlEngine>;
}

// QQmlApplicationEngine is not a trivial to CXX and is not relocatable in Qt
Expand All @@ -75,9 +86,17 @@ mod ffi {
impl UniquePtr<QQmlApplicationEngine> {}
}

use crate::QQmlEngine;
use core::pin::Pin;

pub use ffi::QQmlApplicationEngine;

impl QQmlApplicationEngine {
/// Convert the existing [QQmlApplicationEngine] to a [QQmlEngine]
pub fn as_qqmlengine<'a>(self: Pin<&'a mut Self>) -> Pin<&'a mut QQmlEngine> {
ffi::qqmlapplicationengine_as_qqmlengine(self)
}

/// Create a new QQmlApplicationEngine
pub fn new() -> cxx::UniquePtr<Self> {
ffi::qqmlapplicationengine_new()
Expand Down
18 changes: 14 additions & 4 deletions crates/cxx-qt-lib/src/qml/qqmlengine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,21 @@
//
// SPDX-License-Identifier: MIT OR Apache-2.0

#[cxx::bridge]
#[cxx_qt::bridge(cxx_file_stem = "qqmlengine")]
mod ffi {
unsafe extern "C++Qt" {
include!("cxx-qt-lib/qqmlengine.h");
type QQmlEngine;

/// This signal is emitted when the QML loaded by the engine would like to exit from the event loop with the specified return code ret_code.
#[qsignal]
fn exit(self: Pin<&mut QQmlEngine>, ret_code: i32);

/// This signal is emitted when the QML loaded by the engine would like to quit.
#[qsignal]
fn quit(self: Pin<&mut QQmlEngine>);
}

unsafe extern "C++" {
include!("cxx-qt-lib/qstring.h");
type QString = crate::QString;
Expand All @@ -13,9 +26,6 @@ mod ffi {
include!("cxx-qt-lib/qurl.h");
type QUrl = crate::QUrl;

include!("cxx-qt-lib/qqmlengine.h");
type QQmlEngine;

/// Adds path as a directory where the engine searches for installed modules in a URL-based directory structure.
#[rust_name = "add_import_path"]
fn addImportPath(self: Pin<&mut QQmlEngine>, path: &QString);
Expand Down
7 changes: 7 additions & 0 deletions examples/cargo_without_cmake/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ fn main() {
engine.load(&QUrl::from("qrc:/qt/qml/com/kdab/cxx_qt/demo/qml/main.qml"));
}

if let Some(engine) = engine.as_mut() {
// Listen to a signal from the QML Engine
engine.as_qqmlengine().on_quit(|_| {
println!("QML Quit!");
});
}

// Start the app
if let Some(app) = app.as_mut() {
app.exec();
Expand Down
6 changes: 6 additions & 0 deletions examples/qml_minimal/qml/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ Window {

onClicked: myObject.sayHi(myObject.string, myObject.number)
}

Button {
text: qsTr("Quit")

onClicked: Qt.quit()
}
}
}
// ANCHOR_END: book_main_qml
Loading