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. How to check screen dpi before a QApplication exists?
Forum Updated to NodeBB v4.3 + New Features

How to check screen dpi before a QApplication exists?

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 5.6k Views 3 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.
  • V Offline
    V Offline
    Vadi2
    wrote on last edited by
    #3

    That won't work, you have to enable or disable it before the application is created.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #4

      Hi
      I have no hires screen to test on so just asking.
      You are saying that

      #include "mainwindow.h"
      #include <QApplication>
      #include <QScreen>
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          if ( a.screens().at(0)->geometry().width() > 2000) // 2000 is just random value. 
              QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true);
          else {
              QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling, false );
          }
          MainWindow w;
          w.show();
      
          return a.exec();
      }
      
      

      Does not fix the issue , turn on/off the scaling ?

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Vadi2
        wrote on last edited by
        #5

        Don't need a high-res display: the bug is on normal, non-high res displays. Using Qt 5.6 - 5.11, enable AA_EnableHighDpiScaling and watch everything blow up in size :(

        and yes, setting the property after the initialisation doesn't seem to affect anything.

        mrjjM 1 Reply Last reply
        0
        • V Vadi2

          Don't need a high-res display: the bug is on normal, non-high res displays. Using Qt 5.6 - 5.11, enable AA_EnableHighDpiScaling and watch everything blow up in size :(

          and yes, setting the property after the initialisation doesn't seem to affect anything.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #6

          @Vadi2
          hi
          and i assume that Qt::AA_DisableHighDpiScaling suffer the same thing as not change anything
          if done in main ?

          1 Reply Last reply
          0
          • V Offline
            V Offline
            Vadi2
            wrote on last edited by
            #7

            I'm not sure what do you mean - but I've already tried setting Qt::AA_DisableHighDpiScaling after my application was created and it was too late.

            mrjjM 1 Reply Last reply
            1
            • V Vadi2

              I'm not sure what do you mean - but I've already tried setting Qt::AA_DisableHighDpiScaling after my application was created and it was too late.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #8

              @Vadi2
              Yes that was what i meant.
              It seems to be changed in Qt5.12 as i cant get it to show as (too) big.

              When using Qt5.6, how do you enable AA_EnableHighDpiScaling ?
              Just in case i want to test on that version.

              1 Reply Last reply
              1
              • V Offline
                V Offline
                Vadi2
                wrote on last edited by
                #9

                Just do it before the QApplication

                int main(int argc, char *argv[])
                {
                    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true);
                    QApplication a(argc, argv);
                    MainWindow w;
                    w.show();
                
                    return a.exec();
                }
                

                With it on (bad):

                0_1549165072716_MainWindow_345.png

                With it off (good):

                0_1549165115372_MainWindow_346.png

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  Hi
                  Oddly enough i installed Qt5.6 and tried on windows 10 but
                  didnt change the Widgets even with (Qt::AA_EnableHighDpiScaling, true);

                  Anyway, i was wondering if a workaround would be

                  int main(int argc, char *argv[])
                  {
                      bool EnableHighDpiScaling = false;
                  
                      {
                          QApplication a(argc, argv);
                          if ( a.screens().at(0)->geometry().width() > 1090) // check somehow. DPI i assume
                              EnableHighDpiScaling = true;
                      }
                  
                      QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling, EnableHighDpiScaling);
                  
                      QApplication a(argc, argv);
                      MainWindow w;
                      w.show();
                  
                      return a.exec();
                  }
                  
                  

                  It cost an extra construction of a QApplication but the added startup time should be negligible ?

                  1 Reply Last reply
                  1
                  • V Offline
                    V Offline
                    Vadi2
                    wrote on last edited by
                    #11

                    This might be a Linux-only bug, as Windows does HiDPI differently (as does macOS).

                    I'll try your suggestion!

                    1 Reply Last reply
                    1
                    • V Offline
                      V Offline
                      Vadi2
                      wrote on last edited by
                      #12

                      Works, thanks a lot!

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved