Skip to content

Support QFlags #1231

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

Merged
merged 15 commits into from
Mar 25, 2025
Merged

Support QFlags #1231

merged 15 commits into from
Mar 25, 2025

Conversation

jnbooth
Copy link
Contributor

@jnbooth jnbooth commented Mar 17, 2025

This PR defines a QFlags<T: QFlag> type to address the non-codegen parts of #1123. It mimics the API of Qt's own QFlags, including operators. The one exception is that because Rust doesn't have the ~ operator, the ~ operator in Qt is mapped to ! in Rust, while the ! operator in Qt is mapped to QFlags::is_empty in Rust.

The backing QFlag trait provides two things: a function for converting from the FFI enum type to its underlying representation (i.e. value.repr) and the TypeId of the corresponding QFlags type. For example:

#[cxx::bridge]
mod ffi {
    #[repr(i32)]
    enum MyFlag { A, B }; 
}
pub use ffi::MyFlag;

unsafe impl QFlag for MyFlag {
    type TypeId = type_id!("QFlags_MyFlag");
    type Repr = i32;
    fn to_repr(self) -> Self::Repr {
        self.repr
    }
}
pub type MyFlags = QFlags<MyFlag>;

It's unsafe because it has the same safety contract as cxx::ExternType::Id. There's also an internal utility macro to replace the above impl block with

impl_qflag!(MyFlag, "QFlags_MyFlag", i32);

Since QFlags can now be defined, this PR also adds QGuiApplication::keyboard_modifiers, QGuiApplication::mouse_buttons, and QGuiApplication::query_keyboard_modifiers,

Closes #1123

Copy link

codecov bot commented Mar 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (50ca5e2) to head (7615883).
Report is 1 commits behind head on main.

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

☔ 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.

Copy link
Collaborator

@LeonMatthesKDAB LeonMatthesKDAB left a comment

Choose a reason for hiding this comment

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

Hi @jnbooth thank you for opening the PR on this.

I left some small comments, the approach itself is solid!

Copy link
Collaborator

@LeonMatthesKDAB LeonMatthesKDAB left a comment

Choose a reason for hiding this comment

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

Excellent work, thank you for the contribution @jnbooth

@LeonMatthesKDAB LeonMatthesKDAB merged commit 24b0d7f into KDAB:main Mar 25, 2025
16 checks passed
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.

QFlags support
2 participants