qt制作简单的图片处理器(只实现对图片进行添加文字) 您所在的位置:网站首页 qt导出pdf包含图片加文字 qt制作简单的图片处理器(只实现对图片进行添加文字)

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

2024-02-10 10:09| 来源: 网络整理| 查看: 265

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

.h文件

#ifndef WIDGET_H #define WIDGET_H #include #include #include #include "controlwgt.h" #include 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 #include #include #include #include "controlwgt.h" #include #include #include #include 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



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有