VS QT opencv打开显示图片,界面添加工具栏,在工具栏中添加字体和颜色对话框功能

VS QT opencv打开显示图片,界面添加工具栏,在工具栏中添加字体和颜色对话框功能_第1张图片

 VS QT opencv打开显示图片,界面添加工具栏,在工具栏中添加字体和颜色对话框功能_第2张图片

 VS QT opencv打开显示图片,界面添加工具栏,在工具栏中添加字体和颜色对话框功能_第3张图片

 

ui界面

VS QT opencv打开显示图片,界面添加工具栏,在工具栏中添加字体和颜色对话框功能_第4张图片

 

#pragma once

#include 
#include "ui_ImageProcess.h"
#include 
using namespace cv;

class ImageProcess : public QMainWindow
{
    Q_OBJECT

public:
    ImageProcess(QWidget *parent = nullptr);
    ~ImageProcess();

private:
    Ui::ImageProcessClass ui;

    Mat image;
    QLabel* label;
    QLabel* label_2;



    //添加槽函数
private slots:
    void on_OpenFig_clicked();
    //按格式on_控件名_clicked命名函数,QT会默认完成函数和按钮动作的链接,如果不这样命名的话就去设置信号槽函数
    void on_Progress_clicked();

};

#include "ImageProcess.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

ImageProcess::ImageProcess(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

	//设置主窗口的大小 
	resize(1400, 480 * 2);


	//    将工具栏放入窗口
	QToolBar* toolBar = new QToolBar();
	//    设置默认值
	addToolBar(Qt::TopToolBarArea, toolBar);
	//    只允许上下停靠
	toolBar->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
	toolBar->setFloatable(false);
	toolBar->setMovable(true);

	//在工具栏中添加工具
	toolBar->addAction(ui.actionopen);
	toolBar->addAction(ui.actionsave);
	toolBar->addAction(ui.actionawRect);
	toolBar->addAction(ui.actionzoom);


	//添加颜色对话框
	connect(ui.actioncolor, &QAction::triggered, this, [=]() {

		QColor color = QColorDialog::getColor(Qt::white, this);
		qDebug() << "color info" << color << color.name();

		});



	//添加字体对话框
	connect(ui.actionfont, &QAction::triggered, this, [=]() {
		
		bool ok;
		QFont font = QFontDialog::getFont(&ok, QFont("Times", 12), this);
		if (ok) {
			// font is set to the font the user selected
		}
		else {
			// the user canceled the dialog; font is set to the initial
			// value, in this case Times, 12.
		}

		});




}

void ImageProcess::on_OpenFig_clicked()
{

	QString filename;
	filename = QFileDialog::getOpenFileName(this,
		tr("Select Image File"),
		"",
		tr(" *.png *.bmp *.jpg *.tif *.GIF "));

	if (filename.isEmpty())
	{
		return;
	}
	else
	{

		string str = filename.toStdString();  // 将filename转变为string类型;
		image = imread(str);
		//image=imread(fileName.toLatin1().data);
		cvtColor(image, image, COLOR_BGR2RGB);
		cv::resize(image, image, Size(300*2, 200*2));
		QImage img = QImage((const unsigned char*)(image.data), image.cols, image.rows, QImage::Format_RGB888);

		label = new QLabel();
		label->setPixmap(QPixmap::fromImage(img));
		label->resize(QSize(img.width(), img.height()));
		ui.scrollArea->setWidget(label);


	}

}

void ImageProcess::on_Progress_clicked()
{
	flip(image, image, 4);//反转函数 0 上下反转;整数,水平发转;负数,水平垂直均反转
	QImage img1 = QImage((const unsigned char*)(image.data), image.cols, image.rows, QImage::Format_RGB888);
	label_2 = new QLabel();
	label_2->setPixmap(QPixmap::fromImage(img1));
	label_2->resize(QSize(img1.width(), img1.height()));
	ui.scrollArea_2->setWidget(label_2);

}

ImageProcess::~ImageProcess()
{}

打开和处理图像的按钮的槽函数通过手动连接参考下文

QT+VS+Opencv制作界面显示图片_学霸家有事的博客-CSDN博客

你可能感兴趣的:(C/C++/QT,qt,opencv,开发语言)