Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples: add basic widgets example #622

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions crates/cxx-qt-lib-extras/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ fn main() {
"core/qcommandlineoption",
"core/qcommandlineparser",
"gui/qapplication",
"gui/qpushbutton",
];

for rust_source in &rust_bridges {
Expand All @@ -68,6 +69,7 @@ fn main() {
"core/qcommandlineoption",
"core/qcommandlineparser",
"gui/qapplication",
"gui/qpushbutton",
];

builder = builder.cc_builder(move |cc| {
Expand Down
10 changes: 10 additions & 0 deletions crates/cxx-qt-lib-extras/include/gui/qpushbutton.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// clang-format off
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
// clang-format on
// SPDX-FileContributor: Andrew Hayzen <[email protected]>
//
// SPDX-License-Identifier: MIT OR Apache-2.0

#pragma once

#include <QtWidgets/QPushButton>
3 changes: 3 additions & 0 deletions crates/cxx-qt-lib-extras/src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@

mod qapplication;
pub use qapplication::QApplication;

mod qpushbutton;
pub use qpushbutton::QPushButton;
8 changes: 8 additions & 0 deletions crates/cxx-qt-lib-extras/src/gui/qpushbutton.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// clang-format off
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
// clang-format on
// SPDX-FileContributor: Andrew Hayzen <[email protected]>
//
// SPDX-License-Identifier: MIT OR Apache-2.0

#include "cxx-qt-lib-extras/qpushbutton.h"
53 changes: 53 additions & 0 deletions crates/cxx-qt-lib-extras/src/gui/qpushbutton.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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

#[cxx_qt::bridge]
mod ffi {
unsafe extern "C++" {
include!("cxx-qt-lib/qstring.h");
type QString = cxx_qt_lib::QString;
}

unsafe extern "C++Qt" {
include!("cxx-qt-lib-extras/qpushbutton.h");

#[qobject]
type QPushButton;

// TODO: we should use upcasting methods here and implement QAbstractButton and QWidget
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should figure out how upcasting is going to work, as otherwise we are going to duplicate a load of methods in the widgets world

// so that we don't need to duplicate all of the methods

/// This signal is emitted when the button is activated (i.e., pressed down then released while the mouse cursor is inside the button)
#[qsignal]
fn clicked(self: Pin<&mut QPushButton>, checked: bool);

/// Set the text shown on the button
#[rust_name = "set_text"]
fn setText(self: Pin<&mut QPushButton>, text: &QString);

/// Shows the widget and its child widgets.
fn show(self: Pin<&mut QPushButton>);
}

#[namespace = "rust::cxxqtlib1"]
unsafe extern "C++Qt" {
include!("cxx-qt-lib/common.h");

#[doc(hidden)]
#[rust_name = "qpushbutton_init_default"]
fn make_unique() -> UniquePtr<QPushButton>;
}
}

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()
}
}

pub use ffi::QPushButton;
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() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should have some kind of test as well, even if C++ to see if this works, unsure how so far....

let mut app = QApplication::new();

// TODO: we should really pass a parent in here
let mut push_button = QPushButton::new();
Comment on lines +14 to +15
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use UniquePtr or should we use raw pointer and set a parent like Qt, or should we support both?


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();
}
}
Loading