Skip to content

Commit

Permalink
examples: add basic widgets example
Browse files Browse the repository at this point in the history
  • Loading branch information
ahayzen-kdab committed Oct 8, 2024
1 parent 0c481d0 commit db51492
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions crates/cxx-qt-lib-extras/src/gui/qpushbutton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
ffi::qpushbutton_init_default()
}
Expand Down
26 changes: 26 additions & 0 deletions examples/widgets_minimal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
# SPDX-FileContributor: Andrew Hayzen <[email protected]>
#
# SPDX-License-Identifier: MIT OR Apache-2.0

[package]
name = "widgets-minimal-no-cmake"
version = "0.1.0"
authors = [
"Andrew Hayzen <[email protected]>",
"Be Wilson <[email protected]>",
"Gerhard de Clercq <[email protected]>",
"Leon Matthes <[email protected]>"
]
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" ] }
13 changes: 13 additions & 0 deletions examples/widgets_minimal/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
// SPDX-FileContributor: Andrew Hayzen <[email protected]>
//
// 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();
}
35 changes: 35 additions & 0 deletions examples/widgets_minimal/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
// SPDX-FileContributor: Andrew Hayzen <[email protected]>
//
// 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();
}
}

0 comments on commit db51492

Please sign in to comment.