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 @@ -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
10 changes: 10 additions & 0 deletions crates/cxx-qt-lib-extras-headers/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 <info@kdab.com>
// clang-format on
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0

#pragma once

#include <QtWidgets/QPushButton>
4 changes: 4 additions & 0 deletions crates/cxx-qt-lib-extras-headers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ pub fn build_opts() -> cxx_qt_build::CxxQtBuildersOpts {
include_str!("../include/gui/qapplication.h"),
"qapplication.h",
),
(
include_str!("../include/gui/qpushbutton.h"),
"qpushbutton.h",
),
] {
opts = opts.header(file_contents, "cxx-qt-lib-extras", file_name);
}
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 @@ -13,6 +13,7 @@ fn main() {
"core/qcommandlineoption",
"core/qcommandlineparser",
"gui/qapplication",
"gui/qpushbutton",
];

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

builder = builder.cc_builder(move |cc| {
Expand Down
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 <info@kdab.com>
// clang-format on
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0

#include "cxx-qt-lib-extras/qpushbutton.h"
54 changes: 54 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,54 @@
// 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

#[cxx_qt::bridge(cxx_file_stem = "qpushbutton")]
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]
#[allow(dead_code)]
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;
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() {
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();

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