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 get actual device screen width in pixels
Forum Updated to NodeBB v4.3 + New Features

How get actual device screen width in pixels

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 2.6k Views 2 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.
  • R Offline
    R Offline
    Rory_1
    wrote on last edited by
    #1

    I need the actual screen dimensions in pixels running in mac os. A little research suggests I want to make the call:

    #if defined(Q_OS_MAC)
    int screenWidth = CGDisplayPixelsWide(CGMainDisplayID());
    #endif
    

    However, I need a header file to make this work (if this is the correct call). Can anyone help me with the header I need or another approach to get this info. Thanks in advance.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rory_1
      wrote on last edited by Rory_1
      #2

      Okay, I got it to work by adding

      mac:LIBS += -framework ApplicationServices
      

      to my .pro file and using the header

      #include <ApplicationServices/ApplicationServices.h>
      

      However, this returned 1920 pixels, the same as Qt does in QScreen. This is the virtual pixels reported by Mac OS. Any ideas on getting the actual number of device pixels (2880 in my case)?

      R 1 Reply Last reply
      0
      • R Rory_1

        Okay, I got it to work by adding

        mac:LIBS += -framework ApplicationServices
        

        to my .pro file and using the header

        #include <ApplicationServices/ApplicationServices.h>
        

        However, this returned 1920 pixels, the same as Qt does in QScreen. This is the virtual pixels reported by Mac OS. Any ideas on getting the actual number of device pixels (2880 in my case)?

        R Offline
        R Offline
        Rory_1
        wrote on last edited by
        #3

        It appears what I need is in NSScreen backingScaleFactor. I've just learned what a .mm file is and I don't know anything about object c. How do I access the backingScaleFactor from my Qt c++ mainwindow class?

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

          Hi,

          That's Objective-C++, you can take a look at the QtMacExtras framework as a simple base on how to use that.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          R 1 Reply Last reply
          1
          • SGaistS SGaist

            Hi,

            That's Objective-C++, you can take a look at the QtMacExtras framework as a simple base on how to use that.

            R Offline
            R Offline
            Rory_1
            wrote on last edited by
            #5

            @SGaist

            Thanks for the guidance. :*)

            R 1 Reply Last reply
            0
            • R Rory_1

              @SGaist

              Thanks for the guidance. :*)

              R Offline
              R Offline
              Rory_1
              wrote on last edited by Rory_1
              #6

              I've given objective c a try but I'm getting a "expected unqualified-id" error:

              expected unqualified-id >>>>>>>>>>>>>>>>>>>>>>>>>>>NSObjRuntime.h
              @class NSString, Protocol;
              ^

              My Qt calling class header includes

              #include <MacOs/macscale.h>
              

              MacOs/macscale.h

              #ifndef MACSCALE_H
              #define MACSCALE_H
              
              #include <QtCore/qglobal.h>
              #import <Foundation/Foundation.h>
              namespace QtMac {
              Q_DECL_EXPORT CGFloat macBackingScaleFactor();
              }  // namespace QtMac
              #endif // MACSCALE_H
              

              MacOs/macscale.mm

              #include "MacOS/macScale.h"
              #import "macScale.h"
              #import <AppKit/AppKit.h>
              
              namespace QtMac {
              CGFloat macBackingScaleFactor() {
                  return [[NSScreen mainScreen] backingScaleFactor];
              }
              }  // namespace QtMac
              

              I've added to my pro file

              OBJECTIVE_SOURCES += MacOS/macScale.mm
              

              And finally, in the Qt calling class I use

              QVariant bSF = QtMac::macBackingScaleFactor();
              

              Any ideas on what I'm doing wrong?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                The idea of Objective-C++ is to shield the interface from the Objective-C part.

                Remove the import statement from your header and use float as return type.

                By the way, your use of the Q_DECL_EXPORT macro is wrong. Take a look at its documentation for the correct use.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                R 2 Replies Last reply
                1
                • SGaistS SGaist

                  The idea of Objective-C++ is to shield the interface from the Objective-C part.

                  Remove the import statement from your header and use float as return type.

                  By the way, your use of the Q_DECL_EXPORT macro is wrong. Take a look at its documentation for the correct use.

                  R Offline
                  R Offline
                  Rory_1
                  wrote on last edited by
                  #8

                  @SGaist
                  Thanks. I really appreciate the help. Back to the books!

                  1 Reply Last reply
                  0
                  • SGaistS SGaist

                    The idea of Objective-C++ is to shield the interface from the Objective-C part.

                    Remove the import statement from your header and use float as return type.

                    By the way, your use of the Q_DECL_EXPORT macro is wrong. Take a look at its documentation for the correct use.

                    R Offline
                    R Offline
                    Rory_1
                    wrote on last edited by
                    #9

                    @SGaist said in How get actual device screen width in pixels:

                    Q_DECL_EXPORT

                    I got it to work. I was missing this line in my pro file:

                    mac:LIBS += -framework AppKit
                    

                    As always, the help is very much appreciated. Have a great time over the holiday season!

                    1 Reply Last reply
                    2

                    • Login

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