经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 软件/图像 » OpenGL » 查看文章
QT OpenGLWidget高分屏适配时出现的问题
来源:cnblogs  作者:zhxmdefj  时间:2020/12/8 8:56:01  对本文有异议

参考官方文档,为了解决4K屏幕显示QT界面时出现窗口变小分辨率不匹配的问题,我们可以在 QApplication a(argc, argv); 这句之前添加:

  1. #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
  2. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  3. #endif
Constant Value Description
Qt::AA_EnableHighDpiScaling 20 Enables high-DPI scaling in Qt on supported platforms (see also High DPI Displays). Supported platforms are X11, Windows and Android. Enabling makes Qt scale the main (device independent) coordinate system according to display scale factors provided by the operating system. This corresponds to setting the QT_AUTO_SCREEN_SCALE_FACTOR environment variable to 1. This attribute must be set before QGuiApplication is constructed. This value was added in Qt 5.6.

运行结果显示其他界面均运行正常,但是在 QOpenGLWidget 内,渲染的图像只显示在左下角,占整个渲染区域的 1/4 左右。

其原因是启用 AA_EnableHighDpiScaling 后,QOpenGLWidget 内部的 OpenGL 不归属于 QT 的缩放范围,所以我们需要自己缩放 QOpenGLWidget 内的像素尺寸。

解决方案

解决的方法就是自行获取 devicePixelRatio,手动进行缩放。实现的方式大致有两种,具体取决于你 QOpenGLWidget 裁剪的方式。ratio 可以通过 QApplication::desktop()->devicePixelRatio(); 获取。

如果是通过 resizeGL(int width, int height) 裁剪,缩放就应写在其中。
以下代码参考了博文 https://blog.csdn.net/genispan/article/details/107864829

  1. void QOpenGLWidget::resizeGL(int width, int height)
  2. {
  3. makeCurrent();
  4. //参数X,Y指定了视见区域的左下角在窗口中的位置,一般情况下为(0,0),Width和Height指定了视见区域的宽度和高度。
  5. int ratio = QApplication::desktop()->devicePixelRatio();
  6. glViewport(0, 0, (GLint)width*ratio, (GLint)height*ratio);
  7. /*
  8. glMatrixMode设置当前矩阵模式:
  9. GL_MODEVIEW,对模型视景矩阵堆栈应用随后的矩阵操作。
  10. GL_PROJECTION,对投影矩阵应用随后的矩阵操作。
  11. GLTEXTURE,对纹理矩阵堆栈应用随后的矩阵操作。
  12. */
  13. glMatrixMode(GL_PROJECTION);
  14. glLoadIdentity();
  15. //glOrtho 参数分别代表(左下角x坐标,右上角x坐标,左下角y坐标,右上角y坐标)——坐标全相对于窗口左下角--原点)
  16. glOrtho(0, width*ratio, -height*ratio, 0, 0, 1); ///防止图像上下翻转
  17. glMatrixMode(GL_MODELVIEW);
  18. recalculatePosition();
  19. emit imageSizeChanged(mRenderWidth, mRenderHeight);
  20. updateScene();
  21. }

裁剪实现在对输入纹理的处理的话,相应的代码应该写到 paintGL() 内:

  1. void QOpenGLWidget::initializeGL(){
  2. ...
  3. QScreen *pScreen = QApplication::desktop()->screen(screenNum)->screen();
  4. static const qreal NORMAL_DPI = 95.863739404014453;
  5. auto xFactor = pScreen->logicalDotsPerInchX() / NORMAL_DPI;
  6. auto yFactor = pScreen->logicalDotsPerInchY() / NORMAL_DPI;
  7. auto ratio = pScreen->devicePixelRatio();
  8. _xRatio = xFactor * ratio;
  9. _yRatio = yFactor * ratio;
  10. }
  11. void QOpenGLWidget::paintGL() {
  12. ...
  13. widgetWidth *= _xRatio;
  14. widgetHeight *= _yRatio;
  15. //QImage对象img即为输入纹理
  16. auto img = currImg;
  17. if (!currImg.isNull()) {
  18. img = img.scaled(widgetWidth, widgetHeight, Qt::KeepAspectRatio);
  19. img = img.copy((img.width() - widgetWidth) / 2, (img.height() - widgetHeight) / 2, widgetWidth, widgetHeight);
  20. }
  21. ...
  22. }

裁剪过后QOpenGLWidget应该就能正常显示了。

原文链接:http://www.cnblogs.com/zhxmdefj/p/14062243.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号