diff --git a/crates/cxx-qt-lib/src/gui/qpainter.rs b/crates/cxx-qt-lib/src/gui/qpainter.rs index 3e69417a6..789053955 100644 --- a/crates/cxx-qt-lib/src/gui/qpainter.rs +++ b/crates/cxx-qt-lib/src/gui/qpainter.rs @@ -3,6 +3,8 @@ // // SPDX-License-Identifier: MIT OR Apache-2.0 +use core::pin::Pin; + #[cxx::bridge] mod ffi { #[namespace = "Qt"] @@ -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. @@ -415,4 +417,8 @@ impl QPainter { None } } + + pub fn set_pen(self: Pin<&mut ffi::QPainter>, pen: &impl AsRef) { + self.set_pen_from_pen(pen.as_ref()); + } } diff --git a/crates/cxx-qt-lib/src/gui/qpen.rs b/crates/cxx-qt-lib/src/gui/qpen.rs index 131707d8e..d67721b54 100644 --- a/crates/cxx-qt-lib/src/gui/qpen.rs +++ b/crates/cxx-qt-lib/src/gui/qpen.rs @@ -180,6 +180,19 @@ impl fmt::Display for QPen { } } +impl AsRef for ffi::QColor { + fn as_ref(&self) -> &ffi::QPen { + let pen = ffi::qpen_init_from_qcolor(&self); + pen + } +} + +impl Into 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. diff --git a/examples/qml_features/rust/src/custom_parent_class.rs b/examples/qml_features/rust/src/custom_parent_class.rs index 4c1a9a049..a5080bf55 100644 --- a/examples/qml_features/rust/src/custom_parent_class.rs +++ b/examples/qml_features/rust/src/custom_parent_class.rs @@ -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(),