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
Forum Updated to NodeBB v4.3 + New Features

Get screensize

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 3 Posters 3.3k Views 1 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.
  • N Offline
    N Offline
    Natural_Bugger
    wrote on 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

    sierdzioS 1 Reply Last reply
    0
    • N Natural_Bugger

      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

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on 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
      1
      • sierdzioS sierdzio

        @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 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

        JonBJ sierdzioS 2 Replies Last reply
        0
        • N Natural_Bugger

          @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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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
          0
          • N Natural_Bugger

            @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

            sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on 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(:^

            JonBJ N 2 Replies Last reply
            1
            • JonBJ JonB

              @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 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

              JonBJ 1 Reply Last reply
              0
              • sierdzioS sierdzio

                Woah, that's bizarre!

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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 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
                • sierdzioS sierdzio

                  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 last edited by
                  #8

                  @sierdzio

                  indeed!

                  looking again, you're right!

                  1 Reply Last reply
                  0
                  • N Natural_Bugger

                    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

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on 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
                    • sierdzioS Offline
                      sierdzioS Offline
                      sierdzio
                      Moderators
                      wrote on 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
                      0
                      • JonBJ JonB

                        @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 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?

                        JonBJ 1 Reply Last reply
                        0
                        • N Natural_Bugger

                          @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?

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on 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
                          0
                          • sierdzioS sierdzio

                            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 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                                                                      ^
                            
                            sierdzioS 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @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 last edited by
                              #14

                              @JonB

                              it return: cba

                              JonBJ 1 Reply Last reply
                              0
                              • N Natural_Bugger

                                @JonB

                                it return: cba

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on 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
                                0
                                • N Natural_Bugger

                                  @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                                                                      ^
                                  
                                  sierdzioS Offline
                                  sierdzioS Offline
                                  sierdzio
                                  Moderators
                                  wrote on 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
                                  • JonBJ JonB

                                    @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 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

                                    JonBJ 1 Reply Last reply
                                    0
                                    • N Natural_Bugger

                                      @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

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on 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
                                      • sierdzioS Offline
                                        sierdzioS Offline
                                        sierdzio
                                        Moderators
                                        wrote on last edited by
                                        #19

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

                                        (Z(:^

                                        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