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?

How to check screen dpi before a QApplication exists?

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 5.2k Views
  • 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 Vadi2
    2 Feb 2019, 02:48

    AA_EnableHighDpiScaling needs to be set before a QApplication is started, but on Linux setting this property for a normal non-DPI display will cause everything to be 2x the size on Qt versions under 5.12.

    Thus, I need to check the screen size before setting this property, but the screen size isn't available before the application...

    How can I work around this Qt bug? I support Qt 5.7+

    K Offline
    K Offline
    kenchan
    wrote on 2 Feb 2019, 04:21 last edited by
    #2

    @Vadi2
    so why not set it anyway with the static function before you create the QApplication object then reset it as appropriate when you can get the information? All this can happen before you create your GUI objects right?

    1 Reply Last reply
    1
    • V Offline
      V Offline
      Vadi2
      wrote on 2 Feb 2019, 14:00 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
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 2 Feb 2019, 14:31 last edited by mrjj 2 Feb 2019, 14:51
        #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 2 Feb 2019, 15:22 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.

          M 1 Reply Last reply 2 Feb 2019, 15:36
          0
          • V Vadi2
            2 Feb 2019, 15:22

            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.

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 2 Feb 2019, 15:36 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 2 Feb 2019, 16:35 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.

              M 1 Reply Last reply 2 Feb 2019, 16:39
              1
              • V Vadi2
                2 Feb 2019, 16:35

                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.

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 2 Feb 2019, 16:39 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 3 Feb 2019, 03:38 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
                  • M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 3 Feb 2019, 10:38 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 4 Feb 2019, 03:09 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 13 Feb 2019, 16:31 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