Qt-Xps项目

20 篇文章 0 订阅
本文档展示了使用Qt库创建一个图像编辑应用程序的过程。`xps.ui`文件用于定义用户界面,包括菜单、按钮和滑块等元素。`XImage`类实现了图像的打开、保存、画笔选择、橡皮擦功能以及鼠标事件处理。`Xps`类作为主窗口,包含了状态栏、菜单和槽函数,用于响应用户操作。整个应用允许用户在图像上进行绘画并调整画笔大小和颜色。
摘要由CSDN通过智能技术生成

ui_xps.h

/********************************************************************************
** Form generated from reading UI file 'xps.ui'
**
** Created by: Qt User Interface Compiler version 5.14.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_XPS_H
#define UI_XPS_H

#include <QtCore/QVariant>
#include <QtWidgets/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_NAMESPACE

class Ui_XpsClass
{
public:
    QAction *openaction;
    QAction *saveaction;
    QWidget *centralWidget;
    QGroupBox *groupBox;
    QPushButton *colorButton;
    QPushButton *penButton;
    QPushButton *eraseButton;
    QSlider *penSize;
    QScrollArea *scrollArea;
    XImage *image;
    QMenuBar *menuBar;
    QMenu *menu;
    QStatusBar *statusBar;

    void setupUi(QMainWindow *XpsClass)
    {
        if (XpsClass->objectName().isEmpty())
            XpsClass->setObjectName(QString::fromUtf8("XpsClass"));
        XpsClass->resize(600, 400);
        openaction = new QAction(XpsClass);
        openaction->setObjectName(QString::fromUtf8("openaction"));
        saveaction = new QAction(XpsClass);
        saveaction->setObjectName(QString::fromUtf8("saveaction"));
        centralWidget = new QWidget(XpsClass);
        centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
        groupBox = new QGroupBox(centralWidget);
        groupBox->setObjectName(QString::fromUtf8("groupBox"));
        groupBox->setGeometry(QRect(10, 0, 151, 341));
        colorButton = new QPushButton(groupBox);
        colorButton->setObjectName(QString::fromUtf8("colorButton"));
        colorButton->setGeometry(QRect(20, 30, 93, 28));
        colorButton->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 85, 255);\n"
"color: rgb(255, 255, 255);"));
        penButton = new QPushButton(groupBox);
        penButton->setObjectName(QString::fromUtf8("penButton"));
        penButton->setGeometry(QRect(20, 70, 93, 28));
        penButton->setCheckable(true);
        penButton->setChecked(true);
        eraseButton = new QPushButton(groupBox);
        eraseButton->setObjectName(QString::fromUtf8("eraseButton"));
        eraseButton->setGeometry(QRect(20, 110, 93, 28));
        eraseButton->setCheckable(true);
        penSize = new QSlider(groupBox);
        penSize->setObjectName(QString::fromUtf8("penSize"));
        penSize->setGeometry(QRect(10, 170, 111, 22));
        penSize->setOrientation(Qt::Horizontal);
        scrollArea = new QScrollArea(centralWidget);
        scrollArea->setObjectName(QString::fromUtf8("scrollArea"));
        scrollArea->setGeometry(QRect(130, 0, 471, 351));
        scrollArea->setWidgetResizable(false);
        image = new XImage();
        image->setObjectName(QString::fromUtf8("image"));
        image->setGeometry(QRect(0, 0, 469, 349));
        scrollArea->setWidget(image);
        XpsClass->setCentralWidget(centralWidget);
        menuBar = new QMenuBar(XpsClass);
        menuBar->setObjectName(QString::fromUtf8("menuBar"));
        menuBar->setGeometry(QRect(0, 0, 600, 26));
        menu = new QMenu(menuBar);
        menu->setObjectName(QString::fromUtf8("menu"));
        XpsClass->setMenuBar(menuBar);
        statusBar = new QStatusBar(XpsClass);
        statusBar->setObjectName(QString::fromUtf8("statusBar"));
        XpsClass->setStatusBar(statusBar);

        menuBar->addAction(menu->menuAction());
        menu->addAction(openaction);
        menu->addAction(saveaction);

        retranslateUi(XpsClass);
        QObject::connect(colorButton, SIGNAL(clicked()), XpsClass, SLOT(SetColor()));
        QObject::connect(penButton, SIGNAL(clicked()), XpsClass, SLOT(SetPen()));
        QObject::connect(eraseButton, SIGNAL(clicked()), XpsClass, SLOT(SetEraser()));
        QObject::connect(penSize, SIGNAL(valueChanged(int)), XpsClass, SLOT(SetPen()));

        QMetaObject::connectSlotsByName(XpsClass);
    } // setupUi

    void retranslateUi(QMainWindow *XpsClass)
    {
        XpsClass->setWindowTitle(QCoreApplication::translate("XpsClass", "Xps", nullptr));
        openaction->setText(QCoreApplication::translate("XpsClass", "open", nullptr));
#if QT_CONFIG(shortcut)
        openaction->setShortcut(QCoreApplication::translate("XpsClass", "Ctrl+O", nullptr));
#endif // QT_CONFIG(shortcut)
        saveaction->setText(QCoreApplication::translate("XpsClass", "save", nullptr));
#if QT_CONFIG(shortcut)
        saveaction->setShortcut(QCoreApplication::translate("XpsClass", "Ctrl+S", nullptr));
#endif // QT_CONFIG(shortcut)
        groupBox->setTitle(QCoreApplication::translate("XpsClass", "\345\267\245\345\205\267\346\240\217", nullptr));
        colorButton->setText(QCoreApplication::translate("XpsClass", "\351\242\234\350\211\262", nullptr));
        penButton->setText(QCoreApplication::translate("XpsClass", "\347\224\273\347\254\224", nullptr));
        eraseButton->setText(QCoreApplication::translate("XpsClass", "\346\251\241\347\232\256\346\223\246", nullptr));
        menu->setTitle(QCoreApplication::translate("XpsClass", "\350\217\234\345\215\225", nullptr));
    } // retranslateUi

};

namespace Ui {
    class XpsClass: public Ui_XpsClass {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_XPS_H

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;
};

ximage.cpp

#include "ximage.h"
#include <QFileDialog>
#include <QPainter>
#include <QMouseEvent>
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();

}
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::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(col);
	pen.setCapStyle(Qt::RoundCap);
	pen.setJoinStyle(Qt::RoundJoin);
}

//橡皮擦
void XImage::SetEraser(int size)
{
	pen.setWidth(size);
	//用原图做画笔,实现恢复
	pen.setBrush(QBrush(src));
}


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.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;
private:
    Ui::XpsClass ui;
};

xps.cpp

#include "xps.h"
#include <QButtonGroup>
#include <QColorDialog>
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->setChecked(true);
}
void Xps::SetEraser()
{
	ui.image->SetEraser(ui.penSize->value());
	status = XERASER;
	ui.eraseButton->setChecked(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("保存结束"), 5000);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值