Enabling anti-aliasing on QQuickItem or QSGGeometryNode
-
Is there an easy way to have antialiased drawing in my custom QQuickItem-derived element? I only draw basic shapes but they look ugly by default. Here is my rendered triangle for example:
I saw some people online saying I need to write an opengl shader or something, but this seems way overkill for my use case, and it assumes opengl is used, which is not the case on Mac for example...
I did do
setAntialiasing(true)
but without much benefit.I'm actually porting some of my QML code to make it more performant since i need to draw thousands of (simple) things. in my original QML code i used Shape/ShapePath with
layer.samples = 4
and it looked nice enough:Shape { id: playheadShape width: control.playheadTriangleWidth height: control.playheadTriangleHeight x: transport.playhead.ticks * control.pxPerTick - width / 2 y: control.height - height layer.enabled: true layer.samples: 4 ShapePath { fillColor: control.palette.text strokeColor: control.palette.text PathLine { x: 0 y: 0 } PathLine { x: playheadShape.width y: 0 } PathLine { x: playheadShape.width / 2 y: playheadShape.height } } }
It looked like this:
I basically want to mimic that in c++. I'm currently using QSGGeometryNode with a 2D QSGGeometry and a QSGFlatColorMaterial like this:
QSGNode * RulerItem::updatePaintNode (QSGNode * oldNode, UpdatePaintNodeData * data) { // ... auto * playheadNode = new QSGGeometryNode; auto * playheadGeometry = new QSGGeometry (QSGGeometry::defaultAttributes_Point2D (), 3); playheadNode->setGeometry (playheadGeometry); playheadNode->setFlag (QSGNode::OwnsGeometry); auto * playheadMaterial = new QSGFlatColorMaterial; playheadMaterial->setColor (text_color_); playheadNode->setMaterial (playheadMaterial); playheadNode->setFlag (QSGNode::OwnsMaterial); QSGGeometry::Point2D * playheadVertices = playheadGeometry->vertexDataAsPoint2D (); playheadVertices[0] = playhead_vertices_[0]; playheadVertices[1] = playhead_vertices_[1]; playheadVertices[2] = playhead_vertices_[2]; rootNode->appendChildNode (playheadNode); return rootNode; }
Any ideas?
-
I came across this example which uses QRhi (which avoids dependence on OpenGL directly like other people said to use): https://doc.qt.io/qt-6/qtquick-scenegraph-customrendernode-example.html
It still seems overly complex but at least it looks like I'm in the right direction.
Then, I came across this: https://doc.qt.io/qt-6/qrhirenderbuffer.html#sampleCount where I can set
sampleCount
to enable anti-aliasing. I don't really understand howQRhiRenderBuffer
can be used, but somehow usingQRhiRenderBuffer
with sampleCount set to 4 or 8 in aQSGRenderNode
-derived class seems like the proper way to have cross-platform hardware-accelerated anti-aliased drawing. Feel free to correct me.Until I find code examples showcasing how to bring all these functionalities together I have more important things to work on so I'll use QML
Shape
/ShapePath
for now which makes it super easy to draw simple things (like an anti-aliased triangle).