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. Get screensize

Get screensize

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 3 Posters 2.9k 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.
  • N Offline
    N Offline
    Natural_Bugger
    wrote on 13 Aug 2020, 08:03 last edited by Natural_Bugger
    #1

    hello,

    I'm trying to get the screen size for my desktop app.

    project file:

    QT       += core gui
    

    headers

    #include <qrect.h>
    #include <qscreen.h>
    #include <QMessageBox>
    
    QScreen *screen = QGuiApplication::primaryScreen();
         QRect  screenGeometry = screen->geometry();
         int height = screenGeometry.height();
         int width = screenGeometry.width();
    
    QMessageBox msgBox;
             msgBox.setText((QString)width + " x " + (QString)height);
             msgBox.exec();
    
    

    or

    QSize size = qApp->screens()[0]->size();
    QMessageBox msgBox;
             msgBox.setText(((QString)size.width()+ " x " + (QString)size.height() );
             msgBox.exec();
    

    but both methods return "garbage", what am i missing?

    regards

    S 1 Reply Last reply 13 Aug 2020, 08:16
    0
    • N Natural_Bugger
      13 Aug 2020, 08:03

      hello,

      I'm trying to get the screen size for my desktop app.

      project file:

      QT       += core gui
      

      headers

      #include <qrect.h>
      #include <qscreen.h>
      #include <QMessageBox>
      
      QScreen *screen = QGuiApplication::primaryScreen();
           QRect  screenGeometry = screen->geometry();
           int height = screenGeometry.height();
           int width = screenGeometry.width();
      
      QMessageBox msgBox;
               msgBox.setText((QString)width + " x " + (QString)height);
               msgBox.exec();
      
      

      or

      QSize size = qApp->screens()[0]->size();
      QMessageBox msgBox;
               msgBox.setText(((QString)size.width()+ " x " + (QString)size.height() );
               msgBox.exec();
      

      but both methods return "garbage", what am i missing?

      regards

      S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 13 Aug 2020, 08:16 last edited by
      #2

      @Natural_Bugger said in Get screensize:

      msgBox.setText((QString)width + " x " + (QString)height);

      You should use QString::number() instead:

      msgBox.setText(QString::number(width) + " x " + QString::number(height));
      

      (Z(:^

      N 1 Reply Last reply 13 Aug 2020, 08:22
      1
      • S sierdzio
        13 Aug 2020, 08:16

        @Natural_Bugger said in Get screensize:

        msgBox.setText((QString)width + " x " + (QString)height);

        You should use QString::number() instead:

        msgBox.setText(QString::number(width) + " x " + QString::number(height));
        
        N Offline
        N Offline
        Natural_Bugger
        wrote on 13 Aug 2020, 08:22 last edited by Natural_Bugger
        #3

        @sierdzio

        thnx,

        it works perfect for the first method.
        but not for the second.

        QSize size = qApp->screens()[0]->size();
        QMessageBox msgBox;
                 msgBox.setText(QString::number(size.width()) + " x " + QString::number(size.height()));
                 msgBox.exec();
        

        it returns:

        0801 x 0291
        

        when it should return 1920 x 1080

        J S 2 Replies Last reply 13 Aug 2020, 08:28
        0
        • N Natural_Bugger
          13 Aug 2020, 08:22

          @sierdzio

          thnx,

          it works perfect for the first method.
          but not for the second.

          QSize size = qApp->screens()[0]->size();
          QMessageBox msgBox;
                   msgBox.setText(QString::number(size.width()) + " x " + QString::number(size.height()));
                   msgBox.exec();
          

          it returns:

          0801 x 0291
          

          when it should return 1920 x 1080

          J Offline
          J Offline
          JonB
          wrote on 13 Aug 2020, 08:28 last edited by JonB
          #4

          @Natural_Bugger
          Well the code looks right to me now. Just in case, what does qApp->screens().count() return ?

          I'm "surprised" at one thing. Your output 0801 & 0291 have leading 0s, and are 4 digits long. That does not look like the default output from QString::number(), what is going on?

          EDIT Oh, I see what @sierdzio is saying below. If you read them right-to-left then you do get 1920 x 1080. I think somewhere you have indeed set for Right-to-Left text output! Change your string " x " over to " <<< " and see which direction those chevrons come out --- is it indeed >>>?! EDIT My bad. Corrected in my post below.

          N 2 Replies Last reply 13 Aug 2020, 08:37
          0
          • N Natural_Bugger
            13 Aug 2020, 08:22

            @sierdzio

            thnx,

            it works perfect for the first method.
            but not for the second.

            QSize size = qApp->screens()[0]->size();
            QMessageBox msgBox;
                     msgBox.setText(QString::number(size.width()) + " x " + QString::number(size.height()));
                     msgBox.exec();
            

            it returns:

            0801 x 0291
            

            when it should return 1920 x 1080

            S Offline
            S Offline
            sierdzio
            Moderators
            wrote on 13 Aug 2020, 08:29 last edited by
            #5

            Woah, that's bizarre!

            The only thing that comes to my mind is an RTL language, but even that should not change number direction.

            (Z(:^

            J N 2 Replies Last reply 13 Aug 2020, 08:37
            1
            • J JonB
              13 Aug 2020, 08:28

              @Natural_Bugger
              Well the code looks right to me now. Just in case, what does qApp->screens().count() return ?

              I'm "surprised" at one thing. Your output 0801 & 0291 have leading 0s, and are 4 digits long. That does not look like the default output from QString::number(), what is going on?

              EDIT Oh, I see what @sierdzio is saying below. If you read them right-to-left then you do get 1920 x 1080. I think somewhere you have indeed set for Right-to-Left text output! Change your string " x " over to " <<< " and see which direction those chevrons come out --- is it indeed >>>?! EDIT My bad. Corrected in my post below.

              N Offline
              N Offline
              Natural_Bugger
              wrote on 13 Aug 2020, 08:37 last edited by
              #6

              thnx @JonB

              QMessageBox msgBox;
                  msgBox.setText(QString::number(qApp->screens().count()));
                  msgBox.exec();
              

              returns 1

              but when i change ...

              QSize size = qApp->screens()[0]->size();
              

              to

              QSize size = qApp->screens()[1]->size();
              

              it doesn't compile

              J 1 Reply Last reply 13 Aug 2020, 08:39
              0
              • S sierdzio
                13 Aug 2020, 08:29

                Woah, that's bizarre!

                The only thing that comes to my mind is an RTL language, but even that should not change number direction.

                J Offline
                J Offline
                JonB
                wrote on 13 Aug 2020, 08:37 last edited by
                #7

                @sierdzio
                Excellent spot, as I have edited my response above! But it's not " change number direction", because the numbers have been changed to strings!

                1 Reply Last reply
                0
                • S sierdzio
                  13 Aug 2020, 08:29

                  Woah, that's bizarre!

                  The only thing that comes to my mind is an RTL language, but even that should not change number direction.

                  N Offline
                  N Offline
                  Natural_Bugger
                  wrote on 13 Aug 2020, 08:38 last edited by
                  #8

                  @sierdzio

                  indeed!

                  looking again, you're right!

                  1 Reply Last reply
                  0
                  • N Natural_Bugger
                    13 Aug 2020, 08:37

                    thnx @JonB

                    QMessageBox msgBox;
                        msgBox.setText(QString::number(qApp->screens().count()));
                        msgBox.exec();
                    

                    returns 1

                    but when i change ...

                    QSize size = qApp->screens()[0]->size();
                    

                    to

                    QSize size = qApp->screens()[1]->size();
                    

                    it doesn't compile

                    J Offline
                    J Offline
                    JonB
                    wrote on 13 Aug 2020, 08:39 last edited by JonB
                    #9

                    @Natural_Bugger said in Get screensize:

                    QSize size = qApp->screens()[1]->size();

                    Anyway, I now see you do indeed only have one screen. Just forget about this area.

                    In any case, I have edited my answer. Somewhere I really think you have a text right-to-left issue! Do what I have edited above so that the string has literal <<< in it, which direction do those come out?

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      sierdzio
                      Moderators
                      wrote on 13 Aug 2020, 08:42 last edited by
                      #10

                      Yeah I'm right but I shouldn't be :D RTL should not invert numbers, as far as I know, only regular text.

                      Perhaps this will work better (https://doc.qt.io/qt-5/qlocale.html#toString-6):

                      QLocale::toString(size.width()) + " x " + QLocale::toStringsize.height())
                      

                      (Z(:^

                      N 1 Reply Last reply 13 Aug 2020, 08:46
                      0
                      • J JonB
                        13 Aug 2020, 08:28

                        @Natural_Bugger
                        Well the code looks right to me now. Just in case, what does qApp->screens().count() return ?

                        I'm "surprised" at one thing. Your output 0801 & 0291 have leading 0s, and are 4 digits long. That does not look like the default output from QString::number(), what is going on?

                        EDIT Oh, I see what @sierdzio is saying below. If you read them right-to-left then you do get 1920 x 1080. I think somewhere you have indeed set for Right-to-Left text output! Change your string " x " over to " <<< " and see which direction those chevrons come out --- is it indeed >>>?! EDIT My bad. Corrected in my post below.

                        N Offline
                        N Offline
                        Natural_Bugger
                        wrote on 13 Aug 2020, 08:42 last edited by
                        #11

                        @JonB said in Get screensize:
                        I think somewhere you have indeed set for Right-to-Left text output! Change your string " x " over to " <<< " and see which direction those chevrons come out --- is it indeed >>>?!

                        thank you for your answer.

                        chevrons?

                        what do i need to do?

                        J 1 Reply Last reply 13 Aug 2020, 08:43
                        0
                        • N Natural_Bugger
                          13 Aug 2020, 08:42

                          @JonB said in Get screensize:
                          I think somewhere you have indeed set for Right-to-Left text output! Change your string " x " over to " <<< " and see which direction those chevrons come out --- is it indeed >>>?!

                          thank you for your answer.

                          chevrons?

                          what do i need to do?

                          J Offline
                          J Offline
                          JonB
                          wrote on 13 Aug 2020, 08:43 last edited by JonB
                          #12

                          @Natural_Bugger said in Get screensize:

                          what do i need to do?

                          I told you. Change your string " x " to " <<< ", how much clearer can I be?

                          msgBox.setText(QString::number(size.width()) + " <<< " + QString::number(size.height()));
                          

                          EDIT I'm sorry, my bad, I've just realized I was thinking too literally, like a mirror!

                          Try:

                          msgBox.setText(QString::number(size.width()) + " abc " + QString::number(size.height()));
                          

                          I want to know whether that comes out as abc or as cba?

                          N 1 Reply Last reply 13 Aug 2020, 08:48
                          0
                          • S sierdzio
                            13 Aug 2020, 08:42

                            Yeah I'm right but I shouldn't be :D RTL should not invert numbers, as far as I know, only regular text.

                            Perhaps this will work better (https://doc.qt.io/qt-5/qlocale.html#toString-6):

                            QLocale::toString(size.width()) + " x " + QLocale::toStringsize.height())
                            
                            N Offline
                            N Offline
                            Natural_Bugger
                            wrote on 13 Aug 2020, 08:46 last edited by
                            #13

                            @sierdzio said in Get screensize:

                            Yeah I'm right but I shouldn't be :D RTL should not invert numbers, as far as I know, only regular text.

                            Perhaps this will work better (https://doc.qt.io/qt-5/qlocale.html#toString-6):

                            QLocale::toString(size.width()) + " x " + QLocale::toStringsize.height())
                            
                            #include <QLocale>
                            

                            returns

                            error: cannot call member function ‘QString QLocale::toString(int) const’ without object                                                                      ^
                            
                            S 1 Reply Last reply 13 Aug 2020, 08:51
                            0
                            • J JonB
                              13 Aug 2020, 08:43

                              @Natural_Bugger said in Get screensize:

                              what do i need to do?

                              I told you. Change your string " x " to " <<< ", how much clearer can I be?

                              msgBox.setText(QString::number(size.width()) + " <<< " + QString::number(size.height()));
                              

                              EDIT I'm sorry, my bad, I've just realized I was thinking too literally, like a mirror!

                              Try:

                              msgBox.setText(QString::number(size.width()) + " abc " + QString::number(size.height()));
                              

                              I want to know whether that comes out as abc or as cba?

                              N Offline
                              N Offline
                              Natural_Bugger
                              wrote on 13 Aug 2020, 08:48 last edited by
                              #14

                              @JonB

                              it return: cba

                              J 1 Reply Last reply 13 Aug 2020, 08:49
                              0
                              • N Natural_Bugger
                                13 Aug 2020, 08:48

                                @JonB

                                it return: cba

                                J Offline
                                J Offline
                                JonB
                                wrote on 13 Aug 2020, 08:49 last edited by
                                #15

                                @Natural_Bugger
                                Yup! So we are all agreed: there is right-to-left going on here, the results of the screen size are correct but are being printed right-to-left! Not my area, but that is what needs sorting out....

                                N 1 Reply Last reply 13 Aug 2020, 08:52
                                0
                                • N Natural_Bugger
                                  13 Aug 2020, 08:46

                                  @sierdzio said in Get screensize:

                                  Yeah I'm right but I shouldn't be :D RTL should not invert numbers, as far as I know, only regular text.

                                  Perhaps this will work better (https://doc.qt.io/qt-5/qlocale.html#toString-6):

                                  QLocale::toString(size.width()) + " x " + QLocale::toStringsize.height())
                                  
                                  #include <QLocale>
                                  

                                  returns

                                  error: cannot call member function ‘QString QLocale::toString(int) const’ without object                                                                      ^
                                  
                                  S Offline
                                  S Offline
                                  sierdzio
                                  Moderators
                                  wrote on 13 Aug 2020, 08:51 last edited by
                                  #16

                                  @Natural_Bugger ok it's not a static method, eh.

                                  Then:

                                  QLocale locale;
                                  msgBox.setText(locale.toString(size.width()) + " x " + locale.toStringsize.height()));
                                  

                                  (Z(:^

                                  1 Reply Last reply
                                  0
                                  • J JonB
                                    13 Aug 2020, 08:49

                                    @Natural_Bugger
                                    Yup! So we are all agreed: there is right-to-left going on here, the results of the screen size are correct but are being printed right-to-left! Not my area, but that is what needs sorting out....

                                    N Offline
                                    N Offline
                                    Natural_Bugger
                                    wrote on 13 Aug 2020, 08:52 last edited by
                                    #17

                                    @JonB @sierdzio

                                    i found the error.

                                    i used a custom button class, i found online and it contains a method to reverse the text.

                                    void CustomButton::reverseText()
                                    {
                                        QString buttonText = text();
                                        std::reverse(buttonText.begin(), buttonText.end());
                                        setText( buttonText );
                                    }
                                    

                                    : )

                                    thank you both

                                    J 1 Reply Last reply 13 Aug 2020, 08:56
                                    0
                                    • N Natural_Bugger
                                      13 Aug 2020, 08:52

                                      @JonB @sierdzio

                                      i found the error.

                                      i used a custom button class, i found online and it contains a method to reverse the text.

                                      void CustomButton::reverseText()
                                      {
                                          QString buttonText = text();
                                          std::reverse(buttonText.begin(), buttonText.end());
                                          setText( buttonText );
                                      }
                                      

                                      : )

                                      thank you both

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 13 Aug 2020, 08:56 last edited by JonB
                                      #18

                                      @Natural_Bugger
                                      Using a custom button which calls reverse is not a good place to start from....

                                      But I don't understand, the code you gave us uses QMessageBox::setText(), which is what we looked at, what has that got to do with any custom button, or the reverseText() method?

                                      1 Reply Last reply
                                      1
                                      • S Offline
                                        S Offline
                                        sierdzio
                                        Moderators
                                        wrote on 13 Aug 2020, 08:56 last edited by
                                        #19

                                        Hah, ok good that you've found it. Happy coding!

                                        (Z(:^

                                        1 Reply Last reply
                                        1

                                        7/19

                                        13 Aug 2020, 08:37

                                        topic:navigator.unread, 12
                                        • Login

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