Find the active QScreen of any QWidget
-
Hi, Is there an easy way to find a widget's active QScreen it resides on, obviously in a multi display environment. I am using Qt5.8 on windows 10.
I have extended the desktop with with an additional monitor which has lower resolution then my laptop.
This gives me some scaling issues with QPicture I am trying to work around.
I tried the function windowsHandle() which has a function screen but this function returns zero, propably because my widget is not a top level widget but instead part of an MDI child. -
Hi
Could you not you use
http://doc.qt.io/qt-5/qdesktopwidget.html#screenNumber
"Returns the index of the screen that contains the largest part of widget" -
I found this in the docu:
"virtualDesktop : const bool
This property holds if the system manages the available screens in a virtual desktop.
For virtual desktops, screen() will always return the same widget. The size of the virtual desktop is the size of this desktop widget."So... yep virtualDeskTop() returns true.
-
Hi
Ok, i think you need to loop over the screens and their geometry and
check that against the apps x,y to see on which screen it is placed.
if its x are bigger than screen 0 width, then its on screen 1.Notice, that apps. x might be negative. At least thats the case when placing
remote desktop windows and second monitor is on the left. . I didn't test
what Qt reports in this case.Technically its one big screen so hence screenNumber reports correctly.
Sorry, i missed the "extended" word in your description. :) -
This works ok. But not every widget has a windowHandle (maybe olny top level ones?) So then i grab the parent to check it.
QScreen* getActiveScreen(QWidget* pWidget) const { QScreen* pActive = nullptr; while (pWidget) { auto w = pWidget->windowHandle(); if (w != nullptr) { pActive = w->screen(); break; } else pWidget = pWidget->parentWidget(); } return pActive; }
-
Hi
ok so for a top level widget/window, ->screen()
actually knows which screen even if its a virtual screen/extended.
That is good to know :)
Its explained here whom have >windowHandle();
http://doc.qt.io/qt-5/qwidget.html#windowHandle