How to draw pixmap with same anti alias quality as QPushButton::icon?
-
@Christian-Ehrlicher I thought that you cannot set the devicePixelRatio on a QWidget paint device?
-
@patrickkidd
did you see the painters renderhints?
https://doc.qt.io/qt-5/qpainter.html#RenderHint-enum -
@raven-worx said in How to draw pixmap with same anti alias quality as QPushButton::icon?:
@patrickkidd
did you see the painters renderhints?
https://doc.qt.io/qt-5/qpainter.html#RenderHint-enumThose don’t apply to drawing pixmaps
-
@patrickkidd
QPainter::SmoothPixmapTransform
?? -
@Christian-Ehrlicher said in How to draw pixmap with same anti alias quality as QPushButton::icon?:
When you provide a c++ example we could test it out.
#include <QtWidgets> class Widget : public QWidget { public: QPixmap *m_pixmap; Widget(QWidget *parent=nullptr) : QWidget(parent) { m_pixmap = new QPixmap("away.png"); } ~Widget() { delete m_pixmap; } void paintEvent(QPaintEvent *) { QPainter p(this); p.drawPixmap(QRect(100, 100, 28, 28), *m_pixmap, m_pixmap->rect()); } }; int main(int argc, char **argv) { QApplication app(argc, argv); Widget w; w.resize(300, 300); w.show(); QPushButton b(QIcon(QPixmap("away.png")), "", &w); b.setIconSize(QSize(28, 28)); b.setFlat(true); b.resize(28, 28); b.move(150, 100); b.show(); app.exec(); }
@raven-worx said in How to draw pixmap with same anti alias quality as QPushButton::icon?:
@patrickkidd
QPainter::SmoothPixmapTransform
??This has no effect.
-
Here are two screenshots from my app on a display with
devicePixelRatio == 1
. These show the reverse behavior from my code example.
UsingQPushButton::icon
inQPushButton::paintEvent
:Using
QPainter::drawPixmap
Again, notice how the
QIcon
painting viaQPushButton
is better thatQPainter::drawPixmap
in these examples, but the opposite is true in both my python/c++ code examples.The end goal is to get behavior like the first screenshot here when calling
QPainter::drawPixmap
. -
@patrickkidd said in How to draw pixmap with same anti alias quality as QPushButton::icon?:
Again, notice how the QIcon painting via QPushButton is better that QPainter::drawPixmap in these examples, but the opposite is true in both my python/c++ code examples.
So I first would try to change the example to behave the same as your application.
-
@Christian-Ehrlicher said in How to draw pixmap with same anti alias quality as QPushButton::icon?:
@patrickkidd said in How to draw pixmap with same anti alias quality as QPushButton::icon?:
Again, notice how the QIcon painting via QPushButton is better that QPainter::drawPixmap in these examples, but the opposite is true in both my python/c++ code examples.
So I first would try to change the example to behave the same as your application.
That is the original question in this post that I am stuck on.
-
But what's the question - how to create a minimal reproducer? Simplify your program until it either does no longer have to problem or it's really minimal - but this is basic stuff which has to be done for every bug you're hunting.
-
@Christian-Ehrlicher I have provided a basic example that reproduces the problem, and in two different languages. The example clearly shows unpredictable behavior in the way pixmaps are painted.
-
Hi
ok i could reproduce it using your c++ codeAnd you are right - it seems that drawPixmap somehow not using any render hints.
p.setRenderHint(QPainter::Antialiasing,true); p.setRenderHint(QPainter::SmoothPixmapTransform,true); p.setRenderHint(QPainter::LosslessImageRendering,true); p.drawPixmap(QRect(100, 100, iconsize, iconsize), *m_pixmap, m_pixmap->rect());
makes no difference. I thought it was the scaling to fit rect but even with no scaling, it still not as smooth.
You can make it paint nicely with
QIcon ico; ico.addPixmap(*m_pixmap); ico.paint(&p,QRect(100, 100, iconsize, iconsize));
Runnable test project if others want to check it out.
https://we.tl/t-Q6PXROE2Le -
@mrjj said in How to draw pixmap with same anti alias quality as QPushButton::icon?:
QIcon ico; ico.addPixmap(*m_pixmap); ico.paint(&p,QRect(100, 100, iconsize, iconsize));
That certainly solves it. Wow. I looked into the QIcon paint code and found it TLDR;
-
@patrickkidd what exactly have you found?