环境:Win10-x64+VS2015+Qt5.9.7+Halcon12
首先创建一个Qt GUI Application。工程名:myDockWidget。将Halcon12包含目录与库目录文件放到工程目录下:
选择工程属性页,配置halcon环境:
#pragma once
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "ui_myDockWidget.h"
#include "Halcon.h"
#include "cpp\HalconCpp.h"
#include "halconcpp\HalconCpp.h"
using namespace HalconCpp;
#pragma execution_character_set("utf-8")
class myDockWidget : public QMainWindow
{
Q_OBJECT
public:
myDockWidget(QWidget *parent = Q_NULLPTR);
private:
Ui::myDockWidgetClass ui;
QPushButton *runPushButton;
QLabel *m_qLabel;
HObject mainImage;
HTuple m_hWindowID;
private slots:
void runPushButtonSlot();
void progressPushButtonSlot();
};
#include "myDockWidget.h"
myDockWidget::myDockWidget(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//显示——停靠窗口
m_qLabel = new QLabel("", this);
m_qLabel->setAlignment(Qt::AlignCenter);
m_qLabel->setStyleSheet("background-color: rgb(50, 50, 50);");
m_qLabel->setMinimumWidth(500);
m_qLabel->setMinimumHeight(300);
QHBoxLayout *hlayout0 = new QHBoxLayout;
hlayout0->addWidget(m_qLabel);
QGridLayout *vlayout0 = new QGridLayout;
vlayout0->addLayout(hlayout0, 0, 0);
QWidget *cornerWidget0 = new QWidget;
cornerWidget0->setLayout(vlayout0);
setCentralWidget(cornerWidget0);
//调试——停靠窗口
QDockWidget *debugging = new QDockWidget("调试", this);//构建停靠窗口,指定父类
debugging->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetClosable);//设置停靠窗口特性,可移动,可关闭
debugging->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);//设置可停靠区域为主窗口左边和右边
debugging->setMinimumWidth(200);
QTextEdit *debuggingEdit = new QTextEdit("调试——停靠窗口");
debugging->setWidget(debuggingEdit);
addDockWidget(Qt::RightDockWidgetArea, debugging);
//测试——停靠窗口
QDockWidget *dw3 = new QDockWidget("测试", this);//构建停靠窗口,指定父类
dw3->setFeatures(QDockWidget::DockWidgetMovable);//设置停靠窗口特性,可移动,可关闭
dw3->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);//设置可停靠区域为主窗口左边和右边
QHBoxLayout *hlayout31 = new QHBoxLayout;
runPushButton = new QPushButton(tr("打开图片"));
runPushButton->setStyleSheet("background-color: rgb(0, 255, 127);");
QObject::connect(runPushButton, SIGNAL(clicked()), this, SLOT(runPushButtonSlot()));
QPushButton *progressPushButton = new QPushButton(tr("处理图片"));
QObject::connect(progressPushButton, SIGNAL(clicked()), this, SLOT(progressPushButtonSlot()));
hlayout31->addWidget(runPushButton);
hlayout31->addWidget(progressPushButton);
QGridLayout *vlayout3 = new QGridLayout;
vlayout3->addLayout(hlayout31, 0, 0);
QWidget *cornerWidget3 = new QWidget;
cornerWidget3->setLayout(vlayout3);
dw3->setWidget(cornerWidget3);
dw3->setMaximumHeight(100);
dw3->setMaximumWidth(300);
addDockWidget(Qt::LeftDockWidgetArea, dw3);
}
void myDockWidget::runPushButtonSlot()
{
runPushButton->setStyleSheet("background-color: rgb(255, 100, 100);");
QString filename = QFileDialog::getOpenFileName(this, tr("选择图像"), "", tr("Images (*.png *.bmp *.jpg)"));
if (filename.isEmpty())
return;
else
{
ReadImage(&mainImage, filename.toStdString().c_str());
HTuple mainWndID;
mainWndID = (Hlong)m_qLabel->winId();
QString s_hWindowID = QString::fromStdString(m_hWindowID.ToString().Text());
if (s_hWindowID == "[]")
{
OpenWindow(0, 0, m_qLabel->width(), m_qLabel->height(), mainWndID, "visible", "", &m_hWindowID);
}
HDevWindowStack::Push(m_hWindowID);
HTuple srcImageHeight, srcImageWidth;
GetImageSize(mainImage, &srcImageWidth, &srcImageHeight);
SetPart(m_hWindowID, 0, 0, srcImageHeight.I(), srcImageWidth.I());
DispObj(mainImage, m_hWindowID);
}
QMessageBox::information(this, "提示:", "加载成功!");
}
void myDockWidget::progressPushButtonSlot()
{
if (mainImage.Key())
{
Rgb1ToGray(mainImage, &mainImage);
DispObj(mainImage, m_hWindowID);
QMessageBox::information(this, "提示:", "处理结束!");
}
}
最终效果: