From db5149232cc05d9d36d331e316ab03b1e07d5800 Mon Sep 17 00:00:00 2001 From: Andrew Hayzen Date: Tue, 25 Jul 2023 15:04:42 +0100 Subject: [PATCH] examples: add basic widgets example --- Cargo.toml | 1 + .../cxx-qt-lib-extras/src/gui/qpushbutton.rs | 3 ++ examples/widgets_minimal/Cargo.toml | 26 ++++++++++++++ examples/widgets_minimal/build.rs | 13 +++++++ examples/widgets_minimal/src/main.rs | 35 +++++++++++++++++++ 5 files changed, 78 insertions(+) create mode 100644 examples/widgets_minimal/Cargo.toml create mode 100644 examples/widgets_minimal/build.rs create mode 100644 examples/widgets_minimal/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index ae34f3637..f0eb6395e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ members = [ "examples/demo_threading/rust", "examples/qml_features/rust", "examples/qml_minimal/rust", + "examples/widgets_minimal", "tests/basic_cxx_only/rust", "tests/basic_cxx_qt/rust", diff --git a/crates/cxx-qt-lib-extras/src/gui/qpushbutton.rs b/crates/cxx-qt-lib-extras/src/gui/qpushbutton.rs index 16c6573b1..362a7a617 100644 --- a/crates/cxx-qt-lib-extras/src/gui/qpushbutton.rs +++ b/crates/cxx-qt-lib-extras/src/gui/qpushbutton.rs @@ -42,6 +42,9 @@ mod ffi { } impl ffi::QPushButton { + // TODO: we need to think about how ownership in widgets is going to work + // as for items like QPushButton you may have a parent and have that + // free the object and not when the unique ptr goes out of scope pub fn new() -> cxx::UniquePtr { ffi::qpushbutton_init_default() } diff --git a/examples/widgets_minimal/Cargo.toml b/examples/widgets_minimal/Cargo.toml new file mode 100644 index 000000000..1f7d84041 --- /dev/null +++ b/examples/widgets_minimal/Cargo.toml @@ -0,0 +1,26 @@ +# SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company +# SPDX-FileContributor: Andrew Hayzen +# +# SPDX-License-Identifier: MIT OR Apache-2.0 + +[package] +name = "widgets-minimal-no-cmake" +version = "0.1.0" +authors = [ + "Andrew Hayzen ", + "Be Wilson ", + "Gerhard de Clercq ", + "Leon Matthes " +] +edition = "2021" +license = "MIT OR Apache-2.0" + +[dependencies] +cxx.workspace = true +cxx-qt.workspace = true +cxx-qt-lib.workspace = true +cxx-qt-lib-extras.workspace = true + +[build-dependencies] +# The link_qt_object_files feature is required for statically linking Qt 6. +cxx-qt-build = { workspace = true, features = [ "link_qt_object_files" ] } diff --git a/examples/widgets_minimal/build.rs b/examples/widgets_minimal/build.rs new file mode 100644 index 000000000..cebad04ff --- /dev/null +++ b/examples/widgets_minimal/build.rs @@ -0,0 +1,13 @@ +// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 + +use cxx_qt_build::CxxQtBuilder; + +fn main() { + CxxQtBuilder::new() + // Link Qt's Network library + .qt_module("Network") + .build(); +} diff --git a/examples/widgets_minimal/src/main.rs b/examples/widgets_minimal/src/main.rs new file mode 100644 index 000000000..635088cbe --- /dev/null +++ b/examples/widgets_minimal/src/main.rs @@ -0,0 +1,35 @@ +// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 + +//! This example provides demostrations of building a Qt Widgets CXX-Qt application + +use cxx_qt_lib::QString; +use cxx_qt_lib_extras::{QApplication, QPushButton}; + +fn main() { + let mut app = QApplication::new(); + + // TODO: we should really pass a parent in here + let mut push_button = QPushButton::new(); + + if let Some(mut push_button) = push_button.as_mut() { + push_button + .as_mut() + .set_text(&QString::from("Hello World!")); + + push_button + .as_mut() + .on_clicked(|_, _| { + println!("Button Clicked!"); + }) + .release(); + + push_button.show(); + } + + if let Some(app) = app.as_mut() { + app.exec(); + } +}