Reputation: 5444
Working with Qt 4.8.4 on OS X -- Desktop Application development. I need to be able to detect, at paint time, if I am on a hiDPI display ("retina") or not. Does anyone know how to achieve this?
Upvotes: 6
Views: 3310
Reputation: 5444
Eventually I just created a small cocoa function to return this value for me. I use it to determine the time of paintEvent whether I should use hiDPI images. Works like charm on my MacBook Pro 15" Retina.
bool MYAppCocoaServices::isHiDPI(QWidget * widget)
{
NSView* view = reinterpret_cast<NSView*>(widget->winId());
CGFloat scaleFactor = 1.0;
if ([[view window] respondsToSelector: @selector(backingScaleFactor)])
scaleFactor = [[view window] backingScaleFactor];
return (scaleFactor > 1.0);
}
I am building this .mm file conditionally on Mac only and call this static function from my c++ code on Mac.
Upvotes: 4
Reputation: 19107
You can use QScreen
for this in Qt 5, and in Qt 4 you can use the QSystemDisplayInfo
class from Qt Mobility.
There is QSystemDisplayInfo
- http://doc.qt.digia.com/qtmobility/qsystemdisplayinfo.html
The relevant methods are getDPIHeight
and getDPIWidth
.
You could also use QDesktopWidget
's physicalDpiX
and physicalDpiY
methods.
Use QScreen
- http://qt-project.org/doc/qt-5.0/qtgui/qscreen.html#physicalDotsPerInch-prop
((QGuiApplication*)QCoreApplication::instance())
->primaryScreen()->physicalDotsPerInch()
There are also physicalDotsPerInchX
and physicalDotsPerInchY
.
Upvotes: 5