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?
Forum Updated to NodeBB v4.3 + New Features

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.7k Views 3 Watching
  • 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 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