From aaee74e7d789e9f433a772f017857c99cda61ce1 Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Fri, 17 Nov 2023 15:30:35 +0100 Subject: [PATCH] Remove dependency to QByteArray from QImage Makes the whole computation faster, as we can avoid an additional copy of the image. --- book/src/tutorial/qt-gui/rust-implementation.md | 8 ++------ crates/with-workspace/Cargo.lock | 14 +++++++------- crates/with-workspace/qt-gui/src/image_painter.rs | 7 +++---- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/book/src/tutorial/qt-gui/rust-implementation.md b/book/src/tutorial/qt-gui/rust-implementation.md index 365fb00..9e5b50a 100644 --- a/book/src/tutorial/qt-gui/rust-implementation.md +++ b/book/src/tutorial/qt-gui/rust-implementation.md @@ -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 @@ -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: diff --git a/crates/with-workspace/Cargo.lock b/crates/with-workspace/Cargo.lock index 08c1909..d45d1bc 100644 --- a/crates/with-workspace/Cargo.lock +++ b/crates/with-workspace/Cargo.lock @@ -221,7 +221,7 @@ dependencies = [ [[package]] name = "cxx-qt" version = "0.5.3" -source = "git+https://github.com/LeonMatthesKDAB/cxx-qt?branch=qimage#6491401796bcb220d1aa2120bc3c044e7a3dca91" +source = "git+https://github.com/LeonMatthesKDAB/cxx-qt?branch=qimage#c6a70963aa027dae8c6ae3091d5e9ddb1f4ab1a5" dependencies = [ "cxx", "cxx-qt-macro", @@ -231,7 +231,7 @@ dependencies = [ [[package]] name = "cxx-qt-build" version = "0.5.3" -source = "git+https://github.com/LeonMatthesKDAB/cxx-qt?branch=qimage#6491401796bcb220d1aa2120bc3c044e7a3dca91" +source = "git+https://github.com/LeonMatthesKDAB/cxx-qt?branch=qimage#c6a70963aa027dae8c6ae3091d5e9ddb1f4ab1a5" dependencies = [ "cc", "codespan-reporting", @@ -248,7 +248,7 @@ dependencies = [ [[package]] name = "cxx-qt-gen" version = "0.5.3" -source = "git+https://github.com/LeonMatthesKDAB/cxx-qt?branch=qimage#6491401796bcb220d1aa2120bc3c044e7a3dca91" +source = "git+https://github.com/LeonMatthesKDAB/cxx-qt?branch=qimage#c6a70963aa027dae8c6ae3091d5e9ddb1f4ab1a5" dependencies = [ "clang-format", "convert_case", @@ -261,7 +261,7 @@ dependencies = [ [[package]] name = "cxx-qt-lib" version = "0.5.3" -source = "git+https://github.com/LeonMatthesKDAB/cxx-qt?branch=qimage#6491401796bcb220d1aa2120bc3c044e7a3dca91" +source = "git+https://github.com/LeonMatthesKDAB/cxx-qt?branch=qimage#c6a70963aa027dae8c6ae3091d5e9ddb1f4ab1a5" dependencies = [ "cxx", "cxx-build", @@ -272,12 +272,12 @@ dependencies = [ [[package]] name = "cxx-qt-lib-headers" version = "0.5.3" -source = "git+https://github.com/LeonMatthesKDAB/cxx-qt?branch=qimage#6491401796bcb220d1aa2120bc3c044e7a3dca91" +source = "git+https://github.com/LeonMatthesKDAB/cxx-qt?branch=qimage#c6a70963aa027dae8c6ae3091d5e9ddb1f4ab1a5" [[package]] name = "cxx-qt-macro" version = "0.5.3" -source = "git+https://github.com/LeonMatthesKDAB/cxx-qt?branch=qimage#6491401796bcb220d1aa2120bc3c044e7a3dca91" +source = "git+https://github.com/LeonMatthesKDAB/cxx-qt?branch=qimage#c6a70963aa027dae8c6ae3091d5e9ddb1f4ab1a5" dependencies = [ "cxx-qt-gen", "proc-macro2", @@ -587,7 +587,7 @@ dependencies = [ [[package]] name = "qt-build-utils" version = "0.5.3" -source = "git+https://github.com/LeonMatthesKDAB/cxx-qt?branch=qimage#6491401796bcb220d1aa2120bc3c044e7a3dca91" +source = "git+https://github.com/LeonMatthesKDAB/cxx-qt?branch=qimage#c6a70963aa027dae8c6ae3091d5e9ddb1f4ab1a5" dependencies = [ "cc", "thiserror", diff --git a/crates/with-workspace/qt-gui/src/image_painter.rs b/crates/with-workspace/qt-gui/src/image_painter.rs index 62ab31e..73cc892 100644 --- a/crates/with-workspace/qt-gui/src/image_painter.rs +++ b/crates/with-workspace/qt-gui/src/image_painter.rs @@ -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}; @@ -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>) {