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. Fit images of different size into my QGraphicsScene
Forum Updated to NodeBB v4.3 + New Features

Fit images of different size into my QGraphicsScene

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 9.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.
  • J Offline
    J Offline
    jk_mk
    wrote on last edited by
    #1

    Hello,

    I have a QGraphicsView of standard size 214x256. I have also created a QGraphicScene of the same size (214x256). But when I load an image having different size from 214x256 for example 94x128, it cannot be fit into my QGraphicsScene.How could I fit images of different size into my QGraphicsScene?

    Thanks

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      You could use "scale of QImage":http://doc.qt.nokia.com/4.7/qimage.html#scaled

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jk_mk
        wrote on last edited by
        #3

        I have used a QGraphicsPixmapItem as seems from the following code. How could I scale me pixmapItem to feet to my QGraphicsScene (named scene)? The size of my scene should be equal to the size of the loaded image?

        @if (!pixmapItem) {
        pixmapItem = scene->addPixmap(QPixmap(fileName, 0, Qt::AutoColor));
        ui->graphicsView_inputImage->setScene(scene);
        } else {
        pixmapItem->setPixmap(QPixmap(fileName, 0, Qt::AutoColor));
        }@

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          Also QPixmap has the "scale method":http://doc.qt.nokia.com/4.7/qpixmap.html#scaled

          So you could load the pixmap to a temporary pixmap and then rescale with the values you have.

          @
          QPixmap tmpmap (fileName, 0, Qt::AutoColor);
          pixmapItem = scene->addPixmap ( tmpmap.scaled ( width, height ) );
          @
          Similar you have to do in the else case.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jk_mk
            wrote on last edited by
            #5

            I have wrote this to my code,but still I cannot fit my png image to the size of my QGraphicsView. size_x and size_y are the xy values of my loaded image

            @ static QGraphicsPixmapItem* pixmapItem;

            if (!pixmapItem) {

            QPixmap tmpmap (QPixmap("2D.png"));
                pixmapItem = scene->addPixmap ( tmpmap.scaled ( size_x, size_y ) );
            
                    ui->graphicsView_inputImage->setScene(scene);
                } else {
                    pixmapItem->setPixmap(QPixmap("2D.png"));
                }@
            

            aslo to create scene I have subclassed QGraphicsScene, if it could help you:

            @Canvas::Canvas(): QGraphicsScene(0,0,214,256)
            {
            }@

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

              [quote author="jk_mk" date="1306595852"]I have wrote this to my code,but still I cannot fit my png image to the size of my QGraphicsView. size_x and size_y are the xy values of my loaded image
              [/quote]

              Well, if tmpmap should have already the size of the loaded picture. Whatelse should it know. In the scaled method you should provide the target size (214, 256). Otherwise scaled will just return a copy.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jk_mk
                wrote on last edited by
                #7

                Yes, you have right. But, I have one more question and I hope you could help me. Every time, I scale in an image what exactly I am doing concerning my data? To be more specific, do I change image's raw data? Because, now I had to display an image of 137x167, and I have scaled it into an image of 214x256. Is it harmful for the image quality? If yes, what should be done in order to maintain image quality (if it is possible)?

                Thanks for your help and your replies

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  irtusb
                  wrote on last edited by
                  #8

                  the scaled method has the "transformation mode":http://doc.qt.nokia.com/latest/qt.html#TransformationMode-enum argument.

                  Qt::FastTransformation: The transformation is performed quickly, with no smoothing.
                  Qt::SmoothTransformation: The resulting image is transformed using bilinear filtering.

                  The second option results in a smoother image.

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    koahnig
                    wrote on last edited by
                    #9

                    As long as you do not save the image to its initial source file you may do several scalings. E.g. you start your application with different sizes of your graphic scene and you always read it from original file, you should not face problems. The quality of the displayed may change with the actual size, but a new start from original file will have different scaling and a different result. See also the input of Jota for scaling quality. Consequence, you should not destroy your original file.

                    If you load your image once and you do several scalings with different sizes, you will certainly have deterioration of the image.

                    Vote the answer(s) that helped you to solve your issue(s)

                    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