Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6613261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:11:50+00:00 2026-05-25T20:11:50+00:00

I have a shape (in blue) loaded from a PNG with transparency: Then I’m

  • 0

I have a shape (in blue) loaded from a PNG with transparency:

enter image description here

Then I’m drawing several circles on top of this shape (in red) with QPainter::drawEllipse.

enter image description here

The result of that is somewhat similar to the third picture with the red shape completely covering the blue one:

enter image description here

What I would like however is for the blue shape to act as a mask to the red one, with a result like this:

enter image description here

Is it possible to do that with QPainter?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:11:51+00:00Added an answer on May 25, 2026 at 8:11 pm

    It’s possible. Assuming you’re loading your PNG into a QImage, you can do something like this to create a mask from your image:

    QImage img("your.png");
    QPixmap mask = QPixmap::fromImage(img.createAlphaMask());
    

    See the other to create*Mask functions in QImage for alternatives.

    Then it’s a simple matter of setting the painter’s clip region:

    QPainter p(this);
    p.setClipRegion(QRegion(mask));
    

    Here’s a stupid demo (don’t use that code as-is, the image loading, mask and region creation should be cached, they are potentially expensive):

    #include <QtGui>
    
    class W: public QWidget
    {
        Q_OBJECT
        public:
            W(): QWidget(0) { }
    
        protected:
            void paintEvent(QPaintEvent *)
            {
                QPainter p(this);
                QImage img("../back.png");
                QPixmap mask = QPixmap::fromImage(img.createAlphaMask());
    
                // draw the original image on the right
                p.drawImage(300, 0, img);
    
                // draw some ellipses in the middle
                p.setBrush(Qt::red);
                for (int i=0; i<100; i+=10)
                    p.drawEllipse(i+150, i, 20, 70);
    
                // and do the same thing, but with the mask active
                p.setClipRegion(QRegion(mask));
                for (int i=0; i<100; i+=10)
                    p.drawEllipse(i, i, 20, 70);
            }
    };
    

    Which produces something like this:
    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have an irregular shape in png format.As you know this shape
I have this table: id characteristic value 1 color blue 1 shape rectangle 2
I have a dataframe df: colour shape 'red' circle 'blue' square 'blue' circle 'green'
I have this: shape = (2, 4) # arbitrary, could be 3 dimensions such
Using .NET's System.Drawing.Graphics GDI stuff, I have a shape consting of two arrays of
Suppose I have the xml element <fruit color=blue taste=sweet shape=round></fruit> Without jQuery, I could
I have created several shapes with CSS, each shape is contained in an element
I have a shape file, and I want to show it on the web
I have a shape in Visio which is a group. The group shape can
I have a Shape class containing potentially many vertices, and I was contemplating making

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.