-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cxx-qt-gen: add support for choosing a custom parent class
This then allows you to create a QQuickItem such as a QQuickPaintedItem. Closes #474
- Loading branch information
1 parent
ac33def
commit 39b8e44
Showing
14 changed files
with
171 additions
and
9 deletions.
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
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
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
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// SPDX-FileContributor: Andrew Hayzen <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
import QtQuick 2.12 | ||
import QtQuick.Controls 2.12 | ||
import QtQuick.Layouts 1.12 | ||
|
||
import com.kdab.cxx_qt.demo 1.0 | ||
|
||
Page { | ||
header: ToolBar { | ||
RowLayout { | ||
anchors.fill: parent | ||
|
||
ToolButton { | ||
text: qsTr("Red") | ||
|
||
onClicked: customPainter.color = "red" | ||
} | ||
|
||
ToolButton { | ||
text: qsTr("Green") | ||
|
||
onClicked: customPainter.color = "green" | ||
} | ||
|
||
ToolButton { | ||
text: qsTr("Blue") | ||
|
||
onClicked: customPainter.color = "blue" | ||
} | ||
|
||
Item { | ||
Layout.fillWidth: true | ||
} | ||
} | ||
} | ||
|
||
|
||
ColumnLayout { | ||
anchors.left: parent.left | ||
anchors.right: parent.right | ||
anchors.verticalCenter: parent.verticalCenter | ||
|
||
CustomParentClass { | ||
id: customPainter | ||
color: "red" | ||
Layout.alignment: Qt.AlignHCenter | ||
height: 200 | ||
width: 200 | ||
|
||
// TODO: once we can connect to signals on the Rust side, this could be done there | ||
onColorChanged: update() | ||
} | ||
|
||
Label { | ||
Layout.fillWidth: true | ||
horizontalAlignment: Text.AlignHCenter | ||
text: qsTr("In this demo the Rectangle is rendered in Rust by implementing a QQuickPaintedItem.") | ||
wrapMode: Text.Wrap | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// SPDX-FileCopyrightText: 2023 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(cxx_file_stem = "custom_parent_class")] | ||
mod ffi { | ||
unsafe extern "C++" { | ||
include!(<QtQuick/QQuickPaintedItem>); | ||
|
||
type QColor = cxx_qt_lib::QColor; | ||
include!("cxx-qt-lib/qcolor.h"); | ||
|
||
type QRectF = cxx_qt_lib::QRectF; | ||
include!("cxx-qt-lib/qrectf.h"); | ||
|
||
type QSizeF = cxx_qt_lib::QSizeF; | ||
include!("cxx-qt-lib/qsizef.h"); | ||
|
||
// Define the API from QPainter that we need | ||
type QPainter; | ||
include!(<QtGui/QPainter>); | ||
|
||
#[rust_name = "fill_rect"] | ||
fn fillRect(self: Pin<&mut QPainter>, rectangle: &QRectF, color: &QColor); | ||
} | ||
|
||
#[cxx_qt::qobject( | ||
base = "QQuickPaintedItem", | ||
parent = "QQuickItem", | ||
qml_uri = "com.kdab.cxx_qt.demo", | ||
qml_version = "1.0" | ||
)] | ||
#[derive(Default)] | ||
pub struct CustomParentClass { | ||
#[qproperty] | ||
color: QColor, | ||
} | ||
|
||
// Define that we need to inherit size() from the base class | ||
#[cxx_qt::inherit] | ||
unsafe extern "C++" { | ||
fn size(self: &qobject::CustomParentClass) -> QSizeF; | ||
} | ||
|
||
impl qobject::CustomParentClass { | ||
#[qinvokable(cxx_override)] | ||
pub fn paint(self: Pin<&mut Self>, painter: *mut QPainter) { | ||
// We need to convert the *mut QPainter to a Pin<&mut QPainter> so that we can reach the methods | ||
if let Some(painter) = unsafe { painter.as_mut() } { | ||
let mut pinned_painter = unsafe { Pin::new_unchecked(painter) }; | ||
|
||
// Now pinned painter can be used as normal | ||
// to render a rectangle with two colours | ||
let size = self.as_ref().size(); | ||
pinned_painter.as_mut().fill_rect( | ||
&QRectF::new(0.0, 0.0, size.width() / 2.0, size.height()), | ||
self.as_ref().color(), | ||
); | ||
let darker_color = self.as_ref().color().darker(150); | ||
pinned_painter.as_mut().fill_rect( | ||
&QRectF::new(size.width() / 2.0, 0.0, size.width() / 2.0, size.height()), | ||
&darker_color, | ||
); | ||
} | ||
} | ||
} | ||
} |
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