Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QImage set transparency working but the initial form recovery don't
Qt 6.12 LTS is out! See what's new in the release blog

QImage set transparency working but the initial form recovery don't

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 3.0k Views 1 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello,

    I drew an image using QPainter and I was trying to make it transparency. I was finally made it but now I would like to set the initial format. In fact I'm trying to do the inverse steps.

    There's how I set the transparency:

    @image = image.convertToFormat(QImage::Format_ARGB32);
    image.fill(qRgba(0,0,0,0));@

    1 Reply Last reply
    0
    • Pradeep P NP Offline
      Pradeep P NP Offline
      Pradeep P N
      wrote on last edited by
      #2

      Hi,

      You can try with QPainter::CompositionMode. This might help you.

      Pradeep Nimbalkar.
      Upvote the answer(s) that helped you to solve the issue...
      Keep code clean.

      1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Something's not clear with your question. Do you mean you would like to start with:

        @
        QImage image(width, height, QImage::Format_ARGB32);
        image.fill(Qt::transparent);
        @

        ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          From the start the image is normal displayed.

          In paintEvent(QPaintEvent *event) I have:

          @
          QImage *img=new QImage(":/l4.png");
          QImage image=img->scaled(64,64,Qt::IgnoreAspectRatio);
          QPainter p;
          p.begin(this);
          QPointF newPoint;
          newpoint.setX(10);
          newpoint.setY(30);
          p.drawImage(newpoint,image);@

          I also have a button so when I clicked it the image dissapear (becomes transparent) as I supposed to do.

          The slot is function void transparent():
          @void MyProject::transparent()
          {
          image = image.convertToFormat(QImage::Format_ARGB32);
          image.fill(qRgba(0,0,0,0));
          }@

          This does exactly what I want but now I have another button so when I clicked it I want to display the image;

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            so you want an "UNDO":http://doc.qt.io/qt-5/qundo.html function?
            Anyway you would need to save the image and reset it.

            --- 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
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              [quote author="Flavia" date="1422443422"]
              @
              QImage *img=new QImage(":/l4.png"); << Beware ! you have a memory leak
              QImage image=img->scaled(64,64,Qt::IgnoreAspectRatio);
              QPainter p;
              p.begin(this);
              QPointF newPoint;
              newpoint.setX(10);
              newpoint.setY(30);
              p.drawImage(newpoint,image);
              @
              [/quote]

              Beside the memory leak you are creating in your paint event, you are using a local variable named image and not your member variable of the same name.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                Yes, you're right! In fact I was declaring img and image in my header file class as public and instances in constructor. In paint event I have only:

                @QPainter p;
                p.begin(this);
                QPointF newPoint;
                newpoint.setX(10);
                newpoint.setY(30);
                p.drawImage(newpoint,image);@

                I was in a rush of writing so I passed this fact in my previous post.
                Thanks anyway for your suggestions and remarks!

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  So you have it working as you want now ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #9

                    Not yet but I was taking a look at QPainter::CompositionMode as Pradeep P N said and it seems to be the solution. I have not tested yet but the examples I found are exactly what I need.

                    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
                    • Unsolved