Scene Graph - Custom Geometry example partially ported to cxx-qt
#1270
Replies: 1 comment 5 replies
-
Hi @OlivierLDff , great work and an interesting read, thank you for showing this here. Your use-case is one of the exact reasons why we want to be a superset of CXX. Something like the QSG* API may never be part of "official" CXX-Qt, as it's a (somewhat) niche API with a ton of complexity and little value to wrapping it in Rust. You're right that more documentation on this would be useful (I'm happy to accept PRs on this if you're willing to contribute :) ). Regarding the slow compilation: Are you using CXX-Qt 0.7.x or the main branch? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Scene Graph - Custom Geometry
Hi I've been experimenting cxx-qt for
QQuickItem
subclassing, I want to share my experience and give you some hint of what struggle I ran into.TDLR:
updatePaintNode
in cpp (and show that it is easy not to go full rust and escape hatch are everywhere). Anyway doing bindings to have 1:1 code won't add any value to the codebase.QQuickItem
and inherited functions with types that are not available incxx-qt
.Note
This readme is a based on the original example.
cxx-qt
.cxx
and followed the tutorials in the cxx-qt documentation.The objective:
Prerequisite
You have a minimal
cxx-qt
project withbuild.rs
, amain.rs
and amain.qml
.BezierCurve declaration
In
src/bezier_curve.rs
, we declare theBezierCurve
class. This class is aQQuickItem
derived class.As of version
0.7.x
ofcxx-qt
, noQQuickItem
exists. This is why we import#include!(<QtQuick/QQuickItem>);
in theunsafe extern "C++"
block. This is a workaround untilcxx-qt
supportsQQuickItem
directly.By itself it won't compile, because the constructor of
QQuickItem
expects aQQuickItem*
and not aQObject*
.This can be dealt with in different ways, either using
cxx_qt::Constructor
trait orcxx_qt::Initialize
trait. More can be found in the Traits section.We will go with
cxx_qt::Initialize
trait, because we also need to initialize some stuff:ItemHasContents
flagp1
,p2
,p3
,p4
andsegment_count
properties to theupdate()
method of theQQuickItem
.And here we start to hit the limitations of
cxx-qt
. But it's ok we can deal with it ourself. We need to be able to call:set_flag
method ofQQuickItem
to set theItemHasContents
flag and access the associated enum.update()
method ofQQuickItem
to update the item when the properties change.All the code shouldn't be required anymore if
cxx-qt
supportsQQuickItem
directly and with the newupcast
feature they are working on.So far so good, we use the
inherit
attribute to inherit thesetFlag
andupdate
methods fromQQuickItem
. This allows us to call them directly on ourBezierCurve
object. The correct way in the future will be to upcast ourBezierCurve
object toQQuickItem
and call the methods on it.But what about
QQuickItemFlag
that I wrote? In c++ this isQQuickItem::Flag
, but we can't directly access it in rust. I followed what I've seen in thecxx-qt
codebase, and used thecxx documentation about extern enum
. We want to use an enum that already exists in c++ world, not create a new one.So in
cpp/cxxqtlib1_qquickitem.h
we have:The namespace doesn't matter, but I used the one
cxx-qt
uses in it's codebase. Then let's use it in our code:Of course don't forget to update
build.rs
to include the new header file:Overriding the paint method
From now on, I will take a shortcut of not porting the whole
updatePaintNode
method to rust, as this is no longer acxx-qt
usage, but more acxx
usage. Starting to work on bindings for the wholeQSG*
family of classes is out of scope of this example.I believe the
cxx-qt
could do gradual work on adding new API, but I guess a lot of discussion should go on because they are many way to approach the problem (as with any bindings library).Let's override the
updatePaintNode
method ofQQuickItem
to use our ownBezierCurve
class. Note that you will seeQQuickItemUpdatePaintNodeData
in the code, I used the same alias technique as forQQuickItemFlag
.And
beziercurve.h
mostly taken from the original example.And there we should have everything!
Some general questions I had:
include!("cxx-qt-lib/qpointf.h")
is required.cxx-qt
(ie what I'm doing, with the enum trick etc…)cxx-qt
.quick
feature? So we could have at least bindings forQQuickItem
.Thank you for the good work.
Beta Was this translation helpful? Give feedback.
All reactions