Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to get accurate screen size of monitor



  • I'm currently writing a new QML app that will display the app depending on the size of the monitor of the user. If the user's screen size is < 11 inches (windows tablet), I'd display the app in a maximized window. If it's > 11 inches, I'd display the app 70% of the screen.

    To find out the size of the monitor, I had the following code:

    function shouldDisplayFullScreen() {
     if(((Math.sqrt(Math.pow(Screen.width, 2) + Math.pow(Screen.height, 2))) / Screen.pixelDensity / 25.4) < 11) {
      return true;
     } else {
      return false;
     }
    }
    

    As a test on my 1440p 27" monitor, my console displayed the following:
    Screen width: 2560
    Screen height: 1440
    Screen pixelDensity: 2.8348200660963894
    Screen diagonal size in pixels: 2937.2095601097312

    If I did 2937 / 2.83 / 25.4 (to get the screen size in inches), I get something around 40"

    Is my calculation wrong? Does Screen.pixelDensity really represent x pixels in 1 millimeter? Any simpler way of calculating the user's monitor size?


  • Moderators

    Citing the documentation: "Depending on what information the underlying system provides the value might not be entirely accurate.".

    It's definitely not accurate on Android devices. Perhaps you've just confirmed this to also fail on Windows.

    Any simpler way of calculating the user's monitor size?

    Yes, just use Window.show() and it will automatically call showFullScreen() on mobile devices and showNormal() on desktops.

    Or check out physicalDotsPerInch from QScreen. Warning: it will probably be exactly as inaccurate as Screen in QML.


Log in to reply