Skip to content

Commit a48a792

Browse files
Fix transition from QApplication::desktop() to QScreen for Qt5 and Qt6
Signed-off-by: Cédrik Fuoco <[email protected]>
1 parent 1f1b673 commit a48a792

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

src/lib/app/RvCommon/QTDesktopVideoDevice.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff 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
{

0 commit comments

Comments
 (0)