Nominate our 2022 Qt Champions!

composition of qgraphics items on qscene graph

  • I am drawing multiple qgraphics items on mouse moves in qgraphics scene. The items have alpha(opacity)=50%. But, I want the items to have image composition, so that overlapping region does not become dark- overlapping region should have opacity=50%. I do not want the alphas of different items to be added up. Overlapping region should show only the portion from the topmost item. How can I do that?

    I am drawing the items using the paint functions and after that adding to the scene, they just overlap and the overlapping region becomes darker. How can I avoid this. I also need to implement in an efficient way. I may have thousands of items on the scene( actually the items will be small circles/dots created during mouse moves).

    Can somebody suggest me how can I do it? I guess, in the paint function of the qgraphicsitem, I need to read the qimage of the scene and do some manipulation inside it. Or is there any other alternative? Need help asap.

  • If you don't want items to show from behind another item, why are you using alpha at all?

    It would help if you could describe more exactly which effect you try to achieve.

  • See here:

    This is the problem I am trying to solve(I am using graphics item though). I am making a drawing program which responds/change alpha with pressure sensitivity/mouse speed. During mouse move, points will be drawn from the stroke start to stroke end. Each of my point will be an graphics item. When the whole stroke is drawn(after mouse release), the points will be grouped together. Right now, the alphas of the items are showing dark color (something like:

  • How should it look if strokes overlap?

    Right now, it sounds like it would be simpler if each stroke was a qgraphicsitem (instead of each point).

  • If strokes overlap, there may be overlap( it will be darker).
    If I just make a stroke as a graphicsitem( instead of each point), it will not be right. It is pressure sensitive, so different points in a stroke may have different alpha based on the pressure input.
    If I just use a stroke, I have to use a single alpha value all over the stroke, which is wrong.

    That is why I am trying to draw points first and then group them as a stroke graphics item later.

  • What you need is a pixmap per stroke with the correct alpha values set for each pixel. Then all you need to do is to paint those pixmaps on top of each other.

  • I guess QImage will be better than QPixmap for pixel manipulation? Also, how can I know the size of pixmap before drawing the stroke? Should I just set it as the drawing canvas size?

    I still may need to draw the points inside the stroke, as both the opacity and thickness of stroke may change on the fly(like photoshop ). Without drawing individual points, I do not have other solutions. Is there any alternative?

  • Maybe even better have a class that can generate a QImage with the correct alpha for one point. That way, you can change any parameter you want after the fact, and simply have your image updated.
    Then a class that combines the images from all points within a stroke. Then you paint the stroke images on top of each other.

  • Thanks for your input. How to show the qimage in the qscenegraph? I am using a scene. Do, I need to make graphics item of the qimage points?
    I can combine only after when the after mouse release(when the stroke is completed, right?).

    The stroke needs to be a qgraphics item, as I want easy deletion/movement of strokes later.

  • I think something like this might work:

    • Create a class for each point. The class has all the parameters to generate the correct alpha image for that point on demand
    • Create a QGraphicsItem for the stroke. The item hold a list of point classes, and a list of cached pixmaps (one for each image). Each time a point is added, the corresponding cached pixmap is created. If a point class changes parameters later on, an existing image is replaced within the GraphicsItem, and the corresponding cached pixmap is updated
    • Within the GraphicsItem, create one additional pixmap which combines all the cached point pixmaps. You can do this by painting directly into a new QPixmap, and selecting the right composition mode.
    • When painting the GraphicsItem, all you need to do is update any dirty caches (single point pixmaps, then overall pixmaps) and paint the one resulting pixmap, again selecting the right composition mode, so multiple strokes look right on top of each other

    All that said, bear in mind that the GraphicsView framework does not use the GPU, and runs single-threaded only (exception: You could create the QImages in a separate thread). So how well this solution will perform...hard to say.

  • Thanks everyone for their informative replies. I am also confused about whether I should use qwidget or qgraphicsview. I need to zoom, move strokes. I do not really understand why most qt drawing softwares are using qwidget(pencil2D, Krita). Can anybody explain?