-
Notifications
You must be signed in to change notification settings - Fork 77
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,6 @@ | |
|
||
mod qapplication; | ||
pub use qapplication::QApplication; | ||
|
||
mod qpushbutton; | ||
pub use qpushbutton::QPushButton; |
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" |
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 | ||
// 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; |
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" ] } |
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(); | ||
} |
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} |
There was a problem hiding this comment.
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