composition of qgraphics items on qscene graph
-
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.
-
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: http://stackoverflow.com/questions/14154704/how-to-avoid-transparency-overlap-using-opengl)
-
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?