本文主要是介绍QPainter融合两张图片渲染,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
QPainter融合两张图片渲染
QPainter painter(this);
out = src.copy();
QPainter p(&out);
p.setRenderHint(QPainter::Antialiasing);
p.drawImage(0,0,img);
p.end();
painter.drawImage(0,0,out);
painter.end();
XPS图片编辑器
1.打开存储图片(菜单)
2.画笔和橡皮
3.画笔大小(QSlider)
4.画笔颜色(QColorDialog)
ximage.h
#pragma once
#include <QWidget>
#include <QMouseEvent>
#include <QColor>
#include <QPen>class XImage : public QWidget
{Q_OBJECT
public:XImage(QWidget *parent = nullptr);~XImage();void paintEvent(QPaintEvent *ev);void mouseMoveEvent(QMouseEvent *ev);void mouseReleaseEvent(QMouseEvent *ev);
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;
};
ximage.cpp
#include "ximage.h"
#include <QPainter>
#include <QFileDialog>XImage::XImage(QWidget *parent):QWidget(parent)
{}XImage::~XImage()
{}void XImage::paintEvent(QPaintEvent *ev)
{QPainter p(this);if(!out.isNull())p.drawImage(0,0,out);
}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();
} 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();
}void XImage::mouseReleaseEvent(QMouseEvent *ev)
{mpos = QPoint();
}void XImage::setPen(int size,QColor col)
{pen.setWidth(size);pen.setBrush(col);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;
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>enum XSTATUS
{XPEN,XERASER
};QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:XSTATUS status = XPEN;MainWindow(QWidget *parent = nullptr);~MainWindow();
public slots:void setEraser();void setPen();void setColor();void save();
private:QColor col;Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QColorDialog>
#include <QButtonGroup>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{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,200);setPen();//画笔和橡皮按钮排他选择QButtonGroup *gp = new QButtonGroup(this);gp->addButton(ui->eraseButton);gp->addButton(ui->penButton);gp->setExclusive(true);
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::setPen()
{ui->image->setPen(ui->penSize->value(),col);status = XPEN;ui->penButton->setChecked(true);
}void MainWindow::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 MainWindow::setEraser()
{ui->image->setEraser(ui->penSize->value());status = XERASER;ui->eraseButton->setChecked(true);
}void MainWindow::save()
{ui->image->save();ui->statusbar->showMessage(QStringLiteral("保存结束"),5000);
}
main.cpp
#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;w.show();return a.exec();
}
ui_mainwindow.h
#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QGroupBox>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenu>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QScrollArea>
#include <QtWidgets/QSlider>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QWidget>
#include "ximage.h"QT_BEGIN_NAMESPACEclass Ui_MainWindow
{
public:QAction *openaction;QAction *saveaction;QWidget *centralwidget;QGroupBox *groupBox;QPushButton *penButton;QPushButton *eraseButton;QPushButton *colorButton;QSlider *penSize;QScrollArea *scrollArea;XImage *image;QMenuBar *menubar;QMenu *menu;QStatusBar *statusbar;void setupUi(QMainWindow *MainWindow){if (MainWindow->objectName().isEmpty())MainWindow->setObjectName(QString::fromUtf8("MainWindow"));MainWindow->resize(800, 600);openaction = new QAction(MainWindow);openaction->setObjectName(QString::fromUtf8("openaction"));saveaction = new QAction(MainWindow);saveaction->setObjectName(QString::fromUtf8("saveaction"));centralwidget = new QWidget(MainWindow);centralwidget->setObjectName(QString::fromUtf8("centralwidget"));groupBox = new QGroupBox(centralwidget);groupBox->setObjectName(QString::fromUtf8("groupBox"));groupBox->setGeometry(QRect(0, 0, 121, 561));penButton = new QPushButton(groupBox);penButton->setObjectName(QString::fromUtf8("penButton"));penButton->setGeometry(QRect(20, 40, 48, 48));penButton->setStyleSheet(QString::fromUtf8("background-image: url(./Resources/pen.png);"));penButton->setCheckable(true);penButton->setChecked(true);eraseButton = new QPushButton(groupBox);eraseButton->setObjectName(QString::fromUtf8("eraseButton"));eraseButton->setGeometry(QRect(20, 120, 48, 48));eraseButton->setStyleSheet(QString::fromUtf8("background-image: url(./Resources/eraser.png);"));eraseButton->setCheckable(true);colorButton = new QPushButton(groupBox);colorButton->setObjectName(QString::fromUtf8("colorButton"));colorButton->setGeometry(QRect(20, 200, 48, 48));colorButton->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 0, 0);"));penSize = new QSlider(groupBox);penSize->setObjectName(QString::fromUtf8("penSize"));penSize->setGeometry(QRect(0, 280, 101, 16));penSize->setOrientation(Qt::Horizontal);scrollArea = new QScrollArea(centralwidget);scrollArea->setObjectName(QString::fromUtf8("scrollArea"));scrollArea->setGeometry(QRect(120, 0, 681, 581));scrollArea->setWidgetResizable(false);image = new XImage();image->setObjectName(QString::fromUtf8("image"));image->setGeometry(QRect(0, 0, 679, 579));scrollArea->setWidget(image);MainWindow->setCentralWidget(centralwidget);menubar = new QMenuBar(MainWindow);menubar->setObjectName(QString::fromUtf8("menubar"));menubar->setGeometry(QRect(0, 0, 800, 27));menu = new QMenu(menubar);menu->setObjectName(QString::fromUtf8("menu"));MainWindow->setMenuBar(menubar);statusbar = new QStatusBar(MainWindow);statusbar->setObjectName(QString::fromUtf8("statusbar"));MainWindow->setStatusBar(statusbar);menubar->addAction(menu->menuAction());menu->addAction(openaction);menu->addAction(saveaction);retranslateUi(MainWindow);QObject::connect(penSize, SIGNAL(valueChanged(int)), MainWindow, SLOT(setPen()));QObject::connect(colorButton, SIGNAL(clicked()), MainWindow, SLOT(setColor()));QObject::connect(penButton, SIGNAL(clicked()), MainWindow, SLOT(setPen()));QObject::connect(eraseButton, SIGNAL(clicked()), MainWindow, SLOT(setEraser()));QMetaObject::connectSlotsByName(MainWindow);} // setupUivoid retranslateUi(QMainWindow *MainWindow){MainWindow->setWindowTitle(QCoreApplication::translate("MainWindow", "MainWindow", nullptr));openaction->setText(QCoreApplication::translate("MainWindow", "\346\211\223\345\274\200", nullptr));
#if QT_CONFIG(shortcut)openaction->setShortcut(QCoreApplication::translate("MainWindow", "Ctrl+O", nullptr));
#endif // QT_CONFIG(shortcut)saveaction->setText(QCoreApplication::translate("MainWindow", "\344\277\235\345\255\230", nullptr));
#if QT_CONFIG(shortcut)saveaction->setShortcut(QCoreApplication::translate("MainWindow", "Ctrl+S", nullptr));
#endif // QT_CONFIG(shortcut)groupBox->setTitle(QCoreApplication::translate("MainWindow", "\345\267\245\345\205\267\346\240\217", nullptr));penButton->setText(QString());eraseButton->setText(QString());colorButton->setText(QCoreApplication::translate("MainWindow", "\351\242\234\350\211\262", nullptr));menu->setTitle(QCoreApplication::translate("MainWindow", "\346\226\207\344\273\266", nullptr));} // retranslateUi};namespace Ui {class MainWindow: public Ui_MainWindow {};
} // namespace UiQT_END_NAMESPACE#endif // UI_MAINWINDOW_H
这篇关于QPainter融合两张图片渲染的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!