c++ - Qt如何枚举屏幕?

标签 c++ windows qt qt5 multiple-monitors

今天我发现 Qt 枚举屏幕的顺序 ( QGuiApplication::screens ) 与 Windows 中的顺序 ( EnumDisplayMonitors ) 不同。

这种差异背后的逻辑是什么,以便在混合使用 Windows API 和 Qt 时将其考虑在内?例如,如果需要在屏幕 #2 中显示某些内容(使用 Windows 枚举)。

这里是我用来测试的代码(也可用 in GitHub ):

#include <qapplication.h>
#include <qdebug.h>
#include <qscreen.h>
#include <Windows.h>
#include <iostream>

std::ostream& operator<<(std::ostream& of, const RECT& rect)
{
  return of << "RECT(" << rect.left << ", " << rect.top << " " << (rect.right - rect.left) << "x" << (rect.bottom - rect.top) << ")";
}

BOOL CALLBACK printMonitorInfoByHandle(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
  auto index = (int*)dwData;
  std::cout << ++*index << " " << *lprcMonitor << std::endl;
  return TRUE;
}

int main(int argc, char* argv[])
{
  QApplication a(argc, argv);

  qDebug() << "*** Qt screens ***";
  const auto screens = qApp->screens();
  for (int ii = 0; ii < screens.count(); ++ii) {
    qDebug() << ii + 1 << screens[ii]->geometry();
  }

  qDebug() << "*** Windows monitors ***";
  int index = 0;
  EnumDisplayMonitors(NULL, NULL, printMonitorInfoByHandle, (LPARAM)&index);

  return 0;
}

我的显示器配置是,从左到右,2 (1280x1024),3 (1920x1080),1 (1920x1080),是我的主屏幕 3

结果:

*** Qt screens ***
1 QRect(0,0 1920x1080)
2 QRect(1920,233 1920x1080)
3 QRect(-1280,47 1280x1024)
*** Windows monitors ***
1 RECT(1920, 233 1920x1080)
2 RECT(-1280, 47 1280x1024)
3 RECT(0, 0 1920x1080)

最佳答案

据我在不同系统中所见,EnumDisplayMonitors 按照显示设置中定义的顺序返回监视器,而 QGuiApplication::screens 总是显示在第一个位置的主屏幕(实际上,QGuiApplication::primaryScreen 只是这样做:返回第一个元素)。

查看源代码,在 Windows Qt 中也使用 EnumDisplayMonitors 函数,但基本上将主屏幕移动到第一个位置(它实际上将主屏幕插入到第一个位置,而在列表末尾任何其他监视器)。

因此,主屏幕将位于第一个位置,索引低于主屏幕的屏幕将移动一个位置,而其余屏幕将与索引匹配。


作为旁注,根据代码的注释,如果主屏幕在应用程序执行期间发生更改,Qt 无法报告更改。

Note that the side effect of this policy is that there is no way to change primary screen reported by Qt, unless we want to delete all existing screens and add them again whenever primary screen changes.

关于c++ - Qt如何枚举屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54351270/

相关文章:

c++ - 无法计算输出

asp.net - Web 服务器上的计划任务 - 性能

python - PyQt QtCharts 设计器插件

Windows 相当于 $export

windows - 如何判断网络适配器在 Windows 上是否可移动

c++ - Qt与图像处理

c++ - 如何将 QActions 列表添加到 QMenu 并使用单个插槽处理它们?

c++ - 初始化 vector 的 vector 的类构造函数

android - :libvlc:buildDebugARMv7 FAILED

c# - 我如何创建一个可以从 c# 调用的 cpp 函数,并将函数作为参数