View Full Version : MFC Control in a Qt Tab Widget

29th June 2009, 22:49
I'm working on a project that is using the Qt/MFC Migration Framework and I'm trying to reuse some existing MFC controls inside of a Qt dialog.

Does anyone know if it is possible to insert an MFC control (CDialog or CWnd) inside of a QTabWidget. Right now we're doing the opposite, we have an MFC dialog with a tab control which is populated with a mix of MFC tabs (CDialog) and Qt tabs (QWinWidget). However, this approach is giving me a headache because the QWinWidget controls are not properly being drawn nor are they receiving focus or keyboard input correctly. I am hoping that using a Qt dialog with a QTabWidget will work better than this approach.

29th June 2009, 23:13
Does anyone know if it is possible to insert an MFC control (CDialog or CWnd) inside of a QTabWidget.

I think you should use QWinHost.

30th June 2009, 21:41
Can anyone provide me with an example of how to use this QWinHost to wrap an MFC CWnd or CDialog class so that the control can be used in a QTabWidget? I've been banging my head for a while with this, and I can't seem to get anything to work.

I'm trying to follow the example Qt has (http://doc.trolltech.com/solutions/4/qtwinmigrate/winmigrate-win32-in-qt-example.html)that shows how a Win32 control has been wrapped in the QWinHost. They derive a class from QWinHost and override the createWindow function to specify how to create the Win32 control. This is one of my problems because the createWindow class passes in a HWND for the parent, but all MFC classes require a CWnd object as a parent. I've tried every permutation of CWnd::FromHandle, CWnd::FromPermanentHandle, CWnd::Attach, etc, and I can't seem to get anything to work. It seems to me that the QWinHost wasn't designed for wrapping MFC controls.

30th June 2009, 21:48
It was designed specifically for that. Have you tried simply calling setWindow() on the QWinHost instance?

30th June 2009, 22:04
Is this the correct usage of the QWinHost in the context of MFC?

QDialog* lpDialog = new QDialog();

CDialog* lpMfcDlg= new CDialog();

QWinHost* lpChild = new QWinHost(lpDialog);


Interestingly, when I do this, nothing crashes, but the tab housing the MFC control does not display its contents, and in fact does not draw anything (the previous visited tab's contents are still being drawn indicating that this tab is literally drawing nothing - not even covering up what was drawn before).

Another thing I've noticed is that the CMyMfcDlg::OnInitDialog method is not being called, but it is called if I were to do this instead: CMyMfcDlg:: DoModal( ).

EDIT: lpMfcDlg->GetSafeHwnd() was returning NULL, but I corrected that and it is still not working. The tab is still unresponsive/not being drawn.

30th June 2009, 22:40
It all might be because you are trying to manipulate the shown/hidden state of the mfc control (when placing it in a host). I never used QWinHost so I'm merely guessing here, but what happens if you use QWinHost for a non-MFC widget or when you don't place the mfc control in a tab widget?