QT OpenGLWidget高分屏适配时出现的问题

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

#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
	QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#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

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

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

void QOpenGLWidget::initializeGL(){
    ...
    QScreen *pScreen = QApplication::desktop()->screen(screenNum)->screen();
	static const qreal NORMAL_DPI = 95.863739404014453;
	auto xFactor = pScreen->logicalDotsPerInchX() / NORMAL_DPI;
	auto yFactor = pScreen->logicalDotsPerInchY() / NORMAL_DPI;
	auto ratio = pScreen->devicePixelRatio();
	_xRatio = xFactor * ratio;
	_yRatio = yFactor * ratio;
}
void QOpenGLWidget::paintGL() {
    ...
    widgetWidth *= _xRatio;
	widgetHeight *= _yRatio;
    //QImage对象img即为输入纹理
    auto img = currImg;
	if (!currImg.isNull()) {
		img = img.scaled(widgetWidth, widgetHeight, Qt::KeepAspectRatio);
		img = img.copy((img.width() - widgetWidth) / 2, (img.height() - widgetHeight) / 2, widgetWidth, widgetHeight);
    }
    ...
}

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

posted @ 2020-11-30 16:55  KelvinVS  阅读(1377)  评论(0编辑  收藏  举报