Qt获取屏幕DPI和分辨率

本文章向大家介绍Qt获取屏幕DPI和分辨率,主要包括Qt获取屏幕DPI和分辨率使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
DPI
Qt+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); 

Qt+MinGW

#include <QPaintDevice>  
//逻辑DPI
    int horizontalDPI = logicalDpiX(); 
    int verticalDPI  = logicalDpiY();  
//物理DPI (和逻辑DPI不一定相同)
    int horizontalDPI = physicalDpiX(); 
    int verticalDPI  = physicalDpiY();   

分辨率

#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();  

原文地址:http://www.manongjc.com/article/49204.html

Last modification:February 17th, 2020 at 12:20 pm
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment