Skip to content

Commit

Permalink
Implement as_ref for overloads in cxx-qt-lib
Browse files Browse the repository at this point in the history
Closed: KDAB#810
  • Loading branch information
Montel committed Mar 20, 2024
1 parent 9aec86e commit 7a96d82
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion crates/cxx-qt-lib/src/gui/qpainter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//
// SPDX-License-Identifier: MIT OR Apache-2.0

use core::pin::Pin;

#[cxx::bridge]
mod ffi {
#[namespace = "Qt"]
Expand Down Expand Up @@ -344,7 +346,7 @@ mod ffi {
fn setOpacity(self: Pin<&mut QPainter>, opacity: f64);

/// Sets the painter's pen to be the given pen.
#[rust_name = "set_pen"]
#[rust_name = "set_pen_from_pen"]
fn setPen(self: Pin<&mut QPainter>, pen: &QPen);

/// Sets the given render hint on the painter if on is true; otherwise clears the render hint.
Expand Down Expand Up @@ -415,4 +417,8 @@ impl QPainter {
None
}
}

pub fn set_pen(self: Pin<&mut ffi::QPainter>, pen: &impl AsRef<ffi::QPen>) {
self.set_pen_from_pen(pen.as_ref());
}
}
13 changes: 13 additions & 0 deletions crates/cxx-qt-lib/src/gui/qpen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,19 @@ impl fmt::Display for QPen {
}
}

impl AsRef<ffi::QPen> for ffi::QColor {
fn as_ref(&self) -> &ffi::QPen {
let pen = ffi::qpen_init_from_qcolor(&self);
pen
}
}

impl Into<QPen> for ffi::QColor {
fn into(self) -> QPen {
ffi::qpen_init_from_qcolor(&self)
}
}

// Safety:
//
// Static checks on the C++ side to ensure the size is the same.
Expand Down
2 changes: 2 additions & 0 deletions examples/qml_features/rust/src/custom_parent_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ impl qobject::CustomParentClass {
// Now pinned painter can be used as normal
// to render a rectangle with two colours
let size = self.as_ref().size();
let color = self.as_ref().color();
pinned_painter.set_pen(color);
pinned_painter.as_mut().fill_rect(
&QRectF::new(0.0, 0.0, size.width() / 2.0, size.height()),
self.as_ref().color(),
Expand Down

0 comments on commit 7a96d82

Please sign in to comment.