在工作中遇到程序需要对多扩展屏进行显示,之前一直是用QApplication::desktop()->screenGeometry()获取屏幕大小,但是此方法在多个显示器就失效了。不过Qt 中已经有封装好的api针对多屏处理。
1 2 3 |
int primaryScreen(); //获取主屏幕的索引 int screenCount(); //获取系统可用的屏幕数量 QRect screenGeometry(int screen=-1); //获取指定屏幕索引的分辨率,默认屏幕-1,主屏幕一般是0 |
有了这几个函数,我们就可以针对多屏进行处理,获取用户设置的主屏幕索引再将我们的程序设置显示到此屏幕就行了,代码如下
1 2 3 4 5 |
int primaryIndex = desktop->primaryScreen(); if (desktop->screenCount()) { window->setGeometry(desktop->screenGeometry(primaryIndex)); } |