文章目录
1. 窗口的绑定
Step1:导入头文件
#ifndef __APPLE__
# include "HalconCpp.h"
#else
# ifndef HC_LARGE_IMAGES
# include <HALCONCpp/HalconCpp.h>
# else
# include <HALCONCppxl/HalconCpp.h>
# endif
#endif
Step2:声明一个全局的控制变量(HTuple类型)
HTuple WindowHandle;//control variables
Step3:在窗口构造函数中绑定Halcon窗口于控件
//窗口绑定设置
Hlong windID = (Hlong)this->ui->gV_image->winId();//绑定控件与halcon窗口
//OpenWindow(0,0,ui->gV_image->width(),ui->gV_image->height(),0,"","",&WindowHandle);
OpenWindow(0,0,ui->gV_image->width(),ui->gV_image->height(),windID,"visible","",&WindowHandle);//创建一个窗体ui
SetPart(WindowHandle,0,0,2748,3664);//1944 2592
SetDraw(WindowHandle,"margin");
SetLineWidth(WindowHandle,1);
SetColor(WindowHandle,"red");
注意:如果没绑定,则Halcon窗口是弹出的,而不是嵌入在控件中的
- 绑定好的:
- 未绑定的:
至此,窗口的绑定工作已完成。
2. 图片显示
void MainWindow::on_pB_Open_clicked()
{
//
HTuple FileNameModel;
HObject halconImage;
QString path = QFileDialog::getOpenFileName(this,"Select Image","","(*.png *.jpg)");
if(path == "") return;
SetDraw(WindowHandle,"margin");
SetLineWidth(WindowHandle,1);
SetColor(WindowHandle,"blue");
FileNameModel = HTuple(path.toLatin1().data());
ReadImage(&halconImage,FileNameModel);
DispObj(halconImage,WindowHandle);
}
3. 完整工程代码
.pro配置文件
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = halconQt_demo1_gui
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
CONFIG += c++11
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
INCLUDEPATH += $$PWD/include/halcon18_x64
INCLUDEPATH += $$PWD/include/halcon18_x64/halconcpp
win32: LIBS += -L$$PWD/lib/halcon18_x64/ -lhalcon
INCLUDEPATH += $$PWD/lib/halcon18_x64
DEPENDPATH += $$PWD/lib/halcon18_x64
win32: LIBS += -L$$PWD/lib/halcon18_x64/ -lhalconcpp
INCLUDEPATH += $$PWD/lib/halcon18_x64
DEPENDPATH += $$PWD/lib/halcon18_x64
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#ifndef __APPLE__
# include "HalconCpp.h"
#else
# ifndef HC_LARGE_IMAGES
# include <HALCONCpp/HalconCpp.h>
# else
# include <HALCONCppxl/HalconCpp.h>
# endif
#endif
using namespace HalconCpp;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pB_Open_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QFileDialog>
#include <QDebug>
HTuple WindowHandle;//control variables
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//窗口绑定设置
Hlong windID = (Hlong)this->ui->gV_image->winId();//绑定控件与halcon窗口
//OpenWindow(0,0,ui->gV_image->width(),ui->gV_image->height(),0,"","",&WindowHandle);//如果没绑定,则Halcon窗口是弹出的,而不是嵌入在控件中的
OpenWindow(0,0,ui->gV_image->width(),ui->gV_image->height(),windID,"visible","",&WindowHandle);//创建一个窗体ui
SetPart(WindowHandle,0,0,2748,3664);//1944 2592
SetDraw(WindowHandle,"margin");
SetLineWidth(WindowHandle,1);
SetColor(WindowHandle,"red");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pB_Open_clicked()
{
//
QString path = QFileDialog::getOpenFileName(this,"Select Image","","(*.png *.jpg)");
if(path == "") return;
qDebug() << "地址" << path;
HTuple FileNameModel;
HObject halconImage;
FileNameModel = HTuple(path.toLatin1().data());
ReadImage(&halconImage,FileNameModel);
DispObj(halconImage,WindowHandle);
}