QT学习第6天:画板制作(实现在图片上上绘制图像和橡皮擦功能)

项目名称:XPS
编译工具:VS2017

效果图

编辑功能:

保存功能

头文件1:ximage.h代码

#pragma once

#include <QWidget>
#include<QPen>
class XImage : public QWidget
{
   
	Q_OBJECT

public:
	XImage(QWidget *parent = 0);
	~XImage();

	void paintEvent(QPaintEvent * ev);
	void mouseMoveEvent(QMouseEvent *ev);
	void mouseReleaseEvent(QMouseEvent *ev)
	{
   
		mpos = QPoint();
	}
public slots:
	void Open();
	void SetPen(int size, QColor col);
	void SetEraser(int size);
	void Save();
protected:
	//原图
	QImage src;
	//输出图像
	QImage out;
	//上一次的鼠标位置
	QPoint mpos;
	QPen pen;
};

xps.h代码

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_xps.h"
enum XSTATUS
{
   
	XPEN,
	XERASER
};
class XPS : public QMainWindow
{
   
    Q_OBJECT

public:
	XSTATUS status = XPEN;
    XPS(QWidget *parent = Q_NULLPTR);
public slots:
	void SetEraser();
	void SetPen();
	void SetColor();
	void Save();
private:
	QColor col;
    Ui::XPSClass ui;
};

main.cpp代码

#include "xps.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
   
    QApplication a(argc, argv);
    XPS w;
    w.show();
	w.setWindowTitle(QStringLiteral( "往事随风牌画盘"));
    return a.exec();
}

ximage.cpp代码

#include "ximage.h"
#include <QPainter>
#include <QFileDialog>
#include <QMouseEvent>
#include <QPen>
XImage::XImage(QWidget *parent)
	: QWidget(parent)
{
   
}

XImage::~XImage()
{
   
}
void XImage::Open()
{
   
	//打开图像 用户选择图片
	QString filename = QFileDialog::getOpenFileName(this, QStringLiteral("打开图片"), "",
		QStringLiteral("支持格式(*.png *.jpg *.bmp)"));
	if (filename.isEmpty())return;
	if(!src.load(filename))return;
	resize(src.size());
	mpos = QPoint();
	out = src.copy();
	update();
}
void XImage::paintEvent(QPaintEvent *ev)
{
   
	QPainter p(this);
	if (!out.isNull())
		p.drawImage(0, 0, out);

}

void XImage::SetPen(int size, QColor col)
{
   
	
	pen.setWidth(size);
	pen.setBrush(Qt::red);
	pen.setCapStyle(Qt::RoundCap);
	pen.setJoinStyle(Qt::RoundJoin);
}
//橡皮擦
void XImage::SetEraser(int size)
{
   
	pen.setWidth(size);
	pen.setBrush(QBrush(src));
}
void XImage::Save()
{
   
	if (out.isNull())return;
	QString filename=QFileDialog::getSaveFileName(this, QStringLiteral("保存文件"), ""
		, QStringLiteral("支持格式(*.png *.jpg *.bmp)"));
	if (filename.isNull())return;
	if (!out.save(filename))return;
}
void XImage::mouseMoveEvent(QMouseEvent * ev)
{
   
	//绘制到原图中
	if (out.isNull())return;
	QPainter p(&out);
	
	//开启抗锯齿
	p.setRenderHint(QPainter::Antialiasing);
	p.setPen(pen);
	if (mpos.isNull())mpos = ev->pos();
	p.drawLine(QLine(mpos, ev->pos()));
	mpos = ev->pos();
	update();
}

xps.cpp代码

#include "xps.h"
#include<QColorDialog>
#include<QButtonGroup>
XPS::XPS(QWidget *parent)
    : QMainWindow(parent)
{
   
    ui.setupUi(this);
	//打开图片
	connect(ui.openaction, SIGNAL(triggered()), ui.image, SLOT(Open()));
	//保存图片
	connect(ui.saveaction, SIGNAL(triggered()), ui.image, SLOT(Save()));
	col = QColor(200, 0, 0, 255);
	SetPen();
	//画笔和橡皮按钮排他选择
	QButtonGroup *gp = new QButtonGroup(this);
	gp->addButton(ui.eraseButton);
	gp->addButton(ui.penButton);
	gp->setExclusive(true);
}
void XPS::SetPen()
{
   
	ui.image->SetPen(ui.penSize->value(), col);
	status = XPEN;
	ui.penButton->setCheckable(true);
}


void XPS::SetEraser()
{
   
	ui.image->SetEraser(ui.penSize->value());
	status = XERASER;
	ui.eraseButton->setCheckable(true);
}
void XPS::SetColor()
{
   
	col=QColorDialog::getColor(Qt::red, this);
	
	QString sty = QString("background-color:rgba(%1,%2,%3,%4)")
		.arg(col.red()).arg(col.green()).arg(col.blue()).arg(col.alpha());
	ui.colorButton->setStyleSheet(sty);
	
	SetPen();
}

void XPS::Save()
{
   
	ui.image->Save();
	ui.statusBar->showMessage(QStringLiteral("保存成功,5s后自动关闭"),5000);
}


ui文件

(不好描述,已经打包在源码里)

源文件压缩包

链接:https://pan.baidu.com/s/11RKURSpVJIb8NTPyFgPCYw
提取码:0818

全部评论

相关推荐

过往烟沉:我说什么来着,java就业面就是广!
点赞 评论 收藏
分享
jack_miller:我给我们导员说我不在这里转正,可能没三方签了。导员说没事学校催的时候帮我想办法应付一下
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务