在Qt

时间:2017-12-17 01:06:24

标签: c++ macos qt screen resolution

从Qt应用程序我想报告实际的屏幕分辨率,而不是QScreen提供的虚拟分辨率。我试过了:

[[NSScreen mainScreen] backingScaleFactor]

但它返回与QScreen相同的值(高dpi屏幕为2)。但是,在macOS中,如果用户选择了缩放分辨率,那么这不起作用。

例如,在具有2880x1800分辨率且显示分辨率设置为默认值的macbookpro上

CGDisplayPixelsWide(CGMainDisplayID()) //returns 1440
[[NSScreen mainScreen] backingScaleFactor] //returns 2

2 * 1440 = 2880并且有效

但是,当系统首选项中的显示分辨率设置为更多空间时:

CGDisplayPixelsWide(CGMainDisplayID()) //returns 1920 
[[NSScreen mainScreen] backingScaleFactor] // still returns 2

如何获得实际分辨率?

好的,这是我的解决方案,它将显示器原始分辨率返回到我的应用程序,因为它从一台显示器移动到另一台显示器。

// triggered by app move event
QPoint loc = centralWidget->window()->geometry().center();

// get displayID for monitor at point 
CGPoint point = loc.toCGPoint();
const int maxDisplays = 8; 
CGDisplayCount displayCount; 
CGDirectDisplayID displayIDs[maxDisplays];  
CGGetDisplaysWithPoint (point, maxDisplays, displayIDs, &displayCount);
auto displayID;
if (displayCount == 1) displayID = displayIDs[0];
else displayID = CGMainDisplayID();

// get list of all display modes for the monitor
auto modes = CGDisplayCopyAllDisplayModes(displayID, nullptr);
auto count = CFArrayGetCount(modes);
CGDisplayModeRef mode;
int displayHorizontalPixels, displayVerticalPixels = 0;

// the native resolution is the largest display mode
for(auto c = count; c--;) {
    mode = (CGDisplayModeRef)CFArrayGetValueAtIndex(modes, c);
    auto w = CGDisplayModeGetWidth(mode);
    auto h = CGDisplayModeGetHeight(mode);
    if (w > displayHorizontalPixels) displayHorizontalPixels = (int)w;
    if (h > displayVerticalPixels) displayVerticalPixels = (int)h;
}

0 个答案:

没有答案