How do I controll clipping with non-opaque graphics-item's in Qt?
I have a bunch of QGraphicsSvgItem's in a QGraphicsScene that are drawn connected by QGraphicsLineItem's. This show's a graph of a tree-structure.
What I want to do is provide a feature where everything but a selected sub-tree becomes transparent. A kind of "highlight this sub-tree" feature. That part was easy, but the results are ugly because now the lines can be seen through the semi-transparent svg's.
I am looking for some way to still clip other QGraphicsItem's in the scene to the svg item's, giving the effect that the svg's are semi-transparent windows to the background.
I know this code does not use svg's but I figure you can replace that yourself if you are so inclined.
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QGraphicsScene scene;
for( int i = 0; i < 10; ++i ) {
开发者_开发问答QGraphicsLineItem* line = new QGraphicsLineItem;
line->setLine( i * 25.0 + 1.0, 0, i * 25.0 + 23.0, 0 );
scene.addItem( line );
}
for( int i = 0; i < 11; ++i ) {
QGraphicsEllipseItem* ellipse = new QGraphicsEllipseItem;
ellipse->setRect( (i * 25.0) - 9.0, -9.0, 18.0, 18.0f );
ellipse->setBrush( QBrush( Qt::green, Qt::SolidPattern ) );
ellipse->setOpacity( 0.5 );
scene.addItem( ellipse );
}
QGraphicsView view( &scene );
view.show();
return app.exec();
}
I would like the line's to not be seen behind the circle's. I have tried fiddling with the depth-buffer and the stencil buffer using opengl rendering to no avail.
How do I get the QGraphicsSvgItem's (or QGraphicsEllipseItem's in the example code) to still clip the lines even though they are semi-transparent?
The best solution here is to subclass QGraphicsScene and your graphics items.
Create other class for scene and several classes for different graphics items.
Then, you will have "paint" method for each item, where you can draw with the opacity you require.
In that case you will be able to solve clipping problem also because you will have control over the shape and bounding rectangle for each item.
Another nice feature would be ability to link items together in your implementation so that when you click somewhere, you can set the visibility settings for several items at once.
In other words, you will have more control over your entire scene and thus learning and writing these subclasses is a good time investment.
For example, you can see Colliding Mice Example where custom graphics items are painted.
精彩评论