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 round the corners of a QImage?
Forum Updated to NodeBB v4.3 + New Features

How to round the corners of a QImage?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 1.4k Views 2 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.
  • N Offline
    N Offline
    n34rt
    wrote on last edited by
    #1

    How do I create a QImage with a given corner radius?
    Ex:

    QImage image = QImage(200, 200, QImage::Format_ARGB32, 20);
    
    1 Reply Last reply
    0
    • N n34rt

      @ChrisW67 the picture borders look jagged, how to apply antialiasing to it?

      M Offline
      M Offline
      mpergand
      wrote on last edited by
      #6

      @n34rt
      painter.setRenderHint(QPainter::Antialiasing);

      1 Reply Last reply
      1
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Images are rectangular. It's an array of pixels. Do you mean draw a rounded rectangle and have the corners transparent?
        To fill image with transparency see QImage::fill.
        To draw a rounded rectangle see QPainter::drawRoundedRect.

        N 1 Reply Last reply
        0
        • Chris KawaC Chris Kawa

          Images are rectangular. It's an array of pixels. Do you mean draw a rounded rectangle and have the corners transparent?
          To fill image with transparency see QImage::fill.
          To draw a rounded rectangle see QPainter::drawRoundedRect.

          N Offline
          N Offline
          n34rt
          wrote on last edited by n34rt
          #3

          @Chris-Kawa
          I tried this, but the image is still being painted outside the rounded rect:

              QPainter painter(this);
              painter.drawImage(0, 0, image().scaled(250, 100, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
              painter.drawRoundedRect(QRect(0, 0, 250, 100), 20.0, 20.0);
          

          The image is a rectangle i need to round her borders first, dont?

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #4
                QImage source(256, 256, QImage::Format_ARGB32);
                source.fill(Qt::blue);
            
                QImage dest(source.size(), QImage::Format_ARGB32);
                dest.fill(Qt::transparent);
            
                QPainterPath clipPath;
                clipPath.addRoundedRect(source.rect().adjusted(10, 10, -10, -10), 20, 20);
                
                QPainter p(&dest);
                p.setClipPath(clipPath);
                p.drawImage(0, 0, source);
                p.end();
            
                dest.save("/tmp/test.png");
            

            43e14a0e-7b21-4d0b-8b22-f9e2d8aa177e-image.png

            N 1 Reply Last reply
            2
            • C ChrisW67
                  QImage source(256, 256, QImage::Format_ARGB32);
                  source.fill(Qt::blue);
              
                  QImage dest(source.size(), QImage::Format_ARGB32);
                  dest.fill(Qt::transparent);
              
                  QPainterPath clipPath;
                  clipPath.addRoundedRect(source.rect().adjusted(10, 10, -10, -10), 20, 20);
                  
                  QPainter p(&dest);
                  p.setClipPath(clipPath);
                  p.drawImage(0, 0, source);
                  p.end();
              
                  dest.save("/tmp/test.png");
              

              43e14a0e-7b21-4d0b-8b22-f9e2d8aa177e-image.png

              N Offline
              N Offline
              n34rt
              wrote on last edited by
              #5

              @ChrisW67 the picture borders look jagged, how to apply antialiasing to it?

              M 1 Reply Last reply
              0
              • N n34rt

                @ChrisW67 the picture borders look jagged, how to apply antialiasing to it?

                M Offline
                M Offline
                mpergand
                wrote on last edited by
                #6

                @n34rt
                painter.setRenderHint(QPainter::Antialiasing);

                1 Reply Last reply
                1

                • Login

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