QT界面:QT+VS2015+Halcon圖像簡單處理與顯示

環境:Win10-x64+VS2015+Qt5.9.7+Halcon12

首先創建一個Qt GUI Application。工程名:myDockWidget。將Halcon12包含目錄與庫目錄文件放到工程目錄下:
在這裏插入圖片描述
選擇工程屬性頁,配置halcon環境:

在這裏插入圖片描述
在這裏插入圖片描述

頭文件

#pragma once

#include <QtWidgets/QMainWindow>
#include <QHBoxLayout>
#include <QPushButton>
#include <QRadioButton>
#include <QCheckBox>
#include <QSpinBox>
#include <QLabel>
#include <QTextEdit>
#include <QDockWidget>
#include <QMessageBox>
#include <QFileDialog>
#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, "提示:", "處理結束!");
	}
}

最終效果:

在這裏插入圖片描述

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章