Detect changing devicePixelRatio
-
wrote on 16 Mar 2015, 18:42 last edited by mooshx
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.
-
wrote on 16 Mar 2015, 19:27 last edited by
I guess you're on X11?
-
I guess you're on X11?
wrote on 16 Mar 2015, 20:32 last edited by@Wieland Sorry, should have mentioned in original post. This is for Mac/Windows
-
wrote on 16 Mar 2015, 21:28 last edited by
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.
-
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.
wrote on 16 Mar 2015, 21:34 last edited by@Dyami-Caliri This is not my thread, but: Thanks Dyami Caliri! Didn't knew screenChanged() exists :-)
-
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.
wrote on 11 Sept 2015, 05:39 last edited by@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. -
@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.wrote on 3 Aug 2023, 08:58 last edited by intelegenter1 8 Mar 2023, 08:58@fxxxx you should set attribute for your main window: setAttribute(Qt::WA_NativeWindow); but it causes optimization problems, because it is legacy feature. anyway, it will solve your problem, and windowHandle() should return valid pointer, and will emit signal screenChanged().