Skip to content

Traits for CxxQtType/QtObject #562

@LeonMatthesKDAB

Description

@LeonMatthesKDAB

We could move some of our "magic" methods into traits, which would make it a lot more obvious where they are coming from, and would allow us to e.g. "inherit" methods from QObject, reason about types, etc.

// cxx-qt crate - may also need to go in cxx-qt-lib
mod ffi {
    extern "C++" {
        type QObject;

        fn children(self: &QObject) -> QList<*mut QObject>;
        fn objectName(self: &QObject) -> QString;
    }
}

// Note: QtObject vs QObject
trait QtObject {
    fn as_qobject(&self) -> &cxx_qt_lib::QObject;
    fn as_qobject_mut(&mut self) -> Pin<&mut cxx_qt_lib::QObject>;

    fn children(&self) -> QList<*mut QObject> {
        as_qobject().children()
    }

    fn objectName(&self) -> Pin<&mut cxx_qt_lib::QObject> {
        as_qobject().objectName()
    }

    // ... other methods wrapped as needed.
}

trait CxxQtType {
    type Inner;

    fn rust(&self) -> &Self::Inner;
    fn rust_mut(self: Pin<&mut self>) -> &mut Self::Inner;
}

Then in C++, we could generate a generic cast to QObject, similar to how we do constructors.

template<typename T>
QObject *as_qobject(T* t) {
    return static_cast<QObject*>(t);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    ⬆️ featureNew feature or request👷 refactorSomething needs to change🥳🎉 1.0This issue is part of stabilization for 1.0 release

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions