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 retrieve laptop's 'Display resolution'?
QtWS25 Last Chance

How to retrieve laptop's 'Display resolution'?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 481 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.
  • B Offline
    B Offline
    Bart_Vandewoestyne
    wrote on 20 May 2021, 11:22 last edited by
    #1

    I'm a bit confused... Using Qt 5.15.1, I would like to retrieve the "Display resolution" of my laptop (no external screens attached), as given in Windows 10 as follows:

    8f91241a-d611-4463-981e-75ec6176eeeb-image.png

    Since according to https://doc.qt.io/qt-5/qdesktopwidget.html the QDesktopWidget class is obsolete, I want to avoid using that, so I tried using QScreen in the following way:

    const auto screens = QApplication::screens();
    for (const auto& screen : screens)
    {
        const auto geometry = screen->geometry();
        const auto width = geometry.width();
        const auto height = geometry.height();
    }
    

    However, when I try this, it appears that screens has size 0, so no screens are found...

    Am I on the right track to retrieve my Display resolution, or am I doing stuff completely wrong and should I use something else than QScreen?

    J J 2 Replies Last reply 20 May 2021, 11:30
    0
    • B Bart_Vandewoestyne
      20 May 2021, 11:22

      I'm a bit confused... Using Qt 5.15.1, I would like to retrieve the "Display resolution" of my laptop (no external screens attached), as given in Windows 10 as follows:

      8f91241a-d611-4463-981e-75ec6176eeeb-image.png

      Since according to https://doc.qt.io/qt-5/qdesktopwidget.html the QDesktopWidget class is obsolete, I want to avoid using that, so I tried using QScreen in the following way:

      const auto screens = QApplication::screens();
      for (const auto& screen : screens)
      {
          const auto geometry = screen->geometry();
          const auto width = geometry.width();
          const auto height = geometry.height();
      }
      

      However, when I try this, it appears that screens has size 0, so no screens are found...

      Am I on the right track to retrieve my Display resolution, or am I doing stuff completely wrong and should I use something else than QScreen?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 20 May 2021, 11:30 last edited by jsulm
      #2

      @Bart_Vandewoestyne said in How to retrieve laptop's 'Display resolution'?:

      it appears

      it appears or did you check it is really 0? Because inside the loop you're defining local variables without doing anything with them.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      B 1 Reply Last reply 20 May 2021, 11:47
      3
      • B Bart_Vandewoestyne
        20 May 2021, 11:22

        I'm a bit confused... Using Qt 5.15.1, I would like to retrieve the "Display resolution" of my laptop (no external screens attached), as given in Windows 10 as follows:

        8f91241a-d611-4463-981e-75ec6176eeeb-image.png

        Since according to https://doc.qt.io/qt-5/qdesktopwidget.html the QDesktopWidget class is obsolete, I want to avoid using that, so I tried using QScreen in the following way:

        const auto screens = QApplication::screens();
        for (const auto& screen : screens)
        {
            const auto geometry = screen->geometry();
            const auto width = geometry.width();
            const auto height = geometry.height();
        }
        

        However, when I try this, it appears that screens has size 0, so no screens are found...

        Am I on the right track to retrieve my Display resolution, or am I doing stuff completely wrong and should I use something else than QScreen?

        J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 20 May 2021, 11:32 last edited by
        #3

        @Bart_Vandewoestyne works perfectly fine for me, it it is even from inside a VM

        24a39ac1-4bb6-4655-8a52-389d7e3aee74-image.png


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        4
        • J jsulm
          20 May 2021, 11:30

          @Bart_Vandewoestyne said in How to retrieve laptop's 'Display resolution'?:

          it appears

          it appears or did you check it is really 0? Because inside the loop you're defining local variables without doing anything with them.

          B Offline
          B Offline
          Bart_Vandewoestyne
          wrote on 20 May 2021, 11:47 last edited by
          #4

          @jsulm said in How to retrieve laptop's 'Display resolution'?:

          it appears or did you check it is really 0? Because inside the loop you're defining local variables without doing anything with them.

          I did check it as part of a larger application and it was really 0. However, I now also tried the following trimmed down minimal example:

          #include <QApplication>
          #include <QScreen>
          #include <QDebug>
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
          
              const auto screens = QApplication::screens();
              qDebug() << screens.size();
              for (const auto& screen : screens)
              {
                  const auto geometry = screen->geometry();
                  const auto screenName = screen->name();
                  const auto width = geometry.width();
                  const auto height = geometry.height();
                  qDebug() << width << height;
              }
          }
          

          and .pro file

          TEMPLATE = app
          TARGET = qscreen
          INCLUDEPATH += .
          
          QT += widgets
          CONFIG += console 
          
          SOURCES += qscreen.cpp
          

          and that works. So I'll now have to figure out what differences there are between this trimmed down program and my larger application. Stay tuned! I'll report back later! :-)

          B 1 Reply Last reply 20 May 2021, 16:27
          3
          • B Bart_Vandewoestyne
            20 May 2021, 11:47

            @jsulm said in How to retrieve laptop's 'Display resolution'?:

            it appears or did you check it is really 0? Because inside the loop you're defining local variables without doing anything with them.

            I did check it as part of a larger application and it was really 0. However, I now also tried the following trimmed down minimal example:

            #include <QApplication>
            #include <QScreen>
            #include <QDebug>
            
            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
            
                const auto screens = QApplication::screens();
                qDebug() << screens.size();
                for (const auto& screen : screens)
                {
                    const auto geometry = screen->geometry();
                    const auto screenName = screen->name();
                    const auto width = geometry.width();
                    const auto height = geometry.height();
                    qDebug() << width << height;
                }
            }
            

            and .pro file

            TEMPLATE = app
            TARGET = qscreen
            INCLUDEPATH += .
            
            QT += widgets
            CONFIG += console 
            
            SOURCES += qscreen.cpp
            

            and that works. So I'll now have to figure out what differences there are between this trimmed down program and my larger application. Stay tuned! I'll report back later! :-)

            B Offline
            B Offline
            Bart_Vandewoestyne
            wrote on 20 May 2021, 16:27 last edited by
            #5

            @Bart_Vandewoestyne said in How to retrieve laptop's 'Display resolution'?:

            So I'll now have to figure out what differences there are between this trimmed down program and my larger application. Stay tuned! I'll report back later! :-)

            I'm a bit confused... as I now have:

            • A trimmed down stand-alone example program that works.
            • A unit test using the Google Test framework that I got working after creating a new Google Test main() function with
              QApplication a(argc, argv);
              
              right before
              return RUN_ALL_TESTS();
              
            • In my larger application, I have main thread where, the first statement in my main() function is
              QCoreApplication app(argc, argv);
              
              Next to the main thread, I have another thread with an event loop, and it is in that thread that I'm trying to fetch the screen info, but it fails.

            Any idea what could be wrong here? I'm quite in the dark, since I do call the QCoreApplication constructor in the beginning of my program, and the second thread does have an event loop (although I'm not sure if that is relevant for fetching the screen info)...

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on 20 May 2021, 16:33 last edited by Chris Kawa
              #6

              @Bart_Vandewoestyne said in How to retrieve laptop's 'Display resolution'?:

              I'm quite in the dark, since I do call the QCoreApplication constructor in the beginning of my program

              QCoreApplication is the base class for non-UI applications.
              For UI stuff (desktop included) you need at least QGuiApplication or QApplication for widget based app.

              B 1 Reply Last reply 26 May 2021, 07:25
              5
              • C Chris Kawa
                20 May 2021, 16:33

                @Bart_Vandewoestyne said in How to retrieve laptop's 'Display resolution'?:

                I'm quite in the dark, since I do call the QCoreApplication constructor in the beginning of my program

                QCoreApplication is the base class for non-UI applications.
                For UI stuff (desktop included) you need at least QGuiApplication or QApplication for widget based app.

                B Offline
                B Offline
                Bart_Vandewoestyne
                wrote on 26 May 2021, 07:25 last edited by
                #7

                @Chris-Kawa said in How to retrieve laptop's 'Display resolution'?:

                QCoreApplication is the base class for non-UI applications.
                For UI stuff (desktop included) you need at least QGuiApplication or QApplication for widget based app.

                I indeed overlooked that one. After switching from QCoreApplicatio to QGuiApplication everything works as expected and my screen is detected.

                Thanks!

                1 Reply Last reply
                1

                3/7

                20 May 2021, 11:32

                • Login

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