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 Apr 17, 2024
1 parent e6977cb commit dae15d9
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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 @@ -43,6 +43,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
28 changes: 28 additions & 0 deletions examples/widgets_minimal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
# SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
#
# SPDX-License-Identifier: MIT OR Apache-2.0

[package]
name = "widgets-minimal-no-cmake"
version = "0.1.0"
authors = [
"Andrew Hayzen <andrew.hayzen@kdab.com>",
"Be Wilson <be.wilson@kdab.com>",
"Gerhard de Clercq <gerhard.declercq@kdab.com>",
"Leon Matthes <leon.matthes@kdab.com>"
]
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" ] }
cxx-qt-lib-headers.workspace = true
cxx-qt-lib-extras-headers.workspace = true
15 changes: 15 additions & 0 deletions examples/widgets_minimal/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
//
// 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")
.with_opts(cxx_qt_lib_headers::build_opts())
.with_opts(cxx_qt_lib_extras_headers::build_opts())
.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 <info@kdab.com>
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
//
// 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 dae15d9

Please sign in to comment.