Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt MainWindow with QOpenGLWIdget in Retina display displays wrong size

Tags:

c++

macos

qt

opengl

I have a Qt application with a MainWindow.

I embed a QOpenGLWidget in it. Everything works fine until I start using an Apple Retina Display and run my app in High DPI mode: my QOpenGLWidget is just 1/4 of the size it was supposed to have (i.e., it only fills the bottom-left part of the area it is supposed to fill). This widget is displaying raw OpenGL data (actually, an OpenSceneGraph context)

What can I do to solve this?

like image 384
manatttta Avatar asked Feb 06 '23 03:02

manatttta


1 Answers

Found out that the best option for now, for OpenGL related widgets and events, is to use the QPaintDevice::devicePixelRatio() (http://doc.qt.io/qt-5/qpaintdevice.html#devicePixelRatio)

This implies multiplying everything that uses pixel coordinates, i.e., mouse events, resizing events, and so on. Example:

void MyGLWidget::resizeGL(int width, int height) {
  width *= Application::desktop()->devicePixelRatio(); 
  height *= Application::desktop()->devicePixelRatio(); 

  ...
  // Continue with previous code

}

When running in low resolution mode in a Retina/HighDPI display, or when running in a regular display, this ratio is 1, so this seems portable to me.

like image 121
manatttta Avatar answered Feb 08 '23 16:02

manatttta