Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to draw pixmap with same anti alias quality as QPushButton::icon?

How to draw pixmap with same anti alias quality as QPushButton::icon?

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 5 Posters 3.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    patrickkidd
    wrote on last edited by patrickkidd
    #1

    It appears as though QPushButton::icon painted via QPushButton::paintEvent draws with higher quality antialiasing than QPainter::drawPixmap. How is that so? What I can do to call QPainter:: drawPixmap with higher quality? The lines are pretty jagged when devicePixelRatio == 1. Unfortunately, these example pixmaps are with devicePixelRatio == 2.

    With QPainter::drawPixmap:

    Screen Shot 2020-04-04 at 10.23.33 PM.png

    With QPushButton::icon:

    Screen Shot 2020-04-04 at 10.23.42 PM.png

    Here is the code for the QPainter::drawPixmap example:

    class PixmapButtonHelper:
        
            PADDING = 12
            RADIUS = 2
    
        def paintPixmapButton(self, e):
            p = QPainter(self)
            p.setRenderHint(QPainter.Antialiasing, True)
            if not self.isEnabled(): # hack
                p.setOpacity(.5)
    
            if self.isDown() or self.isChecked():
                if self.isDown():
                    p.setBrush(util.CONTROL_BG)
                if self.isChecked():
                    p.setPen(QPen(QColor('#5a9adb'), 1))
                borderRect = self.rect()
                borderRect.setX(1)
                borderRect.setY(1)
                borderRect.setWidth(borderRect.width() - 2)
                borderRect.setHeight(borderRect.height() - 2)
                p.drawRoundedRect(borderRect, self.RADIUS, self.RADIUS)
    
            iconRect = QRect(self.PADDING, self.PADDING, self.width() - self.PADDING, self.height() - self.PADDING)
            iconRect.moveCenter(self.rect().center())
            if self.isChecked() and self._checkedPixmap:
                pixmap = self._checkedPixmap
            else:
                pixmap = self._uncheckedPixmap
            p.drawPixmap(iconRect, pixmap, pixmap.rect())
    
            p.end()
            p = None
    
    
    class PixmapPushButton(QPushButton, PixmapButtonHelper):
    
        def __init__(self, parent=None, **kwargs):
            super(QPushButton, self).__init__(parent)
            PixmapButtonHelper.__init__(self, **kwargs)
    
        def paintEvent(self, e):
            self.paintPixmapButton(e)
    

    But then, the graininess seems to switch sometimes:

    Screen Shot 2020-04-04 at 10.28.14 PM.png

    Here is the code for the last example:

    class Widget(QWidget):
        def __init__(self, parent=None):
            super().__init__(parent)
            self._pixmap = QPixmap('/Users/patrick/dev/familydiagram/resources/away.png')
        def paintEvent(self, e):
            p = QPainter(self)
            p.drawPixmap(QRect(100, 100, 28, 28), self._pixmap, self._pixmap.rect())
            p = None
    app = QApplication(sys.argv)
    w = Widget()
    w.show()
    w.resize(300, 300)
    
    b = QPushButton(QIcon(QPixmap('/Users/patrick/dev/familydiagram/resources/away.png')), '', w)
    b.setIconSize(QSize(28, 28))
    b.setFlat(True)
    b.resize(28, 28)
    b.move(150, 100)
    b.show()
    
    app.exec()
    

    This behavior seems unpredictable. Am I missing something in the pixmap scaling somewhere? How can I do this predictably?

    https://alaskafamilysystems.com/

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #17

      Hi
      ok i could reproduce it using your c++ code

      alt text

      And 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));
      
      

      alt text

      Runnable test project if others want to check it out.
      https://we.tl/t-Q6PXROE2Le

      P 1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi
        Im wondering if you see this on a 1440p or 4k ?
        I tried on a 1920x1200 screen but both icons look 100% the same regardless
        of button/painter.

        P 1 Reply Last reply
        0
        • mrjjM mrjj

          Hi
          Im wondering if you see this on a 1440p or 4k ?
          I tried on a 1920x1200 screen but both icons look 100% the same regardless
          of button/painter.

          P Offline
          P Offline
          patrickkidd
          wrote on last edited by
          #3

          @mrjj said in How to draw pixmap with same anti alias quality as QPushButton::icon?:

          Hi
          Im wondering if you see this on a 1440p or 4k ?
          I tried on a 1920x1200 screen but both icons look 100% the same regardless
          of button/painter.

          I'm not sure how that is relevant because I am pointing out different painting behavior on the same display. The pinging behavior should be the same on the same display.

          https://alaskafamilysystems.com/

          mrjjM Christian EhrlicherC 2 Replies Last reply
          0
          • P patrickkidd

            @mrjj said in How to draw pixmap with same anti alias quality as QPushButton::icon?:

            Hi
            Im wondering if you see this on a 1440p or 4k ?
            I tried on a 1920x1200 screen but both icons look 100% the same regardless
            of button/painter.

            I'm not sure how that is relevant because I am pointing out different painting behavior on the same display. The pinging behavior should be the same on the same display.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @patrickkidd
            well im asking since you talk about devicePixelRatio (will be the same for same display so ?)
            and i cannot reproduce it here so i wonder if you are changing something
            or its related to HI DPI scaling.

            I have only see pixelation like that using SVG files with a very
            small viewbox (the page) and QIcon would render the pixmap at max that resulution and when drawn it would be blown up to iconsize and look bad.

            you could try rendering with QIcon paint and see if that makes a differnce.

            P 1 Reply Last reply
            1
            • mrjjM mrjj

              @patrickkidd
              well im asking since you talk about devicePixelRatio (will be the same for same display so ?)
              and i cannot reproduce it here so i wonder if you are changing something
              or its related to HI DPI scaling.

              I have only see pixelation like that using SVG files with a very
              small viewbox (the page) and QIcon would render the pixmap at max that resulution and when drawn it would be blown up to iconsize and look bad.

              you could try rendering with QIcon paint and see if that makes a differnce.

              P Offline
              P Offline
              patrickkidd
              wrote on last edited by
              #5

              @mrjj Did you try reproducing with the full code example?

              https://alaskafamilysystems.com/

              1 Reply Last reply
              0
              • P patrickkidd

                @mrjj said in How to draw pixmap with same anti alias quality as QPushButton::icon?:

                Hi
                Im wondering if you see this on a 1440p or 4k ?
                I tried on a 1920x1200 screen but both icons look 100% the same regardless
                of button/painter.

                I'm not sure how that is relevant because I am pointing out different painting behavior on the same display. The pinging behavior should be the same on the same display.

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @patrickkidd said in How to draw pixmap with same anti alias quality as QPushButton::icon?:

                I'm not sure how that is relevant because I am pointing out different painting behavior on the same display

                It is relevant because when you don't set the correct device pixel ratio it will look similar to your problem.

                Did you try reproducing with the full code example?

                When you provide a c++ example we could test it out.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                P 2 Replies Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  @patrickkidd said in How to draw pixmap with same anti alias quality as QPushButton::icon?:

                  I'm not sure how that is relevant because I am pointing out different painting behavior on the same display

                  It is relevant because when you don't set the correct device pixel ratio it will look similar to your problem.

                  Did you try reproducing with the full code example?

                  When you provide a c++ example we could test it out.

                  P Offline
                  P Offline
                  patrickkidd
                  wrote on last edited by
                  #7

                  @Christian-Ehrlicher I thought that you cannot set the devicePixelRatio on a QWidget paint device?

                  https://alaskafamilysystems.com/

                  raven-worxR 1 Reply Last reply
                  0
                  • P patrickkidd

                    @Christian-Ehrlicher I thought that you cannot set the devicePixelRatio on a QWidget paint device?

                    raven-worxR Offline
                    raven-worxR Offline
                    raven-worx
                    Moderators
                    wrote on last edited by
                    #8

                    @patrickkidd
                    did you see the painters renderhints?
                    https://doc.qt.io/qt-5/qpainter.html#RenderHint-enum

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    P 1 Reply Last reply
                    0
                    • raven-worxR raven-worx

                      @patrickkidd
                      did you see the painters renderhints?
                      https://doc.qt.io/qt-5/qpainter.html#RenderHint-enum

                      P Offline
                      P Offline
                      patrickkidd
                      wrote on last edited by
                      #9

                      @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-enum

                      Those don’t apply to drawing pixmaps

                      https://alaskafamilysystems.com/

                      raven-worxR 1 Reply Last reply
                      0
                      • P patrickkidd

                        @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-enum

                        Those don’t apply to drawing pixmaps

                        raven-worxR Offline
                        raven-worxR Offline
                        raven-worx
                        Moderators
                        wrote on last edited by
                        #10

                        @patrickkidd
                        QPainter::SmoothPixmapTransform??

                        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                        If you have a question please use the forum so others can benefit from the solution in the future

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          @patrickkidd said in How to draw pixmap with same anti alias quality as QPushButton::icon?:

                          I'm not sure how that is relevant because I am pointing out different painting behavior on the same display

                          It is relevant because when you don't set the correct device pixel ratio it will look similar to your problem.

                          Did you try reproducing with the full code example?

                          When you provide a c++ example we could test it out.

                          P Offline
                          P Offline
                          patrickkidd
                          wrote on last edited by patrickkidd
                          #11

                          @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();
                          }
                          

                          away.png

                          @raven-worx said in How to draw pixmap with same anti alias quality as QPushButton::icon?:

                          @patrickkidd
                          QPainter::SmoothPixmapTransform??

                          This has no effect.

                          https://alaskafamilysystems.com/

                          1 Reply Last reply
                          0
                          • P Offline
                            P Offline
                            patrickkidd
                            wrote on last edited by patrickkidd
                            #12

                            Here are two screenshots from my app on a display with devicePixelRatio == 1. These show the reverse behavior from my code example.
                            Using QPushButton::icon in QPushButton::paintEvent:

                            Screen Shot 2020-04-06 at 9.18.55 AM.png

                            Using QPainter::drawPixmap

                            Screen Shot 2020-04-06 at 9.19.04 AM.png

                            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.

                            The end goal is to get behavior like the first screenshot here when calling QPainter::drawPixmap.

                            https://alaskafamilysystems.com/

                            1 Reply Last reply
                            0
                            • Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #13

                              @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.

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              P 1 Reply Last reply
                              0
                              • Christian EhrlicherC Christian Ehrlicher

                                @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.

                                P Offline
                                P Offline
                                patrickkidd
                                wrote on last edited by
                                #14

                                @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.

                                https://alaskafamilysystems.com/

                                1 Reply Last reply
                                0
                                • Christian EhrlicherC Offline
                                  Christian EhrlicherC Offline
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #15

                                  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.

                                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                  Visit the Qt Academy at https://academy.qt.io/catalog

                                  P 1 Reply Last reply
                                  0
                                  • Christian EhrlicherC Christian Ehrlicher

                                    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.

                                    P Offline
                                    P Offline
                                    patrickkidd
                                    wrote on last edited by
                                    #16

                                    @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.

                                    https://alaskafamilysystems.com/

                                    1 Reply Last reply
                                    0
                                    • mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #17

                                      Hi
                                      ok i could reproduce it using your c++ code

                                      alt text

                                      And 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));
                                      
                                      

                                      alt text

                                      Runnable test project if others want to check it out.
                                      https://we.tl/t-Q6PXROE2Le

                                      P 1 Reply Last reply
                                      0
                                      • mrjjM mrjj

                                        Hi
                                        ok i could reproduce it using your c++ code

                                        alt text

                                        And 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));
                                        
                                        

                                        alt text

                                        Runnable test project if others want to check it out.
                                        https://we.tl/t-Q6PXROE2Le

                                        P Offline
                                        P Offline
                                        patrickkidd
                                        wrote on last edited by
                                        #18

                                        @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;

                                        https://alaskafamilysystems.com/

                                        M 1 Reply Last reply
                                        1
                                        • P patrickkidd

                                          @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;

                                          M Offline
                                          M Offline
                                          manuelschneid3r
                                          wrote on last edited by
                                          #19

                                          @patrickkidd what exactly have you found?

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved