[SOLVED] How to copy QImage into a part of another QImage



  • Hi all,

    I have 2 QImages and I want to copy the second one into the first one but in a specific rectangle.

    Basically I want to do this:

    QImage1(x1, y1, w1, h1)= QImage2->copy(x2 ,y2, w2, h2);

    but QImage1 doesn't have any property to say to copy into a specific location. Should I use QPainter?



  • use QPainter :D



  • [quote author="Code_ReaQtor" date="1375092283"]use QPainter :D[/quote]

    That's good :D

    Actually I started trying QPainter after I post my message here.
    I wrote the following code but it crashes in pntToOut->drawPicture.

    @

    QImage *tmpImage=new QImage(origW, origH, orgImg.format());
    //Do some stuff in tmpImage
    QPainter *pntToOut = new QPainter(&orgImg);
    pntToOut->drawPicture(x,y, (const QPicture &)tmpImage);
    @


  • Moderators

    why do you cast?!
    @
    QPainter p(&orgImage);
    p.drawImage(x,y, tmpImage);
    @



  • [quote author="raven-worx" date="1375093084"]
    why do you cast?!
    @
    QPainter p(&orgImage);
    p.drawImage(x,y, tmpImage);
    @
    [/quote]

    Because I got the following error if I don't cast:
    @
    'void QPainter::drawImage(const QRectF &,const QImage &)' : cannot convert parameter 2 from 'QImage *' to 'const QImage &'
    Reason: cannot convert from 'QImage *' to 'const QImage'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    @


  • Lifetime Qt Champion

    Hi,

    No real need to allocate a new image

    @
    QImage tmpImage(origW, origH, orgImg.format());
    //Do some stuff in tmpImage
    QPainter *pntToOut = new QPainter(orgImg);
    pntToOut->drawPicture(x,y, tmpImage);
    @



  • [quote author="SGaist" date="1375093649"]Hi,

    No real need to allocate a new image

    @
    QImage tmpImage(origW, origH, orgImg.format());
    //Do some stuff in tmpImage
    QPainter *pntToOut = new QPainter(orgImg);
    pntToOut->drawPicture(x,y, tmpImage);
    @[/quote]

    Aha, that solved it. thanks a lot.


  • Moderators

    [quote author="dolevo" date="1375093316"]
    Because I got the following error if I don't cast:
    [/quote]
    The error says that the method expects an value rather than a pointer.
    So you either allocate it on the stack (like SGaist suggested) or you pass the value of the pointer (if you need to stick to pointer):
    @
    p.drawImage(x,y, *tmpImage);
    @



  • [quote author="raven-worx" date="1375094384"][quote author="dolevo" date="1375093316"]
    Because I got the following error if I don't cast:
    [/quote]
    The error says that the method expects an value rather than a pointer.
    So you either allocate it on the stack (like SGaist suggested) or you pass the value of the pointer (if you need to stick to pointer):
    @
    p.drawImage(x,y, *tmpImage);
    @

    [/quote]

    I understand. Thank you very much for your detailed explanation. I appreciate it.


Log in to reply
 

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