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. Attempt to create a circular mask over the image

Attempt to create a circular mask over the image

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 5.6k 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.
  • Z Offline
    Z Offline
    zzedan
    wrote on last edited by
    #1

    I currently have an image and I wish to place a circular mask over it , so that anything anything outside the circular mask becomes transparent. This will give my image a circular effect. This is how I am attempting to do it. However This just draws a circle over the image

    @QPixmap Masking::GetNew(QImage& img)
    {
    img.convertToFormat(QImage::Format_ARGB32);

    QPixmap source = QPixmap::fromImage(img);
    source = source.scaled(100,100,Qt::AspectRatioMode::IgnoreAspectRatio,Qt::TransformationMode::SmoothTransformation);

    QPainter painter(&img);
    QBrush brsh;
    QColor colr;
    colr.setRgb(255,255,255,255); //white
    brsh.setColor(colr);

    painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
    painter.setBrush(brsh);

    painter.drawEllipse(0,0,source.width()-5,source.width()-5);

    return QPixmap::fromImage(img); //Thisshouldhave the effect
    }
    @

    Any suggestion on what I might be doing wrong here. The above function is suppose to take in an image and apply a circular mask over it. and return the resulting image.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MrMNight
      wrote on last edited by
      #2

      Take a look at this code and try it - is that what you want?
      @
      QPixmap GetNew(QImage& img)
      {
      img.convertToFormat(QImage::Format_ARGB32);
      //blank copy image
      QImage imageOut(img.size(),QImage::Format_ARGB32);
      //painter on it
      QPainter painter(&imageOut);
      //set opacity
      painter.setOpacity(0.5);
      painter.setBrush(Qt::white);
      painter.setPen(Qt::NoPen);
      //draw transparent image
      painter.drawImage(0,0,img);
      //set opacity
      painter.setOpacity(1);
      QPainterPath path(QPointF(100,100));
      //your mask - ellipse
      path.addEllipse(100,100,img.width()-200, img.height()-200);
      painter.setClipPath(path);
      //draw untransparent part of image
      painter.drawImage(0,0,img);
      return QPixmap::fromImage(imageOut);
      }
      @

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        zzedan
        wrote on last edited by
        #3

        Yep that did the trick thanks

        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