-
Notifications
You must be signed in to change notification settings - Fork 66
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
ahayzen-kdab
wants to merge
2
commits into
KDAB:main
Choose a base branch
from
ahayzen-kdab:577-extern-cpp-qt-block
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
crates/cxx-qt-lib-extras-headers/include/gui/qpushbutton.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
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(); | ||
|
||
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(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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