下面的代码演示将opencv的窗口嵌入到label里面显示。即labDisplayImg里面显示。


#include <windows.h>
oid QtForOpencv::OnReadimg(){
QString filename = QFileDialog::getOpenFileName(this,
tr("选择图像"),
"",
tr("Images (*.png *.bmp *.jpg *.tif *.GIF )"));
if (filename.isEmpty())
{
return;
}else
{
Mat src=imread(filename.toStdString());
QString title = "photo1";
namedWindow(title.toStdString(), 1); // CV_WINDOW_NORMAL); // 0;
//resizeWindow(title, 640, 480); //设置窗口大小
//moveWindow(title, 100, 100); //移动窗口到指定位置
HWND hwnd = (HWND)cvGetWindowHandle(title.toLatin1().data()); //toLatinl()转char*
//取title窗体的父窗体句柄
HWND hparent =::GetParent(hwnd);
//改变某个子窗体的父窗口(子窗口句柄,新的父窗口句柄)
::SetParent(hwnd, (HWND)ui.labDisplayImg->winId()); //winId()取win32api要用的窗口句柄
//显示窗体SW_SHOW 隐藏窗体SW_HIDE
::ShowWindow(hparent, SW_HIDE);
imshow(title.toStdString(), src);
/*设置label图像
QImage* img = new QImage;
if (!(img->load(filename))){
//加载图像
QMessageBox::information(this,
tr("打开图像失败"),
tr("打开图像失败!"));
delete img;
return;
}
ui.labDisplayImg->setPixmap(QPixmap::fromImage(*img));*/
}
}这里要提一下,如果在QT里面想使用win32 api的话,需要引入<windows.h>
---------------------
作者:hackpig
来源:www.skcircle.com
版权声明:本文为博主原创文章,转载请附上博文链接!