6
votes

I re-implemented QGraphicsView to have the scene zoomed with a mouse wheel event. The scene contains several QGraphicsPixmapItem. The wheel event calls QGraphicsView::scale(qreal sx, qreal sy)

Everything works perfectly but the rendering. As I zoom out (the scene gets smaller), aliasing appears. I tried setting the render hints as following in the re-implemented QGraphicsView constructor:

ImageViewer::ImageViewer(QWidget * parent) :
  QGraphicsView(parent)
{
   setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::HighQualityAntialiasing);
}

I still see these artifacts. How can I get rid of this ?

2
Can you post a screenshot of what's going on? Are you setting any caching strategy on the items?peppe
Thank you @peppe ! I'm not setting anything about caching, so I guess it's default. Here's an example of what it looks like before (1:1 pixel ratio) and after (less than 1:1 pixel ratio) zooming out. You'll see some aliasing in the concentric circles.Algo

2 Answers

6
votes

Please see my comments for this question.

Basically you have to call setTransformationMode(Qt::SmoothTransformation) on the QGraphicsItems you want anti-aliasing to apply to.

Calling setRenderHints on the view did not work for me, either.

0
votes

The render hints are only applied if it is set bevor the painter is used. Here a snipped:

QGraphicsPixmapItem *
drawGraphicsPixmapItem(const QRectF &rect)
{
    auto pixmap = new QPixmap(rect.size().toSize());
    pixmap->fill("lightGrey");

    auto painter = new QPainter(pixmap);
    // set render hints bevor drawing with painter
    painter->setRenderHints(QPainter::Antialiasing);

    QPen pen;
    pen.setColor("black");
    pen.setWidth(3);
    painter->setPen(pen);
    QRectF rectT = rect;
    rectT.adjust(pen.widthF()/2,pen.widthF()/2,-pen.widthF()/2,-pen.widthF()/2);

    QPainterPath circlePath;
    circlePath.addEllipse(rectT);
    circlePath.closeSubpath();
    painter->fillPath(circlePath,QBrush("green"));
    painter->drawPath(circlePath);
    auto pixmapItem = new QGraphicsPixmapItem(*pixmap);
    pixmapItem->setCacheMode(
             QGraphicsItem::CacheMode::DeviceCoordinateCache,
             pixmap->size() );
    return pixmapItem;
}