Ask Your Question

Embedded OpenCV Window with Qt Support in MFC

asked 2015-10-09 06:18:22 -0600

Hello, want to embedded an opencv window with qt support in my mfc application. If i use the non supported .dll in my app everything works fine but need to zoom and shift the image. Because of this, i want to embedded the window with qt support, but when I use the .dll compiled with qt sup. a new window pop up and the picture box in my MFC Dialog ist empty. This is my code to display the Image. In case of the qt sup. the value of hWnd1 is negativ.

cv::namedWindow("Histogram", 1);
HWND hWnd1 = (HWND) cvGetWindowHandle("Histogram");
HWND hParent1 = ::GetParent(hWnd1);
::SetParent(hWnd1, GetDlgItem(IDC_HISTOGRAM)->m_hWnd);
::ShowWindow(hParent1, SW_HIDE);
cv::imshow("Histogram", hist_init);


edit retag flag offensive close merge delete


What is the control type of the IDC_HISTOGRAM item?

tuannhtn gravatar imagetuannhtn ( 2015-10-09 08:07:19 -0600 )edit

picture control

randolf gravatar imagerandolf ( 2015-10-09 09:05:33 -0600 )edit

I just had a look into the cvGetWindowHandle funktion in window_qt.cpp and the return value is pointer of a QWindow. But how can I embedded a QWindow into a MFC Dialog.

randolf gravatar imagerandolf ( 2015-10-09 09:28:54 -0600 )edit

Then it is strange to me. A full code will be easier to pinpoint the bug, if any.

tuannhtn gravatar imagetuannhtn ( 2015-10-09 09:31:06 -0600 )edit

That's the full code to display the Image in a Picture box. Without QT Support it works fine, but with Qt Support a new image window popped up.

const char * WIN_NAME_CV = "Image Display";
RECT rcDlg;
HWND myhWnd = GetDlgItem(IDC_IMG_DISPLAY)->GetSafeHwnd();
::GetWindowRect(myhWnd, &rcDlg);
int winWidth = rcDlg.right - rcDlg.left;
int winHeight = rcDlg.bottom -;
HWND hWnd = (HWND)cvGetWindowHandle(WIN_NAME_CV);
HWND hParent = ::GetParent(hWnd);
::SetParent(hWnd, myhWnd);
::ShowWindow(hParent, SW_HIDE); 
cvResizeWindow(WIN_NAME_CV, winWidth, winHeight);
randolf gravatar imagerandolf ( 2015-10-12 02:43:41 -0600 )edit

Please consider that, when Qt support enabled, cvGetWindowHandle returns an handle to Qt window while your control is embedded in a MFC window. So you are mapping the content of your cv::window over a MFC control while the cv::window is Qt and will open with blank content... I'm surprised about it's work without some exception

pklab gravatar imagepklab ( 2015-10-12 06:21:10 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-10-10 03:04:16 -0600

pklab gravatar image

updated 2015-10-10 03:07:13 -0600

Qt support isn't needed if you use MFC. To display a cv::Mat into MFC control you have 2 way:

  1. Use GDI to transfer the image from OpenCV memory to DC of a CStatic Control like a Picture or Static Text. See a PkMatToGDI class on my web site and/or my old answer how-to-fix-resized-image-in-mfc for the code.
  2. Map a cvWindow over a MFC static control, as is your code, but with some correction.

try this for 2nd way:

const char * WIN_NAME_CV = "Histogram";
RECT rcDlg;
HWND myhWnd = GetDlgItem(IDC_HISTOGRAM)->GetSafeHwnd();

// Or if you have the object for your static control
//HWND myhWnd = m_HistogramControl.GetSafeHwnd();

::GetWindowRect(myhWnd, &rcDlg);

int winWidth = rcDlg.right - rcDlg.left;
int winHeight = rcDlg.bottom -;
HWND hWnd = (HWND)cvGetWindowHandle(WIN_NAME_CV);
HWND hParent = ::GetParent(hWnd);
::SetParent(hWnd, myhWnd);
::ShowWindow(hParent, SW_HIDE);
cvResizeWindow(WIN_NAME_CV, winWidth, winHeight);
edit flag offensive delete link more


Hi, I know that i don't need Qt support to use opencv in MFC, but i want to zoom and scale the Image and the QT window support that. I need this only for prototyping so I don't want to implement zooming and scaling by my self.

randolf gravatar imagerandolf ( 2015-10-12 02:38:36 -0600 )edit

Qt is (not just) a replacement for MFC. I'm not shure but you should use MFC or Qt. Anyway with MFC ... just resize your IDC_HISTOGRAM control than use cvResizeWindow(WIN_NAME_CV, winWidth, winHeight); to fit over new size.

pklab gravatar imagepklab ( 2015-10-12 04:08:02 -0600 )edit

Question Tools

1 follower


Asked: 2015-10-09 06:18:22 -0600

Seen: 1,307 times

Last updated: Oct 10 '15