Skip to content

Add AsQobject trait #1262

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

BenFordTytherington
Copy link
Collaborator

A trait which will wrap the QObjectExt methods for types which upcast to QObject. When used in conjunction with #1252 and #1226, this will allow any type that inherits from QObject - even if this is through many levels of inheritance - to have an as_qobject method, and if the trait is imported, expose the methods of QObject directly on that child class. Closes #562.

Copy link

codecov bot commented May 2, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (5d3b2de) to head (b70840f).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1262   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           73        73           
  Lines        12634     12634           
=========================================
  Hits         12634     12634           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment on lines +131 to +140
pub trait AsObject {
/// Cast self reference into a [QObject] reference
fn as_qobject(&self) -> &QObject;

/// Cast mutable reference into a mutable [QObject] reference
fn as_qobject_mut(&mut self) -> &mut QObject;

/// Cast pinned mutable reference into a pinned mutable [QObject] reference
fn as_qobject_pin(self: Pin<&mut Self>) -> Pin<&mut QObject>;
}
Copy link
Contributor

@jnbooth jnbooth May 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be named AsQObject? Since there should never be a plain &mut to a QObject (they should always be pinned), I think it might make more sense to have something like this:

pub trait AsQObject {
    /// Cast self reference into a [QObject] reference
    fn as_qobject(&self) -> &QObject;

    /// Cast pinned mutable reference into a pinned mutable [QObject] reference
    fn as_qobject_mut(self: Pin<&mut Self>) -> Pin<&mut QObject>;
}

I think it's good practice to use "mut" in function names wherever mutable borrowing/casting is involved. myobj.as_qobject_mut().do_something() immediately reads to me as "this is using a mutable QObject method". myobj.as_qobject_pin().do_something() seems less clear. Pin<&mut QObject> is the only possible mutable reference to a QObject, so it makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Traits for CxxQtType/QtObject
2 participants