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

 

 

 

ui界面

 

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_ImageProcess.h"
#include <opencv.hpp>
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 <qfiledialog.h>
#include <QScrollArea>
#include <QColorDialog>
#include <QToolBar>
#include <qmenu.h>
#include <qaction.h>
#include <qdebug.h>
#include <qobject.h>
#include <QFontDialog>
#include <qfont.h>

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博客

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值