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.4k 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.
  • M Offline
    M Offline
    mrjj
    Lifetime Qt Champion
    wrote on 5 Apr 2020, 07:02 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 5 Apr 2020, 07:04
    0
    • M mrjj
      5 Apr 2020, 07:02

      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 5 Apr 2020, 07:04 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/

      M C 2 Replies Last reply 5 Apr 2020, 08:08
      0
      • P patrickkidd
        5 Apr 2020, 07:04

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

        M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 5 Apr 2020, 08:08 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 6 Apr 2020, 03:09
        1
        • M mrjj
          5 Apr 2020, 08:08

          @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 6 Apr 2020, 03:09 last edited by
          #5

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

          https://alaskafamilysystems.com/

          1 Reply Last reply
          0
          • P patrickkidd
            5 Apr 2020, 07:04

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

            C Online
            C Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 6 Apr 2020, 06:45 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 6 Apr 2020, 15:50
            1
            • C Christian Ehrlicher
              6 Apr 2020, 06:45

              @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 6 Apr 2020, 15:50 last edited by
              #7

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

              https://alaskafamilysystems.com/

              R 1 Reply Last reply 6 Apr 2020, 15:53
              0
              • P patrickkidd
                6 Apr 2020, 15:50

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

                R Offline
                R Offline
                raven-worx
                Moderators
                wrote on 6 Apr 2020, 15:53 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 6 Apr 2020, 15:55
                0
                • R raven-worx
                  6 Apr 2020, 15:53

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

                  P Offline
                  P Offline
                  patrickkidd
                  wrote on 6 Apr 2020, 15:55 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/

                  R 1 Reply Last reply 6 Apr 2020, 16:12
                  0
                  • P patrickkidd
                    6 Apr 2020, 15:55

                    @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

                    R Offline
                    R Offline
                    raven-worx
                    Moderators
                    wrote on 6 Apr 2020, 16:12 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
                    • C Christian Ehrlicher
                      6 Apr 2020, 06:45

                      @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 6 Apr 2020, 17:16 last edited by patrickkidd 4 Jun 2020, 17:18
                      #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 6 Apr 2020, 17:23 last edited by patrickkidd 4 Jun 2020, 17:23
                        #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
                        • C Online
                          C Online
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on 6 Apr 2020, 17:37 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 6 Apr 2020, 17:42
                          0
                          • C Christian Ehrlicher
                            6 Apr 2020, 17:37

                            @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 6 Apr 2020, 17:42 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
                            • C Online
                              C Online
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 6 Apr 2020, 17:47 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 6 Apr 2020, 17:53
                              0
                              • C Christian Ehrlicher
                                6 Apr 2020, 17:47

                                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 6 Apr 2020, 17:53 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
                                • M Offline
                                  M Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 7 Apr 2020, 07:25 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 8 Apr 2020, 23:26
                                  0
                                  • M mrjj
                                    7 Apr 2020, 07:25

                                    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 8 Apr 2020, 23:26 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 7 Nov 2023, 21:04
                                    1
                                    • P patrickkidd
                                      8 Apr 2020, 23:26

                                      @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 7 Nov 2023, 21:04 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