Skip to content

Commit

Permalink
Remove dependency to QByteArray from QImage
Browse files Browse the repository at this point in the history
Makes the whole computation faster, as we can avoid an additional copy
of the image.
  • Loading branch information
LeonMatthesKDAB committed Nov 17, 2023
1 parent 856c011 commit aaee74e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
8 changes: 2 additions & 6 deletions book/src/tutorial/qt-gui/rust-implementation.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ impl cxx_qt::Initialize for qobject::ImagePainter {
✅ Check that you can successfully print "Hello world from CXX-Qt!" by selecting a file path or changing the image filter

Before we implement the image loading and filtering, we'll need some additional types:
* QByteArray (and the QByteArrayCursor helper)
* QSizeF and QRectF to calculate the size for painting
Before we implement the image loading and filtering, we'll need to import QSizeF and QRectF to calculate the size for painting.

✅ Import these types from cxx-qt-lib

Expand Down Expand Up @@ -179,14 +177,12 @@ mod qobject {

Documentation that may be useful:
* [CXX-Qt-lib](https://docs.rs/cxx-qt-lib/latest/cxx_qt_lib/) for using Qt types from Rust
* [rustagram2](https://docs.rs/rustagram2/latest/rustagram/)
* Click on the Re-export of the `image` crate to learn more about how to use the resulting image.
* [Rust standard library documentation](https://doc.rust-lang.org/std/index.html)

✅ Add threading in Rust via [`std::thread::spawn`](https://doc.rust-lang.org/std/thread/fn.spawn.html) to load and convert the image in the background
* Check out the [CXX-Qt book][CXX-Qt book] to learn more about Threading

✅ Add a `BusyIndicator` to QML to show that the background thread is wthat the background thread is waiting.
✅ Add a `BusyIndicator` to QML to show that the background thread is waiting.

The resulting application should look like this:

Expand Down
14 changes: 7 additions & 7 deletions crates/with-workspace/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions crates/with-workspace/qt-gui/src/image_painter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cxx_qt::{CxxQtType, Threading};
use cxx_qt_lib::{QByteArray, QColor, QImage, QRectF, QString, QUrl};
use cxx_qt_lib::{QColor, QImage, QRectF, QString, QUrl};
use image_manipulation::apply_filter;
use rustagram::FilterType;
use std::{error::Error, pin::Pin};
Expand Down Expand Up @@ -118,9 +118,8 @@ impl qobject::ImagePainter {
let image = std::fs::read(path)?;
let image = apply_filter(&image, filter);

let bytes = QByteArray::from(image.as_slice());

QImage::from_data(&bytes, "PNG").ok_or("Failed to convert to QImage!".into())
QImage::from_data(image.as_slice(), Some("PNG"))
.ok_or("Failed to convert to QImage!".into())
}

fn load_file(mut self: Pin<&mut Self>) {
Expand Down

0 comments on commit aaee74e

Please sign in to comment.