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
QQuickItemsubclassing, I want to share my experience and give you some hint of what struggle I ran into.TDLR:
updatePaintNodein 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.QQuickItemand inherited functions with types that are not available incxx-qt.Note
This readme is a based on the original example.
cxx-qt.cxxand followed the tutorials in the cxx-qt documentation.The objective:
Prerequisite
You have a minimal
cxx-qtproject withbuild.rs, amain.rsand amain.qml.BezierCurve declaration
In
src/bezier_curve.rs, we declare theBezierCurveclass. This class is aQQuickItemderived class.As of version
0.7.xofcxx-qt, noQQuickItemexists. This is why we import#include!(<QtQuick/QQuickItem>);in theunsafe extern "C++"block. This is a workaround untilcxx-qtsupportsQQuickItemdirectly.By itself it won't compile, because the constructor of
QQuickItemexpects aQQuickItem*and not aQObject*.This can be dealt with in different ways, either using
cxx_qt::Constructortrait orcxx_qt::Initializetrait. More can be found in the Traits section.We will go with
cxx_qt::Initializetrait, because we also need to initialize some stuff:ItemHasContentsflagp1,p2,p3,p4andsegment_countproperties 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_flagmethod ofQQuickItemto set theItemHasContentsflag and access the associated enum.update()method ofQQuickItemto update the item when the properties change.All the code shouldn't be required anymore if
cxx-qtsupportsQQuickItemdirectly and with the newupcastfeature they are working on.So far so good, we use the
inheritattribute to inherit thesetFlagandupdatemethods fromQQuickItem. This allows us to call them directly on ourBezierCurveobject. The correct way in the future will be to upcast ourBezierCurveobject toQQuickItemand call the methods on it.But what about
QQuickItemFlagthat I wrote? In c++ this isQQuickItem::Flag, but we can't directly access it in rust. I followed what I've seen in thecxx-qtcodebase, 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.hwe have:The namespace doesn't matter, but I used the one
cxx-qtuses in it's codebase. Then let's use it in our code:Of course don't forget to update
build.rsto include the new header file:Overriding the paint method
From now on, I will take a shortcut of not porting the whole
updatePaintNodemethod to rust, as this is no longer acxx-qtusage, but more acxxusage. Starting to work on bindings for the wholeQSG*family of classes is out of scope of this example.I believe the
cxx-qtcould 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
updatePaintNodemethod ofQQuickItemto use our ownBezierCurveclass. Note that you will seeQQuickItemUpdatePaintNodeDatain the code, I used the same alias technique as forQQuickItemFlag.And
beziercurve.hmostly 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.quickfeature? So we could have at least bindings forQQuickItem.Thank you for the good work.
Beta Was this translation helpful? Give feedback.
All reactions