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. Image Resizing in QGraphics
Forum Updated to NodeBB v4.3 + New Features

Image Resizing in QGraphics

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 5.0k 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.
  • S Offline
    S Offline
    sergex
    wrote on last edited by
    #1

    hi,

    I'm having some problems when putting an image in a resizable widget. I am subclassing QGraphicsView and inside this class I have a scene and an item subclassed from QGraphicsItem. The GraphicsView widget gets placed in a QSplitter. So when I resize the splitter I want the image inside to also resize accordingly. I managed to do this by using the function

    @
    fitInView(scene->sceneRect, Qt::IgnoreAspectRatio);
    @

    This function is inside the resizeEvent in the QGraphicsView Class.
    Before using this function, I wasn't able to have the image cover the entire view for some reason even though when I draw the image I am doing inside the paint function in QGraphicsItem:
    @

    painter->drawImage(scene->sceneRect, myImage);

    @
    where scene is the scene that is holding the item.

    So now I need to be able to zoom in my image only in width. I implemented this inside the wheelEvent in the QGraphicsView class, when I zoom out it's fine, the image just starts getting smaller (in width) but when I zoom in since the width of the image starts growing I can't zoom in because of the fitInView function implemented in the resizeEvent obviously.

    So my question is how could I have the image always fit in height in the view even when I resize the widget containing it, and also then be able to zoom-in in width, in this case the viewport would only be seeing the zoomed area since the image now is wider than the viewport until I zoom out to the original size. My problem was that without fitInview the image wasn't resizing when the widget gets resized..

    Any help greatly appreciated!

    Thanks.

    1 Reply Last reply
    0
    • F Offline
      F Offline
      frares
      wrote on last edited by
      #2

      I use transformations. The whole QGraphicsView might be affected or just the item.

      You'll need to reset an item's transformation before applying a new one.

      By the way, does your class inherit from QGraphicsPixmapItem ?

      Hope it helps.
      Francisco

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sergex
        wrote on last edited by
        #3

        Thanks for the reply! No, my item class inherits from QGraphicsItem.
        I don't think I know how to do what you are saying.. could you please explain a little more?

        1 Reply Last reply
        0
        • F Offline
          F Offline
          frares
          wrote on last edited by
          #4

          Try QGraphisPixmapItem:

          "http://doc.qt.nokia.com/4.7/qgraphicspixmapitem.html#details":http://doc.qt.nokia.com/4.7/qgraphicspixmapitem.html#details

          The easiest way: You can set the scale of the QGraphicsView:

          "http://doc.qt.nokia.com/4.7/qgraphicsview.html#scale":http://doc.qt.nokia.com/4.7/qgraphicsview.html#scale

          The "scale" method uses two factors, one for X and another for Y. As I understand, you will always keep the Y factor as 1.0 .

          But before setting a new scale (a particular case of transformation), you will need to reset the previous transformation:

          "http://doc.qt.nokia.com/4.7/qgraphicsview.html#resetTransform":http://doc.qt.nokia.com/4.7/qtransform.html#scale

          A bit more complicated, as that might be a little bit more slower than really understanding the transformations and applying a calculated one, adjusted for the difference in scale, rather than reseting and setting a new one.

          Create a QTransform and apply to it the scale you need:

          "http://doc.qt.nokia.com/4.7/qtransform.html#scale":http://doc.qt.nokia.com/4.7/qtransform.html#scale

          Then apply it to the QGraphicsView without combining it with the previous transformation:

          "http://doc.qt.nokia.com/4.7/qgraphicsview.html#setTransform":http://doc.qt.nokia.com/4.7/qgraphicsview.html#setTransform

          Your resize event might do this:

          • get new width;
          • calculate the ratio between this and the first ever width;
          • create a QTransform object and set its scale properly;
          • apply this to the QGraphicsView using its setTransform method without combination to the previous;

          OR

          • get new width;
          • calculate the ratio between this and the last one;
          • save width for future use;
          • create a QTransform object and set its scale properly;
          • apply this to the QGraphicsView using its setTransform method combining it to the previous;

          I have never tried to do this, but I guess that will work as expected.

          Hope this helps,
          Francisco

          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