Qt獲取屏幕DPI


Qt在使用一些功能時需要用到DPI

Qt5.6已經可以自適應DPI與手動設置DPI
方案:

# 實測MSVC與MinGW都可運行
#include <QPaintDevice>  
//邏輯DPI
int horizontalDPI = logicalDpiX(); 
int verticalDPI  = logicalDpiY();  
//物理DPI (和邏輯DPI不一定相同)這個是屏幕的真實DPI
int horizontalDPI = physicalDpiX(); 
int verticalDPI  = physicalDpiY(); 

下面是參考網上的方法的討論!


# 1. 使用MSVC
// Get desktop dc
HDC desktopDc = GetDC(NULL);
// Get native resolution
float horizontalDPI = GetDeviceCaps(desktopDc, LOGPIXELSX);
float verticalDPI = GetDeviceCaps(desktopDc, LOGPIXELSY);

int dpi = (horizontalDPI + verticalDPI) / 2; 
int fontsize = 4 * dpi / 72; //8pt 144dpi is 4px 
fontsize = fontsize > 8 ? fontsize : 8;
QFont MenuFont("SimHei", fontsize);
QApplication::setFont(MenuFont);
另一種:
#include <QScreen>
QScreen *screen = QGuiApplication::screens()[0];      //屏幕分辨率寬度
qreal dpiX = screen->physicalDotsPerInchX();
qreal dpiY = screen->physicalDotsPerInchY();
qreal dpi = screen->physicalDotsPerInch();
qDebug() << "dpi:" << dpi << " dpiX:" << dpiX << " dpiY:" << dpiY;

# 2. 使用 MinGW
#include <QPaintDevice>  
//邏輯DPI
int horizontalDPI = logicalDpiX(); 
int verticalDPI  = logicalDpiY();  
//物理DPI (和邏輯DPI不一定相同)這個是屏幕的真實DPI
int horizontalDPI = physicalDpiX(); 
int verticalDPI  = physicalDpiY(); 

# 3. 分辨率
#include <QDesktopWidget>
  
int currentScreenWidth = QApplication::desktop()->width();
int currentScreenHeight = QApplication::desktop()->height(); 
//或者 
QDesktopWidget* desktopWidget = QApplication::desktop();
//獲取可用桌面大小
QRect deskRect = desktopWidget->availableGeometry();
//獲取設備屏幕大小
QRect screenRect = desktopWidget->screenGeometry(); 
screenX = screenRect.width();
screenY = screenRect.height();  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM