qt制作简单的图片处理器(只实现对图片进行添加文字)

由于最近需要频繁的在图片上增加文字什么的,就是为了方便阅读,增加一些注释,一开始我是使用的美图秀秀,对图片进行编辑的,后来就想着自己为什么不实现一个,反正原理挺简单的,然后就自己动手试着做了一个简单的图片编辑器。
功能只实现一个在图片上增加文字,因为我目前只需要这一个功能。
这个添加文字功能原理很简单,只需要在图片上方进行添加一个label,然后就是设置字体啊,文字大小啊,等等。。,其次就是拖拽的实现。
代码较多,我就不全部写完了,如有需要可以到这里进行下载
不多说直接看代码。

.h文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include "controlwgt.h"
#include <QCloseEvent>

class Widget : public QWidget
{
    Q_OBJECT

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

    void Init();

    void CreateToolWgt();

protected:
    void closeEvent(QCloseEvent *event);

protected slots:
    void onOpenClicked();
    void onEditBoxClicked();
    void ReceiverMessage(QString i_text,QString i_fraimly,int i_size);
    void onSaveImage();

private:
    QPushButton *m_pOpenBtn=nullptr;
    QPushButton *m_pEditBoxBtn=nullptr;
    QPushButton *m_pSaveImage=nullptr;
    QLabel *m_pImageLabel=nullptr;
    QWidget *m_pToolWgt=nullptr;

    //controlWgt *m_pControlWgt=nullptr;

};
#endif // WIDGET_H

.cpp

#include "widget.h"
#include <QHBoxLayout>
#include <QFileDialog>
#include <qdebug.h>
#include <mylabel.h>
#include "controlwgt.h"
#include <QGuiApplication>
#include <QScreen>
#include <QApplication>
#include <QDesktopWidget>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    Init();
}

Widget::~Widget()
{
}

void Widget::Init()
{
    CreateToolWgt();
    m_pImageLabel=new QLabel(this);
    m_pImageLabel->setStyleSheet("background:white");
    QVBoxLayout *mainLayout=new QVBoxLayout(this);
    mainLayout->addWidget(m_pToolWgt);
    mainLayout->addWidget(m_pImageLabel);
    mainLayout->setSpacing(0);
    mainLayout->setMargin(0);
    this->setLayout(mainLayout);
    this->resize(800,500);

}

void Widget::CreateToolWgt()
{
    m_pOpenBtn=new QPushButton(tr("Open Image"),this);
    m_pOpenBtn->setFixedSize(80,37);

    m_pEditBoxBtn=new QPushButton(tr("Text Box"),this);
    m_pEditBoxBtn->setFixedSize(80,37);

    m_pSaveImage=new QPushButton(tr("Save Image"),this);
    m_pSaveImage->setFixedSize(80,37);

    m_pToolWgt=new QWidget(this);

    QHBoxLayout *toolLayout=new QHBoxLayout(this);
    toolLayout->addWidget(m_pOpenBtn);
    toolLayout->addWidget(m_pEditBoxBtn);
    toolLayout->addWidget(m_pSaveImage);
    toolLayout->addStretch();
    toolLayout->setMargin(0);
    toolLayout->setSpacing(5);
    m_pToolWgt->setLayout(toolLayout);
    m_pToolWgt->setFixedHeight(50);

    connect(m_pOpenBtn,&QPushButton::clicked,this,&Widget::onOpenClicked);
    connect(m_pEditBoxBtn,&QPushButton::clicked,this,&Widget::onEditBoxClicked);
    connect(m_pSaveImage,&QPushButton::clicked,this,&Widget::onSaveImage);


}

void Widget::closeEvent(QCloseEvent *event)
{

}

void Widget::onOpenClicked()
{
    QString strPath=QFileDialog::getOpenFileName(this,"","","JPG(*.jpg);;PNG(*.png)");
    QPixmap pixMap(strPath);
    m_pImageLabel->setPixmap(pixMap);
}

void Widget::onEditBoxClicked()
{
    QPoint globalPos = this->mapToGlobal(QPoint(0,0));//父窗口绝对坐标
    int x = globalPos.x() + (this->width() - this->width()) / 2;//x坐标
    int y = globalPos.y() + (this->height() - this->height()+100) / 2;//y坐标

    myLabel *textLabel=new myLabel(this);
    textLabel->move(x,y);
    controlWgt *m_pControlWgt=new controlWgt(this);
    m_pControlWgt->move(this->width(),this->height()/2);
    textLabel->show();
    m_pControlWgt->show();

    connect(m_pControlWgt,&controlWgt::sSendTextFont,textLabel,&myLabel::ReceiverTextFont);
    connect(m_pControlWgt,&controlWgt::sSendText,textLabel,&myLabel::ReceiverText);
    connect(m_pControlWgt,&controlWgt::sSendOk,textLabel,&myLabel::ReceiverOK);

    connect(textLabel,&myLabel::sSendMessage,this,&Widget::ReceiverMessage);

}

void Widget::ReceiverMessage(QString i_text, QString i_fraimly, int i_size)
{
    controlWgt *m_pControlWgt=new controlWgt(this);
    m_pControlWgt->move(this->width(),this->height()/2);
    m_pControlWgt->SetText(i_text);
    m_pControlWgt->SetFamily(i_fraimly);
    m_pControlWgt->SetFontSize(i_size);

    m_pControlWgt->show();
}

void Widget::onSaveImage()
{
    QString strPath = QDir::currentPath();
    QRect rect =QRect(this->pos().x(), this->pos().y()+160, this->width(), this->height());
    QString strFileName = "Data";
    strFileName = strPath + "/" + strFileName;
    strFileName += ".png";

    QScreen *screen = QGuiApplication::primaryScreen();
    screen->grabWindow(QApplication::desktop()->winId(), rect.x(), rect.y(), rect.width(), rect.height()).save(strFileName, "png");
}


这里只是主窗口的程序。仅供参考。
如需要全部程序代码,请移步资源进行下载

效果图:
在这里插入图片描述

在这里插入图片描述我上传是0积分,自己去下载啊。

界面并未美化,如有需要,可自己进行美化。
ヾ( ̄▽ ̄)ByeBye

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

加油小杜(接qt定制功能,单模块开发等)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值