9

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 ?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Algo
  • 91
  • 1
  • 3
  • Can you post a screenshot of what's going on? Are you setting any caching strategy on the items? – peppe Jul 18 '13 at 22:13
  • 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](https://lh6.googleusercontent.com/-e2pH06jhy94/Ue09l8A1SWI/AAAAAAAAY08/YGa-wj9ollY/w958-h599-no/Fullscreen+capture+7222013+100003+AM.jpg) (1:1 pixel ratio) and [after](https://lh4.googleusercontent.com/-PZJVX0jl6yw/Ue09mo_Iu2I/AAAAAAAAY1E/nMf28uA9MlE/w958-h599-no/Fullscreen+capture+7222013+100007+AM.jpg) (less than 1:1 pixel ratio) zooming out. You'll see some aliasing in the concentric circles. – Algo Jul 22 '13 at 14:13

2 Answers2

9

Please see my comments for this question.

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

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

Patrick
  • 2,243
  • 2
  • 23
  • 32
  • So there's no way to have an antialiased background? Edit: Nm was an error in my code. – Timmmm Feb 19 '14 at 22:30
  • 1
    Using `SmoothTransformation` does some antialiasing when using `fitInView`, but it's basically next to nothing. I now scale the pixmap manually and set `Qt.SmoothTransformation`. This unfortunately wastes lots of time, but the result is much better. – user136036 Jan 24 '20 at 21:17
  • I get an error saying "no member named 'setTransformationMode' in 'QGraphicsItem'". – Donald Duck Dec 16 '21 at 15:44
  • @DonaldDuck It is only for `QGraphicsPixmapItem`. Answer updated. – Patrick Dec 16 '21 at 20:05
1

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;
}
Alex44
  • 3,597
  • 7
  • 39
  • 56