Вопрос

I did a project with Qt 4.8 and I used QDeclarativeItem and draw circles by using QPainter's drawEllipse method.

Now, in Qt5, I really don't seem to find a way to do it. In the porting guide from qt4 to qt5 they say to use the QSG* classes, but I can't find a way to draw ellipses with any of them. I guess the two to look at are: QSGSimpleRectNode and QSGGeometry, but there is nothing...

Any idea? Thanks!

Это было полезно?

Решение

You are correct to use QSG* classes. It will be a lot trickier than the QPainter API since they are basically shallow wrappers around OpenGL, and OpenGL doesn't have native handling of circles, curves, etc.

The way to do it is to use a custom QML item that is implemented in C++ by subclassing QQuickItem and then overriding its updatePaintNode function with the custom drawing.

While you don't have to actually manage all the OpenGL buffers yourself, you do have to send the data to the OpenGL "server" (GPU) by specifying the points to be drawn (and their colors, etc.) For a curve, this is fairly labor intensive as curves in OpenGL are usually represented by many short lines or by a series of points. Here is on example. And here is another.

A filled ellipse or circle would instead by represented by many triangles.

I don't know the extent of your needs, but it is often far easier in OpenGL to instead use a circle image (sprite) made externally, then manipulate its position, size, scale, etc in the OpenGL. You can also do this in QtQuick though the technique would generally be different than using updatePaintNode which is for custom drawing, not image manipulation (generally).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top