一、多显示器Qt窗口处理
假设包含两块屏幕,分辨率分别为1920✖1080,设置为扩展屏,想将窗口(1920✖1080)显示在第二块屏幕上
- 首先获取屏幕数量
- 若为一块屏幕,setGeometry(0, 0, 1920, 1080)
- 若为两块屏幕,获取主屏幕,若第一块屏幕为主屏幕,setGeometry(1920, 0, 1920,
1080),若第二块屏幕为主屏幕,setGeometry(0, 0, 1920, 1080)
注意:QGuiApplication::primaryScreen()->virtualGeometry(),获取到的主屏幕X、Y都为0,无论主屏幕是第一块屏幕还是第二块屏幕
二、代码
代码如下(示例):
QDesktopWidget* desktopWidget = QApplication::desktop(); //获取设备屏幕大小 int screencount = desktopWidget->screenCount(); this->setGeometry(0, 0, 1920, 1080); if (screencount == 2) { QRect rect = desktopWidget->screenGeometry(screencount - 1); QScreen *primaryScreen = QGuiApplication::primaryScreen(); QRect primaryVirtualGeometry = primaryScreen->virtualGeometry(); int primaryScreenX = primaryVirtualGeometry.x(); int primaryScreenY = primaryVirtualGeometry.y(); if (primaryScreenX == 0 && primaryScreenY == 0) { this->setGeometry(1920, 0, 1920, 1080); } else { this->setGeometry(0, 0, 1920, 1080); } }
推荐一个零声学院项目课,个人觉得老师讲得不错,分享给大家:
零声白金学习卡(含基础架构/高性能存储/golang云原生/音视频/Linux内核)
https://xxetb.xet.tech/s/VsFMs