QGraphicsView使用高DPI进行缩放

时间:2017-01-19 11:29:13

标签: c++ qt dpi

我有一个QMainWindow的应用程序,我在其中插入了自己的小部件,继承自QGraphicsView。作为视口我使用QGLWidget。一切正常,但Hidh DPI存在问题:我的小部件(继承自QGraphicsView)非常小。

在创建QApplication之前,我通过

启用高DPI支持
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

并在我的小部件中执行以下操作(来自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
}

我错过了什么?哪里有问题?

UPD1 :我用QOpenGLWidget取代了QGLWidget ,一切都按预期开始了!没有任何修改/计算/额外的东西。设置标志就足够了。但问题是我暂时不能使用QOpenGLWidget而不是QGLWidget。

1 个答案:

答案 0 :(得分:1)

我对dpi缩放不起作用的假设是因为您使用OpenGL小部件作为视口。来自Qt docs

  

应用程序主要使用与设备无关的像素。值得注意的例外是OpenGL和适用于光栅图形的代码。

从该文档来看,这意味着即使您使用Qt::AA_EnableHighDpiScaling,也不会缩放OpenGL内容小部件。

尝试在调整大小代码中直接使用devicePixelRatio()。关于如何在代码中使用它的示例:

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
}

也就是说,对于您在窗口小部件中使用的每个大小/位置,请使用Application::desktop()->devicePixelRatio()缩放系数。这应该可以解决你的问题。

相关问题