I have a minimal application that uses QOpenGLWidget , which integrates the OpenGL wrapper library (OpenSceneGraph). I'm trying to figure out how to properly use Qt5.6 support for high DPI screens when working with OpenGL content as I use.
The function My main() has the following code:
int main(int argc, char** argv) { // DPI support is on QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication app(argc, argv); QMainWindow window; // QOpenGLWidget with OpenSceneGraph content QtOSGWidget* widget = new QtOSGWidget(); window.setCentralWidget(widget); window.show(); return app.exec(); }
QtOSGWidget derived from a QOpenGLWidget with the contents of OpenSceneGraph: I use osgViewer::GraphicsWindowEmbedded to render my simple scene.
To combine OSG with Qt, I override the *GL() methods: paintGL() , resizeGL() and initializeGL() . I follow the Qt docs about what each of the *GL() methods should contain, i.e.:
paintGL() checks to see if the viewer is being updated.resizeGL() ensures the correct resizing of the graphics window (along with the camera and viewport);initializeGL() provides initialization of OpenGL state.- I also redefined Qt mouse events to pass events to OSG
When I run my example on the screen with normal resolution or using QApplication::setAttribute(Qt::AA_DisableHighDpiScaling); , the scene looks like this:

In addition, when I manipulate the camera view, the coordinates of the mouse are fixed correctly.
However, when I set the high DPI option, this is what I get:

Mouse coordinates for events are also scaled and not passed to the OpenSceneGraph event handler.
As you can see, the size of the graphics window is not scaled by Qt. This is probably due to the way I set the size:
virtual void resizeGL( int width, int height ) { // resize event is passed to OSG this->getEventQueue()->windowResize(this->x(), this->y(), width, height); // graphics window resize m_graphicsWindow->resized(this->x(), this->y(), width, height); // camera viewport osg::Camera* camera = m_viewer->getCamera(); camera->setViewport(0, 0, this->width(), this->height()); }
This size does not scale Qt. The same thing happens with the coordinates of the mouse events.
My question is: is there a way to find out what size the scaling will be done to properly execute resizeGL() ? Or how to solve the problem?
Update / solution using manual scaling : thanks to @AlexanderVX's answer, I figured out a scaling solution. First I need to know some DPI reference values ββin sizes X and Y. Then I calculate the sliding coordinates based on this and pass them to my QtOSGWidget widget. So, the main() code should contain:
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication app(argc, argv); int x = QApplication::desktop()->physicalDpiX(); int y = QApplication::desktop()->physicalDpiY();
Then, whenever I access the calibration functions that need to be passed to OpenSceneGraph (OpenGL), I need to do the scaling, for example:
// resizeGL example this->getEventQueue()->windowResize(this->x()*m_scaleX, this->y() * m_scaleY, width*m_scaleX, height*m_scaleY); // mouse event example this->getEventQueue()->mouseButtonPress(event->x()*m_scaleX, event->y()*m_scaleY, button);
Final update : since the target platform of my application is Windows 7-10, it makes sense to stick to the proposed answer @AlexanderV (second part), i.e. use SetProcessDPIAware() .