File tree Expand file tree Collapse file tree 1 file changed +31
-3
lines changed Expand file tree Collapse file tree 1 file changed +31
-3
lines changed Original file line number Diff line number Diff line change @@ -237,8 +237,36 @@ QTDesktopVideoDevice::colorProfile() const
237237
238238 // Get the context for this screen
239239 const QList<QScreen*> screens = QGuiApplication::screens ();
240- WId id = screens[m_screen]->effectiveWinId ();
241- HDC hdc = GetDC (reinterpret_cast <HWND>(id));
240+
241+ // Ensure the screen index is valid.
242+ if (m_screen < 0 || m_screen >= screens.size ())
243+ {
244+ m_colorProfile = ColorProfile ();
245+ return m_colorProfile;
246+ }
247+
248+ QScreen* targetScreen = screens[m_screen];
249+ QWindow* windowOnTargetScreen = nullptr ;
250+ const QList<QWindow*> windows = QGuiApplication::topLevelWindows ();
251+
252+ // Check all windows to find one on the target screen.
253+ for (QWindow* window : windows)
254+ {
255+ if (window->screen () == targetScreen)
256+ {
257+ windowOnTargetScreen = window;
258+ }
259+ }
260+
261+ if (!windowOnTargetScreen)
262+ {
263+ // Return empty profile if no window is found on the screen.
264+ m_colorProfile = ColorProfile ();
265+ return m_colorProfile;
266+ }
267+
268+ HWND hwnd = reinterpret_cast <HWND>(windowOnTargetScreen->winId ());
269+ HDC hdc = GetDC (hwnd);
242270
243271 if (hdc)
244272 {
@@ -268,7 +296,7 @@ QTDesktopVideoDevice::colorProfile() const
268296 }
269297
270298 delete path;
271- ReleaseDC ( reinterpret_cast <HWND>(id) , hdc);
299+ ReleaseDC (hwnd , hdc);
272300 }
273301 else
274302 {
You can’t perform that action at this time.
0 commit comments