How to draw line - not on Canvas, just object?

12,227

If you don't want to use the Canvas, there are a few options:

  • implement QQuickPaintedItem - it uses QPainter API do to drawing on a QML item. Easiest solution, but not very efficient if you do a lot of drawing.

  • implement custom QQuickItem - it is more complex, you will have to implement additional stuff, but it will be faster, because it will use the scenegraph API, so your rendering will be optimized

  • use the Qt3D module, although it is for 3d graphics, it is capable of 2d drawing as well.

Lastly, and obviously you could draw a line by just creating a very narrow and wide Rectangle and give it some rotation. But that's a very clumsy solution. I'd recommend you study the second option, and check some of the examples, implementing QQuickItem with custom geometry and shaders. Here is one similar example.

Update: Since Qt 5.10, there is also the qml Shape element, which offers a declarative way to define vector shapes, IMO the easiest way to have parametric graphics in qml. The downside is performance isn't stellar, still it is perfectly usable if you don't go into excessive amount of elements.

Share:
12,227
Robotex
Author by

Robotex

Updated on August 10, 2022

Comments

  • Robotex
    Robotex almost 2 years

    I'm porting my game to QML and has one difficult. Look at this picture:

    Sprite with vectors

    Triangle is a sprite and lines are force vectors. I can show sprite in QML but I can't understand how to draw vectors. It can have length more that sprite size.

    I think I can use Rectangle with height of 1px, but I don't know how to rotate it.

    Forces can change at each frame so drawing performance must be good.