2

I am trying to learn Qt and QML and I have hitten a wall. I want to be able to determine the screen factor of the display and choose a background image accordingly, but I cannot. This is my code:

import QtQuick 2.2
import QtQuick.Window 2.2

Image {
    id: root
    width: screenGeometry.width
    height: screenGeometry.height
    property real ratio: root.width / root.height

    source: {
        source = "../components/artwork/background.png"
        if (ratio == 16.0 / 9.0) {
            source = "../components/artwork/background_169.png"
        }
        else if (ratio == 16.0 / 10.0) {
        source = "../components/artwork/background_1610.png"
        }
        else if (ratio == 4.0 / 3.0) {
            source = "../components/artwork/background_43.png"
        }
    }
    fillMode: Image.PreserveAspectFit
}

Independently from the screen size, it always choose the generic one. From what I could see trying to debug the problem, it seems that the value of variable ratio is not understood within the if statement. Searching for similar topics, I have found this one, but I can't still see the error.

Your help is much appreciated!

Community
  • 1
  • 1
  • 4
    Are you sure your problem is not the '=='. Almost always a bug to compare doubles with '=='. – Greenflow May 09 '15 at 13:34
  • You shall not compare two floating-point numbers. Compare them with a small epsilon 1e-6, or use some built-in methods, like qFuzzyCompare in Qt C++. – p.i.g. May 09 '15 at 13:36
  • Or, even better, just assign the images to ranges of ratio (if ratio > x ... else if ratio > y ...) and clip or scale them should the ratio not match exactly. – Frank Osterfeld May 09 '15 at 16:36
  • Thanks! I overlooked to that like an absolute beginner! Ypour help is much appreciated! – Anonimo Bergamo May 09 '15 at 21:05

0 Answers0