Getting a CWnd to use as a parent
-
I have an MFC dialog that I want to create as a child window (WS_CHILD). To do that I need a CWnd to be the parent.
My immediate thought was this:
winHost = new QWinHost(stackedWidget); winHost->setObjectName("winHost"); stackedWidget->addWidget(winHost); // // Attach the hwnd of the stacked widget to a CWnd that we use as the parent for the // processing dialog // HWND hwnd{ reinterpret_cast<HWND>(stackedWidget->effectiveWinId()) }; hostWnd.Attach(hwnd); auto result = processingDlg->Create(IDD_PROCESSING, &hostWnd); if (FALSE == result) { int lastErr = GetLastError(); ZTRACE_RUNTIME("lastErr = %d", lastErr); } processingDlg->setParent(winHost); // Provide a Qt object to be parent for any Qt Widgets this creates hwnd = processingDlg->GetSafeHwnd(); ZASSERT(NULL != hwnd); winHost->setWindow(hwnd);
where hostWnd is a CWnd that is a member variable of the class. However, the call to stackedWidget->effectiveWinId() returned a NULL HWND, so it all fell apart.
So how can I get this to work please?
I was working round the problem by defining the dialogue IDD_PROCESSING as WS_POPUP, but that has side effects (amongst others it gets created with a sizing border).
Thanks,
David -
I have resolved this problem.
I moved all bar the first two lines of the code in question from the ctor of the QMainWindow to the showEvent() handler. The HWND for the stacked widget was valid at that point whereas it wasn't in the ctor.
I also added a line to the dtor which read:
hostWnd.Detach();
I hope someone finds this helpful.
David -