简体   繁体   English

QGraphicsView 高 DPI 缩放

[英]QGraphicsView scaling with High DPI

I have an application with QMainWindow in which I insert my own widget, inherited from QGraphicsView.我有一个带有 QMainWindow 的应用程序,我在其中插入我自己的小部件,从 QGraphicsView 继承。 As viewport I use QGLWidget.作为视口,我使用 QGLWidget。 Everything works fine but with Hidh DPI there is a problem: my widget(inherited from QGraphicsView) is very small.一切正常,但使用 Hidh DPI 有一个问题:我的小部件(从 QGraphicsView 继承)非常小。

Before creation of QApplication I enable High DPI support by在创建 QApplication 之前,我通过以下方式启用高 DPI 支持

QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

and in my widget I do following (from signal that comes from QMainWindow deep in the code):在我的小部件中,我执行以下操作(来自代码深处的 QMainWindow 信号):

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! UPD1 :我QOpenGLWidget替换了 QGLWidget ,一切都按预期开始工作! 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.但问题是我现在不能使用 QOpenGLWidget 而不是 QGLWidget。

My assumption on why the dpi scaling does not work is because you use OpenGL widget as your viewport.我对为什么 dpi 缩放不起作用的假设是因为您使用 OpenGL 小部件作为您的视口。 From Qt docs :来自Qt 文档

Applications mostly work with device independent pixels.应用程序主要使用与设备无关的像素。 Notable exceptions are OpenGL and code that works with raster graphics.值得注意的例外是 OpenGL 和使用光栅图形的代码。

From that document, it means that OpenGL-content widgets will not be scaled even if you use Qt::AA_EnableHighDpiScaling .从该文档中,这意味着即使您使用Qt::AA_EnableHighDpiScaling也不会缩放 OpenGL 内容小部件。

Try to use devicePixelRatio() directly in your resizing code.尝试在调整大小代码中直接使用devicePixelRatio() 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() .也就是说,对于您在小部件中使用的每个大小/位置,使用Application::desktop()->devicePixelRatio()缩放因子 That should fix your problem.那应该可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM