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. [SOLVED] How to copy QImage into a part of another QImage
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 8.8k 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.
  • D Offline
    D Offline
    dolevo
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Code_ReaQtor
      wrote on last edited by
      #2

      use QPainter :D

      Please visit my open-source projects at https://github.com/Code-ReaQtor.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dolevo
        wrote on last edited by
        #3

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

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

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

          --- 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
          • D Offline
            D Offline
            dolevo
            wrote on last edited by
            #5

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

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

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

              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
              • D Offline
                D Offline
                dolevo
                wrote on last edited by
                #7

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

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

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

                  --- 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
                  • D Offline
                    D Offline
                    dolevo
                    wrote on last edited by
                    #9

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

                    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