我正在使用 QGraphicsPathItems 绘制半透明路径,但是当两条路径重叠时我会遇到问题,它们变得更加不透明,但我希望它们保持相同级别的透明度,无论有多少路径重叠。
左侧两条独立的路径,变得更加不透明。右侧,单条路径穿过自身,透明度相同,这是我希望通过多条路径实现的效果。
是否有可能实现这样的目标?
最佳答案
我搜索了一下 Qt 是否具有真正的不透明度分层,并在 Andreas Aardal Hanssen 的 Qt 博客中找到了一篇帖子:
https://blog.qt.io/blog/2009/04/23/layered-rendering-part-2-it-helps-solve-many-problems/
他说唯一的方法是使用离屏渲染。
By rendering the “green sub-tree” into a separate layer, we can combine all items and apply one uniform opacity as part of composing these items together. In my last blog I wrote about off-screen rendering. This work has progressed and is in quite a usable state (although the code is really ugly). It works! The rendering output for the same application as the above looks like this.
离屏渲染方案链接为https://blog.qt.io/blog/2009/02/27/braindump-graphics-view-and-the-joys-of-off-screen-rendering .
我认为这个想法是将每一层分别渲染成一个像素图。该层中的项目相对于彼此是不透明的。然后,您使用彼此之间的透明度渲染层本身。
同一层中的项目相对于彼此是不透明的,但相对于其他层中的项目是透明的。
该链接讨论了一些使用 DeepItemCoordinateCache 的原型(prototype)项目,该项目将项目及其子项呈现到屏幕外缓冲区,然后呈现该缓冲区。这将达到预期的效果。
Collapsing a subtree into a single offscreen buffer is possible. I’ve spent two days this week researching it, wrote some code, and ended up with a prototype that’s so ugly I don’t want to share it just yet. 😉 But I’ve seen that it’s perfectly possible without messing up QGV’s internals. I dubbed two new cache modes:
DeepItemCoordinateCache – caches the item and “all” children, no repaints for “any” child if the parent is transformed DeepDeviceCoordinateCache – save for DeviceCoordinateCache
不幸的是,我不知道他的原型(prototype)代码是否在任何地方都可用。他暗示它位于 https://doc.qt.io/qt-5/qtwidgets-graphicsview-embeddeddialogs-example.html 的嵌入式对话框示例中。 , 所以也许你应该在那里搜索。
关于c++ - QGraphicsPathItem 重叠的半透明路径变得更加不透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39284835/