Skip to content

Commit

Permalink
Update book and example
Browse files Browse the repository at this point in the history
  • Loading branch information
BenFordTytherington authored and ahayzen-kdab committed Oct 28, 2024
1 parent 6f74169 commit 9e4072b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 1 deletion.
8 changes: 7 additions & 1 deletion book/src/bridge/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ For [`#[qproperty]`](./extern_rustqt.md#properties), a CXX or Rust name can be p
> **⚠️ Deprecation warning**:
> CXX-Qt <0.6 did automatic case conversion if no `#[cxx_name = "..."]` or `#[rust_name = "..."]` is specified.
> Starting with CXX-Qt 0.7, this is no longer the case!
> Starting with CXX-Qt 0.7, this is no longer the case! Automatic case conversion will be opt-in instead.
### Automatic case conversion

The `#[auto_cxx_name]` and `#[auto_rust_name]` attributes can be used to automatically rename cxx and rust names.
These are placed at a block level on `extern "RustQt"` or `extern "C++Qt"` blocks, and will automatically case convert the items inside, unless they specify either a `rust_name` or `cxx_name`.
By default `#[auto_cxx_name]` will generate a camelCase conversion for`cxx_name` and `#[auto_rust_name]` will generate a snake_case conversion for `rust_name`.
2 changes: 2 additions & 0 deletions book/src/bridge/extern_cppqt.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ A bridge module may contain zero or more `extern "C++Qt"` blocks.
This complements the [`extern "C++"` CXX section](https://cxx.rs/extern-c++.html)
but allows for declaring Qt specific features on C++ types.

Automatically converting to camel or snake case can be done through an [attribute](./attributes.md#automatic-case-conversion) at the block level.

## `QObject`s

QObject types that are defined in C++ can be made available to Rust, by declaring them as [opaque types](https://cxx.rs/extern-c++.html#opaque-c-types) with a `#[qobject]` attribute.
Expand Down
2 changes: 2 additions & 0 deletions book/src/bridge/extern_rustqt.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ A bridge module may contain zero or more `extern "RustQt"` blocks.
This complements the [`extern "Rust"` CXX section](https://cxx.rs/extern-rust.html)
but allows for declaring Qt specific features on C++ types.

Automatically converting to camel or snake case can be done through an [attribute](./attributes.md#automatic-case-conversion) at the block level.

## `QObject`s

The `#[qobject]` attribute may be placed on a type alias to generate a [`QObject`](https://doc.qt.io/qt-6/qobject.html) type in C++.
Expand Down
7 changes: 7 additions & 0 deletions examples/qml_features/qml/pages/NamingPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,12 @@ Page {
text: qsTr("Increment Counter")
onClicked: renamedObject.increment()
}

Label {
Layout.fillWidth: true
horizontalAlignment: Text.AlignHCenter
text: qsTr("Meaning of life: %1").arg(renamedObject.getNum())
wrapMode: Text.Wrap
}
}
}
11 changes: 11 additions & 0 deletions examples/qml_features/rust/src/naming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ pub mod qobject {
#[rust_name = "plus_one"]
fn increment_number(self: Pin<&mut NamedObject>);
}

#[auto_cxx_name]
unsafe extern "RustQt" {
#[qinvokable]
fn get_num(self: &NamedObject) -> i32;
}
}

use std::pin::Pin;
Expand All @@ -39,4 +45,9 @@ impl qobject::NamedObject {
let previous = *self.num();
self.set_num(previous + 1);
}

/// Method to return a greeting
pub fn get_num(&self) -> i32 {
42
}
}
1 change: 1 addition & 0 deletions examples/qml_features/tests/tst_naming.qml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ TestCase {
compare(obj.numberProp, 0);
obj.increment();
compare(obj.numberProp, 1);
compare(obj.getNum(), 42);
}
}

0 comments on commit 9e4072b

Please sign in to comment.