Qt获取屏幕DPI和分辨率

DPI

1、 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); 

2、Qt+MinGW

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

3、分辨率

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

Qt默认DPI是跟随系统的,Qt5.6之后,也可以人工控制

#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
   QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
    QApplication a(argc, argv);
    return a.exec();

你可能感兴趣的:(QT,Qt+MSVC)