Qt - 多屏处理的函数

Qt中,QDesktopWidget封装了一系列用于多屏处理的函数,头文件:#include <QDesktopWidget>

1、QDesktopWidget::screenCount()//获取当前操作系统的可用屏幕数
2、QDesktopWidget::primaryScreen()//获取主屏幕的索引
3、QDesktopWidget::availableGeometry()//获取可用屏幕大小的矩形
4、QDesktopWidget::screenGeometry(int screen = -1)//获取指定索引下的屏幕。主屏幕的索引一般为0
5、setGeometry(QDesktopWidget::screenGeometry(0));//设置UI到第一个屏幕

 

假如这个窗口的指针为this,记得要加头文件哦

示例1:获得当前窗口所在屏幕的大小

#include <QDesktopWidget>
#include <QApplication>
#include <QScreen>

//获得当前屏幕是第几屏幕
int number = QApplication::desktop()->screenNumber(this);
//如果number是-1会出现崩溃,就是用默认0
if(number<0)
{
    number=0;
}
//根据number获得当前窗口所在屏幕的大小
QSize size = QGuiApplication::screens().at(number)->geometry().size()

示例2:移动窗体到屏幕中央显示

#include <QDesktopWidget>
#include <QApplication>
#include <QScreen>

int index = QApplication::desktop()->screenNumber(this);//获得当前窗口所在屏幕的索引
qDebug() << "index = " << index;

//移动窗体到屏幕中央
QRect rect = frameGeometry();
QScreen* screen = QGuiApplication::screens()[index];//用于获取当前系统上的屏幕列表中的特定索引处的屏幕。
QPoint centerPoint = screen->availableGeometry().center();//将返回屏幕工作区的中心点坐标
rect.moveCenter(centerPoint);
move(rect.topLeft());
show();

QGuiApplication::screens()[index] 是一个Qt函数调用,用于获取当前系统上的屏幕列表中的特定索引处的屏幕。这在多显示器环境下很有用,因为你可以使用它来获取每个屏幕的信息和属性。

  • QGuiApplication::screens() 返回一个QList,包含了所有可用的屏幕。
  • 通过指定索引 index,你可以访问特定位置的屏幕,例如 QGuiApplication::screens()[0] 将返回第一个屏幕的信息。

这对于在多显示器设置中编写应用程序时,能够方便地获取每个屏幕的分辨率、DPI、位置等信息非常有用。

availableGeometry().center() 是一个Qt函数调用,通常用于获取屏幕工作区(不包括任务栏或类似的桌面空间)的中心点坐标。

  • availableGeometry() 返回一个矩形,表示当前屏幕工作区的几何信息,通常是排除了任务栏等占用空间的部分。
  • center() 是一个函数,用于获取矩形的中心点坐标。

因此,availableGeometry().center() 将返回屏幕工作区的中心点坐标,这在需要将窗口放置在屏幕中心时非常有用。

 

posted @ 2023-11-02 19:05  [BORUTO]  阅读(1723)  评论(0)    收藏  举报