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



  • 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));@



  • Hi,

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


  • Lifetime Qt Champion

    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);
    @

    ?



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


  • Moderators

    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.


  • Lifetime Qt Champion

    [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.



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


  • Lifetime Qt Champion

    So you have it working as you want now ?



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.