Qt World Summit: Register Today!

QGraphicsView: is selective antialiasing possible?

  • When using antialising

    	graphicsView->setRenderHint( QPainter::Antialiasing );

    curves are nicely drawn but straight lines that are drawn with a pen of size 1 have a width of two pixels instead of one, which is not so nice.

    Is there a way to selectively indicate which items needs to be antialiased?

  • You can control antialiasing on the QPainter level using setRenderHint.
    So, for custom items where you write the paint method yourself, you should be able to decide whether to draw with antialiasing on or off (or even draw some parts with, some parts without).

  • @Asperamanca Excellent idea. I've added an example class for those interested in the implementation:

    class CurveItem : public QGraphicsItem {
    	CurveItem( const int cX1, const int cY1, const int cX2, const int cY2, QGraphicsScene *scene ):
    		Path.moveTo( cX1, cY1 );
    		Path.quadTo( cX1 + ( cX2-cX1 )/2, cY1+30, cX2, cY2 );
    		scene->addItem( this );
        QRectF boundingRect() const {
    		return QRectF( X1, Y1, X2, Y2 );
    	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option=0, QWidget *widget=0){
    		painter->setPen( QPen(QBrush(Qt::red), 1, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin ) );
    		painter->setRenderHint( QPainter::Antialiasing );
    		painter->drawPath( Path );
    	QPainterPath Path;
    	int X1, Y1, X2, Y2;

Log in to reply