Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

What Qt type is best for graphics and image drawing?



  • I am wanting to make a "limitless" surface or grid, and am wondering what type of Qt type to use. I thought of QGraphicsScene, but I'm not sure if I can draw OpenGl-like objects (thinking of lines and rectangles, to draw a grid pattern) with graphics scene. I know I have more drawing capabilities with QPainter, but I am going to be having several hundred different types of objects that lay on the grid, each will have a large amount of properties. I will most likely be drawing them as images, but am not sure about the grid part.
    What's the best way to do this in Qt?


  • Lifetime Qt Champion

    Hi
    QGraphicsScene/view allows zoom and pan and other features out of the box
    and you can draw anything you like as you can
    inherit QGraphicsItem and use QPainter to draw the actual shape.

    So if you need zooming / panning / selection / interaction / Level of Detail support and such
    features, its a good way to go to use QGraphicsScene.

    But if such features is not needed, then a pure QPainter might be better in terms of simplicity.



  • I will definitely use Panning and Zooming features. I wasn't aware you could use QPainter inside it. I guess I will be using QGraphicsScene. Though, do I have to have a QGraphicsView with the QGraphicsScene?


  • Lifetime Qt Champion

    @johnratius
    Hi
    Ok then Graphics View Framework should be the best way :)
    Yes the view draws the items the scene holds. you can have multiple views on to same scene etc.
    So you need the view as that is the Visual part.

    Make sure to read about the coordinate system as that is more complex than simple
    x,y pixel values from a normal drawing api.
    https://doc.qt.io/qt-5/graphicsview.html



  • Thanks, is there an example of QGraphicsScene and QGraphicsView that implements the grid or zoom and panning feature?

    Even better, a grid-like example? I want a "infinite" like grid, and am not sure if I should just draw lots of QRects or lines performance wise.


  • Lifetime Qt Champion

    @johnratius
    Hi
    Check out
    https://doc.qt.io/qt-5/qtwidgets-graphicsview-chip-example.html
    It takes each pixel from an image an make an item out of it :)
    That is sort of a grid.

    But im not really sure what a "limitless" surface or grid would be ?
    Do you have an image of such concept ?



  • @mrjj
    Thanks, I'll check it out. Yes, the user can drag the grid wherever, and it will appear that it is endless. I was planning on drawing each cell from the top left to bottom right, and then translate all of them once the panning reaches -1 or +1 unit on x or y axis back, so it appears the grid keeps going. If that makes sense. Zooming will have to increase the number of drawn cells though. Is this ideal for performance?


  • Lifetime Qt Champion

    @johnratius
    Yes makes perfect sense. You can also use the LOD feature (if possible with your drawings) to
    allow a huge number of objects shown at same time. (100K or more without any issue)
    depending on the PC.



  • What is the LOD feature? I will probably try to use it.


  • Lifetime Qt Champion

    @johnratius

    Hi
    LOD means Level of Detail and its often used in games where an object that is far away from view, is drawn using a lesser detailed model then when up close.

    for Graphics View Framework
    its more like a "factor" that allows you do reduce the drawing
    calls. Like not paint small text or what else that could make sense for your type of shapes.

    from the 40000 sample. Here is only draws the rect when far away and not all the small details.

      const qreal lod = option->levelOfDetailFromTransform(painter->worldTransform());
        if (lod < 0.2) {
            if (lod < 0.125) {
                painter->fillRect(QRectF(0, 0, 110, 70), fillColor);
                return;
            }
    

Log in to reply