user2123079
user2123079

Reputation: 686

QGraphicsView scaling with High DPI

I have an application with QMainWindow in which I insert my own widget, inherited from QGraphicsView. As viewport I use QGLWidget. Everything works fine but with Hidh DPI there is a problem: my widget(inherited from QGraphicsView) is very small.

Before creation of QApplication I enable High DPI support by

QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

and in my widget I do following (from signal that comes from QMainWindow deep in the code):

void MyWidget::onNeedResize(QRect newGeom)
{
      // some logic, that not interact with GUI stuff
      setGeometry(newGeom);
      setSceneRect(QRect(QPoint(0, 0), newGeom.size()));
      // more logic, that not interact with GUI stuff
}

What did I missed? Where is a problem?

UPD1: I replaced QGLWidget by QOpenGLWidget and everything started work just as expected! Without any modifications/calculations/additional stuff. Setting of the flag is enough. But the problem is I can't use QOpenGLWidget instead of QGLWidget for now.

Upvotes: 2

Views: 1977

Answers (1)

vicrion
vicrion

Reputation: 1733

My assumption on why the dpi scaling does not work is because you use OpenGL widget as your viewport. From Qt docs:

Applications mostly work with device independent pixels. Notable exceptions are OpenGL and code that works with raster graphics.

From that document, it means that OpenGL-content widgets will not be scaled even if you use Qt::AA_EnableHighDpiScaling.

Try to use devicePixelRatio() directly in your resizing code. An example on how it can be used within your code:

void MyWidget::onNeedResize(QRect newGeom)
{
      // some logic, that not interact with GUI stuff
      setGeometry(QRect(newGeom.x() * Application::desktop()->devicePixelRatio(), 
                        newGeom.y() * Application::desktop()->devicePixelRatio(), 
                        newGeom.width() * Application::desktop()->devicePixelRatio(), 
                        newGeom.height() * Application::desktop()->devicePixelRatio() ));
      setSceneRect(QRect(QPoint(0, 0), QSize(
                                       newGeom.width() * Application::desktop()->devicePixelRatio(), 
                                       newGeom.height() * Application::desktop()->devicePixelRatio() ) ));
      // more logic, that not interact with GUI stuff
}

That is, for every sizing/position you use within your widget, use a scaling factor of Application::desktop()->devicePixelRatio(). That should fix your problem.

Upvotes: 2

Related Questions