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. How to resize QImage
Forum Updated to NodeBB v4.3 + New Features

How to resize QImage

Scheduled Pinned Locked Moved General and Desktop
6 Posts 5 Posters 42.4k 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.
  • T Offline
    T Offline
    tclin1998
    wrote on last edited by
    #1

    Hi:

    To resize QPixmap 'pmap' I can use
    @
    void resizePmap(QPixmap &pmap)
    {
    pmap = QPixmap(newSize); // old pmap's memory will be recycled by Qt. See my previous post.
    }
    @
    As for QImage,
    @
    void resizeImage(QImage &img)
    {
    img = new QImage(newSize); // Will the old memory of img get recycled by Qt too?

    }
    @

    Thanks!

    -Todd
    Edit: Please use code tags (@) around your code sections; Andre

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      That doesn't even compile. If you want to create a brand new image with a different size, then
      @
      QImage image(oldSize);
      /* ... */
      image = QImage(newSize);
      @

      works. That's not resizing (QImage::scaled).

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tclin1998
        wrote on last edited by
        #3

        Sorry typo
        @
        QImage img(oldSize);

        void resizeImage(QImage &img)
        {
        img = QImage(newSize); // Will the old memory of img get recycled by Qt too?
        }
        @

        My question is : Is the oldSize chunk of memory going to be recycled by Qt?
        Or I have to take care of it?

        Thanks!
        -Todd

        Edit: please use @ tags around code sections; Andre

        1 Reply Last reply
        0
        • M Offline
          M Offline
          maxvanceffer
          wrote on last edited by
          #4

          Or you can use method scaled(size); which will return new image scaled to size

          1 Reply Last reply
          0
          • L Offline
            L Offline
            ludde
            wrote on last edited by
            #5

            By resizing, I assume you will want to keep some of the pixmap / image data from the original pixmap / image? I'm quite sure your example code will not do that - you are just creating a new image with a different size and storing it in the same variable - the contents will be undefined. Or am I missing something? (Your comment that the memory will be recycled makes no sense to me.)

            If you have a pixmap / image, and want to resize it, you have to create a copy of that pixmap / image that contains a part of that image, or a scaled version of that image. To do this, you use the copy() or scaled() methods, respectively. E.g.:

            @
            pmap = pmap.copy(x, y, w, h);
            pmap = pmap.scaled(w, h);
            image = image.copy(x, y, w, h);
            image = image.scaled(w, h);
            @

            scaled() also takes additional arguments that deal with aspect ratio and transformation mode, have a look at the docs for QImage and QPixmap to see how to use them.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              task_struct
              wrote on last edited by
              #6

              [quote author="tclin1998" date="1305739253"]
              My question is : Is the oldSize chunk of memory going to be recycled by Qt?
              Or I have to take care of it?
              [/quote]

              QImage uses "implicit data sharing":http://doc.qt.nokia.com/latest/implicit-sharing.html#implicit-data-sharing, so you don`t have to take care of it.

              "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

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