Congratulations to our 2022 Qt Champions!

Detect changing devicePixelRatio

  • Howdy! We have an app where the main window contains a QGLWidget among other widgets. When the main window is dragged between monitors, one "normal" and one retina, at a certain point the UI "snaps" to either use the retina resolution or normal resolution. Is there a way to reliably detect when the display resolution changes (from the main window's perspective)? After failing to find a signal or other virtual function that does so we tried overriding the bool event(QEvent*) method. In that method we keep track of the devicePixelRatio() value. When it changes we emit a signal. This turns out to work only some of the time. We've found that even if the window never travels to the retina monitor the value reported by devicePixelRatio() is sometimes 2, and sometimes 1. Ideally Qt already has a better way to detect this scenario.

    In case it comes up I'll say in advance we can't simply change over to use QOpenGLWidget. We're stuck using QGLWidget for the time being under Qt 5.3.1 (Mac/Windows). Thanks in advance for any suggestions.

  • I guess you're on X11?

  • @Wieland Sorry, should have mentioned in original post. This is for Mac/Windows

  • You can connect to the screenChanged(QScreen)* signal of the QWindow to which your widget belongs.

    After the window is set up:
    connect(window()->windowHandle(), SIGNAL(screenChanged(QScreen*)), this, SLOT(screenChanged()));

    Where screenChanged() is a slot you define to check devicePixelRatio().

    The window changes screens when the majority of it is in the new screen.

  • @Dyami-Caliri This is not my thread, but: Thanks Dyami Caliri! Didn't knew screenChanged() exists :-)

  • @Dyami-Caliri, thanks! It works fine when I drag my window between two monitors. However, when I relocate the menu bar to switch to the other monitor(System Preferences -> Displays -> Arrangement on Mac), it seems that screenChanged(QScreen*) signal didn't emit. Is this situation an exception? Or should I listen to another signal?
    Thanks a lot.

Log in to reply