Vs下调用Qt gui 操作
新建项目——Qt GUI Application——确定后 next —— finish
Buttons 选项栏
Push Button
ctrl+r 查看效果后 返回vs 添加链接
.cpp文件中添加槽链接
ui.setupUi(this);
QObject::connect(ui.openImg, SIGNAL(clicked()), this, SLOT(OpenImgsSlot()));
QObject::connect(ui.ErodeImg, SIGNAL(clicked()), this, SLOT(ErodeImgeSlot()));
相应操作代码[完整版]
#include "Myimshow_img.h"
Myimshow_img::Myimshow_img(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
QObject::connect(ui.openImg, SIGNAL(clicked()), this, SLOT(OpenImgsSlot()));
QObject::connect(ui.ErodeImg, SIGNAL(clicked()), this, SLOT(ErodeImgeSlot()));
}
Myimshow_img::~Myimshow_img()
{
}
void Myimshow_img::OpenImgsSlot()
{
src = imread("dota.jpg");
cvtColor(src,src,CV_RGB2RGBA);
QImage src_1 = QImage((const unsigned char*)(src.data),src.cols,src.rows,QImage::Format_RGB32);//转化成Qimage格式
QLabel*label = new QLabel(this);
label->move(200, 150);
label->setPixmap(QPixmap::fromImage(src_1));
label->resize(label->pixmap()->size());
label->show();
}
void Myimshow_img::ErodeImgeSlot()
{
erode(src,dst,Mat());
QImage src_2 = QImage((const unsigned char*)(dst.data), dst.cols, dst.rows, QImage::Format_RGB32);//转化成Qimage格式
QLabel*label = new QLabel(this);
label->move(200,150);
label->setPixmap(QPixmap::fromImage(src_2));
label->resize(label->pixmap()->size());
label->show();
}
在头文件(.h)中添加:
#ifndef Myimshow_img_H
#define Myimshow_img_H
#include <QtWidgets/QMainWindow>
#include "ui_Myimshow_img.h"
#include<Qlabel>
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
class Myimshow_img : public QMainWindow
{
Q_OBJECT
public:
Myimshow_img(QWidget *parent = 0);
~Myimshow_img();
private:
Ui::Myimshow_imgClass ui;
Mat src;
Mat dst;
private slots:
void OpenImgsSlot();
void ErodeImgeSlot();
};
#endif
效果展示: