0

I have a QIMage that can be in portrait or landscape mode depending upon its width vs height. Or vice versa.

I am trying to fit a portrait QIMage in landscape mode & a landscape QImage in portrait mode without breaking the aspect ratio.

Following is how I am trying to do this:

Window {
  id: app_window
  visible: true

  Rectangle {
    id: my_image_view_container
    width: my_image.sourceSize.width
    height: my_image.sourceSize.height

    // This is a Image item which has to host a portrait/landscape image.jpg in both portrait & landscape modes on an android device
    Image {
       id: my_image
       anchors.fill: parent
       // changes at runtime based on the image my app selects
       source: "random/path/to/an/image.jpg"
       scale: Qt.KeepAspectRatio
    }
  }
}

It basically works to preserve the aspect ratio. The image is not skewed when I resize my app_window from portrait to landscape.

Problem:
But in landscape mode if the app_window's height is less than my_image's height then the image is not fully visible.

Question:
How should I set the size of my_image_view_container & my_image to be able accomplish hosting image.jpg in both landscape & portrait orientations without breaking the aspect ratio as well not missing to view parts of the image in any mode?

PS:
The challenge is for example: I take a QML snapshot & created a QImage which is 100 units width by 300 units height. Now change the orientation of the mobile device or resize window on desktop to landscape. Now, I need to fit the QIMage which is 300 units height into a height of 100 units. How to set the width & height of QIMage to accomplish this nicely? I see that Google Photos on android does it nicely by embedding a black to both side flanks of the image when you view a portrait capture image in landscape mode & vide versa. I wish to achieve exactly this functionality of Google Photos. Should be possible by setting the correct width & height of the Image QML element?

TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159

1 Answers1

1

For your case, you can use Bindings to choose a different image depending of the current pixel density and the screen state. According to the official doc:

Binding enables objects to automatically update their properties in response to changing attributes in other objects or the occurrence of some external event.

Check the documentation about Scalability in modern Qt, I think that can be really useful. You can check the current state of the screen, landscape or portrait situation, easily by comparing the width & height:

 parameter:  width > height ? contition_1 : condition_2

In your case, I will recommend to have 2 different images depending of the state. In the official doc you can find an Orientation Observer that aims to do what you want for the binding. By the way, you can do something like this:

Image {
   source: {
      if (width > height)
        "image_1.png"
      else
        "image_2.png"
    }
}

[UPDATE]

As alternative, if you want to use the same image,you can handle the property Orientation of the Screen class. If you fit the image size, via the property width, height to the Screen.width & Screen.height properties should be enough. If you are planing to use a custom code, then:

Image {
    id: image
    source: "image.jpg"
    width: whatever
    height: whatever 
    fillMode: Image.PreserveAspectFit
    anchors.top: parent.top

    Screen.onOrientationChanged: {
       if (Screen.primaryOrientation === Qt.PortraitOrientation) {
          image.width = 300;
          image.heigh = 100; 
       } else {
          image.width = 100;
          image.heigh = 300;
       }
    }
}
mohabouje
  • 3,867
  • 2
  • 14
  • 28
  • Please note that I do not have display different images based on orientation. Referring to your answer, I need to resize `image_1.png` if else case matches. Not display `image_2.png` which is different image altogether – TheWaterProgrammer Dec 11 '17 at 15:32
  • [Qt Scalability](http://doc.qt.io/qt-5/scalability.html) looks to be something to use when displaying a `hdpi` or `ldpi` image based on `Screen.PixelDensity`. Please note that my case is a very simple case of being able to display a **portrait QImage in landscape mode** or a **landscape QImage in portrait mode** – TheWaterProgrammer Dec 11 '17 at 15:35
  • This is not what I am looking for. I know when the screen changes from landscape to portrait & vice versa. **The challenge :** Say for example you took a snapshot & created a QImage which is 100 units width by 300 units height. Now change the orientation to landscape. I need to fit the QIMage which is 300 units height into a height of 100 units. How to do this? – TheWaterProgrammer Dec 11 '17 at 15:44
  • I have add an update to the answer using the Screen class to handle the change of the current screen state and to change the sizes of the image, You can use the Screen.width && Screen.height to fit the screen. – mohabouje Dec 11 '17 at 16:15