Notification when application changes to new screen
-
Hello,
I am implementing a multi screen application on desktop. Some screens may be restricted vertically, so I plan to adjust the UI as needed when the application is either manually moved by the user to a different screen, or I programmatically put that screen onto the smaller window.
I can bind to the Screen.height variable or Screen.width and get a notification when the application moves to a different sized monitor window.
ApplicationWindow { property int displaySize: Screen.height onDisplaySizeChanged: { console.log("Display Height:", displaySize) } }
Of course, this could have problems if screens of equivalent pixel resolution exist, or a very small but high DPI screen is used, or if one screen has a menu bar much larger then the other, etc.
The actual ideal end goal is to pass the underlying QScreen::availableGeometry() property into my QML through an existing c++ linkage (in order to take into account status bars and similar - not just physical pixel count).
Questions:
- What signal or variable is emmited when the application has moved screens? (either in qml or the c++ underneath)
- What is the underlying variable the qml Screen item is receiving signals on when it moves? (spent time digging into the QML code to try and find this, but was unsuccessful. Also attempted to get notificatons on availableGeometry() in QScreen, but that seems not to fire when the QML application moves)
- Any better suggestions for handling moving application to different screens, while taking into account menu bars and similar?
Thank you.
-
There it is! Thank you! I knew there had to be something simple here I was overlooking...
ApplicationWindow { onScreenChanged: { console.log("Screen Change!") } }
To find this, you have to dig into the inherited members of ApplicationWindow. In Qt 5.9> "screen" was introduced and you now get a signal on screen change. https://doc.qt.io/qt-5/qml-qtquick-window-window.html#screen-prop
In my case, the application does not allow resizing manually, so only need to react to this. Else you might want to also react to height/width display changes as well.
-
hi @antiocles and welcome
I don't think there's a ready to use signal that you can connect to.
But you should be able to do what you want to any way.
I would suggest using a Eventfilter on your top most parent window.
You'll have to filter forQEvent::NonClientAreaMouseButtonPress
andQEvent::NonClientAreaMouseButtonRelease
Those should only be triggered when the mouse was pressed on an area that can eb used to move the window around.But I don't know if this will also work for moving the window via keyboard shortcuts. For example WindowsKey+(any)arrowKey
You can read more on it in this stack overflow thread:
https://stackoverflow.com/a/55928964 -
Hmm... that is a method I had not thought about yet and an interesting possibility that probably catches most potential cases.
It seems there should be some sort of signal or event somewhere down the stack layers because the screen object is definitely reporting changes when you drag the application back and forth across screens.
-
I would like to try and dig into the underlying code around what is happening with the qml screen object to trace backwards to determine how it is detecting being moved and updating its geometry. I am quite familiar with the c++ side and organization of qt code having patched things before, but qml is new to me and I do not understand the source tree organization.
- Is there a guide or document explaining the source tree organization to help understand where to begin looking to find the underlying implementation of the qml side of the house?
-
I suspect that there is needed some c++ catching processWindowScreenChangedEvent from QGuiApplicationPrivate
-
You can react to a screen change in qml with
onScreenChanged: ...
-
There it is! Thank you! I knew there had to be something simple here I was overlooking...
ApplicationWindow { onScreenChanged: { console.log("Screen Change!") } }
To find this, you have to dig into the inherited members of ApplicationWindow. In Qt 5.9> "screen" was introduced and you now get a signal on screen change. https://doc.qt.io/qt-5/qml-qtquick-window-window.html#screen-prop
In my case, the application does not allow resizing manually, so only need to react to this. Else you might want to also react to height/width display changes as well.