From 32f640ec69af4503998f75b3b796bfce78389efe Mon Sep 17 00:00:00 2001 From: Wladimir Leuschner Date: Wed, 8 Nov 2023 12:35:11 +0100 Subject: [PATCH] Use Desktop Handle in case of no valid app HWND for QPrintDialog The call Win32 API PrintDlgEx needs in the PRINTDLGEX struct a valid window handle for hwndOwner to show up. In case there is no window created, as seen in the example, the call to PrintDlgEx fails with COM error code E_HANDLE. Using the Desktop HWND, in case of no valid app HWND creates a valid call to PrintDlgEx with showing up the dialog. Fixes: QTBUG-118899 Change-Id: Ie7009c8e6e8285a0b6312e310b3d065c532f9e17 Reviewed-by: Friedemann Kleint (cherry picked from commit 7e8ae9cf12611439462e8b26946473fd6492afcc) Reviewed-by: Qt Cherry-pick Bot --- src/printsupport/dialogs/qprintdialog_win.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/printsupport/dialogs/qprintdialog_win.cpp b/src/printsupport/dialogs/qprintdialog_win.cpp index 6ac3bea4c09..6d8c2d0f4d7 100644 --- a/src/printsupport/dialogs/qprintdialog_win.cpp +++ b/src/printsupport/dialogs/qprintdialog_win.cpp @@ -101,7 +101,12 @@ static void qt_win_setup_PRINTDLGEX(PRINTDLGEX *pd, QWindow *parentWindow, if (d->ep->printToFile) pd->Flags |= PD_PRINTTOFILE; - pd->hwndOwner = parentWindow ? (HWND)QGuiApplication::platformNativeInterface()->nativeResourceForWindow("handle", parentWindow) : 0; + + WId wId = parentWindow ? parentWindow->winId() : 0; + //QTBUG-118899 PrintDlg needs valid window handle in hwndOwner + //So in case there is no valid handle in the application, + //use the desktop as valid handle. + pd->hwndOwner = wId != 0 ? HWND(wId) : GetDesktopWindow(); pd->lpPageRanges[0].nFromPage = qMax(pdlg->fromPage(), pdlg->minPage()); pd->lpPageRanges[0].nToPage = (pdlg->toPage() > 0) ? qMin(pdlg->toPage(), pdlg->maxPage()) : 1; pd->nCopies = d->printer->copyCount();