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

For overloads in cxx-qt-lib use impl AsRef #810

Open
ahayzen-kdab opened this issue Jan 17, 2024 · 1 comment · May be fixed by Montel/cxx-qt#99 or #901
Open

For overloads in cxx-qt-lib use impl AsRef #810

ahayzen-kdab opened this issue Jan 17, 2024 · 1 comment · May be fixed by Montel/cxx-qt#99 or #901
Labels
⬆️ feature New feature or request 🙋 good first issue Good for newcomers

Comments

@ahayzen-kdab
Copy link
Collaborator

Where we need to have an overload in cxx-qt-lib, eg QPainter::setPen can take QColor or QPen or an enum. Accept impl AsRef<QPen> then implement Into from QColor and the enum. Then this should allow Rust code to have any of the types.

There are a few places already where we could have had overloads for QPoint, QSize etc consider doing this in those places.

Note however this will mean we need an extra impl block that then calls the direct CXX binding.

@jnbooth
Copy link
Contributor

jnbooth commented Jan 19, 2025

I don't know if it makes sense for QColor to implement AsRef<QPen>, because a QColor reference is not and cannot be a reference to a QPen. You would need to construct the QPen object inside the AsRef implementation, which seems like misuse of the trait and a high risk of memory management issues.

One alternative would be to use enums, like this:

enum SetPenParam<'a> {
    PenStyle(ffi::PenStyle),
    QColor(&'a QColor),
    QPen(&'a QPen),
}

impl From<ffi::PenStyle> for SetPenParam<'static> {
    fn from(value: ffi::PenStyle) -> Self {
        Self::PenStyle(value)
    }
}
impl<'a> From<&'a QColor> for SetPenParam<'a> {
    fn from(value: &'a QColor) -> Self {
        Self::QColor(value)
    }
}
impl<'a> From<&'a QPen> for SetPenParam<'a> {
    fn from(value: &'a QPen) -> Self {
        Self::QPen(value)
    }
}

impl QPainter {
    pub fn set_pen<'a, T: Into<SetPenParam<'a>>>(self: Pin<&mut QPainter>, pen: T) {
        match pen.into() {
            SetPenParam::PenStyle(style) => ffi::qpainter_set_pen_penstyle(self, style),
            SetPenParam::QColor(color) => ffi::qpainter_set_pen_qcolor(self, color),
            SetPenParam::QPen(pen) => ffi::qpainter_set_pen_qpen(self, pen),
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⬆️ feature New feature or request 🙋 good first issue Good for newcomers
Projects
Status: Todo
2 participants