Qt自定义控件的创建与初步使用(二)之图片上绘制文字、箭头、曲线
来源:互联网 发布:义乌美工培训班多少钱 编辑:程序博客网 时间:2024/05/16 17:11
本文目的:编辑自定义控件的界面ui,并在图片上添文字、箭头、曲线、打开、保存等功能。并说明了如何去使用这个编辑好的ui界面控件!
上次简单的说明了如何去创建Qt自定义控件,当时还是对其了解不够深刻,现在看来,QT自定义控件就是你事先把界面写好(一般基于QWidget基类),然后再把它写入QT自定义控件的工程【可参考Qt自定义控件的创建与初步使用(一)这篇博客】中去,最后放到别的工程中,通过简单的拖拽和拷贝就可以用了。那怎么样去做和使用这个自定义的控件呢?又要注意什么呢?具体分为三大步,下面给大家娓娓道来!
配置:Qt creator5.7,Qt 5.7+VS2013(64位)(有人喜欢用后者,全凭个人爱好,我用的是creator),本篇案例的说明用的是Qt5.6(因为本人的电脑装的是Qt5.6,公司的电脑装的是Qt5.7),建议最好用Qt5.7,但实际操作中不会有太大差别。
一、建立基类是QWidget的Qt Application工程,把界面写好。
1.建立一个Qt Application工程,命名为 sof_Interface,基类是QWidget,然后一路下一步,这样你就完成了一个工程的创建(先在这个工程上把需要的界面写好)
2.打开.ui界面,此处为5个功能:打开图片、保存图片、在图片上添加文字、曲线、箭头符号。拖拽5个pushbotton、1个QLabel、1个QLineEdit、1个QDialogbottonbox,并按图所示排号界面,并选中,右键点击转到槽,从而创建相关的槽函数。并添加代码。
sof_Interface.h代码
- <span style="font-family:Times New Roman;font-size:14px;">#ifndef SOF_INTERFACE_H
- #define SOF_INTERFACE_H
-
- #include <QWidget>
- #include <QAbstractButton>
- namespace Ui {
- class sof_Interface;
- }
-
- class sof_Interface : public QWidget
- {
- Q_OBJECT
-
- public:
- explicit sof_Interface(QWidget *parent = 0);
- ~sof_Interface();
-
- void paint(QImage&theImage);
- enum Type{
- type1,
- type2,
- type3,
- };
-
- private slots:
- void on_doOpen_clicked();//打开图片
- void on_doSave_clicked();//保存图片
- void on_buttonBox_clicked(QAbstractButton *button);//ok、cancel
- void on_mou_Track_clicked();//曲线
- void on_arrow_Edit_clicked();//箭头
- void on_text_Edit_clicked();//文字
- private:
- Ui::sof_Interface *ui;
- QString curFile; //打开文件名
- bool isOpen;
- Type path_type; //画的图形种类
- QImage image,tempImage; //原图、缓存图片
- QPixmap pic; //用于显示图片
- bool isDrawing;
- QPoint begin,end;
- QPoint end_pos; //鼠标释放时的位置
- QString Text; //存储文字变量
- protected:
- void mousePressEvent(QMouseEvent *e); //鼠标按下事件
- void mouseMoveEvent(QMouseEvent *e); //鼠标移动事件
- void mouseReleaseEvent(QMouseEvent *e); //鼠标释放事
- void paintEvent(QPaintEvent *); //重绘事件
- };
-
- #endif // SOF_INTERFACE_H
- </span>
sof_Interface.cpp中代码- <span style="font-family:Times New Roman;font-size:14px;">#include "sof_interface.h"
- #include "ui_sof_interface.h"
- #include <QFileDialog>
- #include <QPainter>
- #include <QMessageBox>
- #include <QMouseEvent>
- #include <QlineEdit>
- sof_Interface::sof_Interface(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::sof_Interface)
- {
- ui->setupUi(this);
- isOpen=false;
- path_type=type1;
- isDrawing=false;
- }
-
- sof_Interface::~sof_Interface()
- {
- delete ui;
- }
-
- void sof_Interface::on_doOpen_clicked()
- {
- QString fileName = QFileDialog::getOpenFileName(
- this, "打开图片",
- "",
- "图片格式 (*.bmp *.jpg *.jpeg *.png)");
- if(fileName !=NULL)
- {
- image.load(fileName);
- curFile=fileName;
- isOpen=true;
- }
- update();
- }
-
- void sof_Interface::on_doSave_clicked()
- {
- QString path = QFileDialog::getSaveFileName(
- this, tr("保存图片"),
- "","图片格式 (*.bmp *.jpg *.jpeg *.png)");
- image.save(path);
- }
-
- void sof_Interface::on_buttonBox_clicked(QAbstractButton *button)
- {
- QString str;
-
- if(ui->buttonBox->button(QDialogButtonBox::Ok) == button)
- {
- if(!ui->lineEdit->text().isEmpty())
- {
- str += ui->lineEdit->text()+"\n";
- Text=str;
- str="";
- }
-
- }
- else if(button == ui->buttonBox->button((QDialogButtonBox::Cancel)))
- {
- ui->lineEdit->clear();
-
- }
- }
-
- void sof_Interface::on_mou_Track_clicked()
- {
- path_type=type1;
- }
-
- void sof_Interface::on_arrow_Edit_clicked()
- {
- path_type=type2;
- }
-
- void sof_Interface::on_text_Edit_clicked()
- {
- path_type=type3;
- }
-
-
-
-
- void sof_Interface::mousePressEvent(QMouseEvent *e)
- {
- begin=e->pos();
- }
-
- void sof_Interface::mouseMoveEvent(QMouseEvent *e)
- {
- end=e->pos();
-
- if(path_type==type1)
- {
- isDrawing=false;
- paint(image);
- }
- else
- {
- isDrawing=true;
- tempImage=image;
- paint(tempImage);
- }
- }
-
- void sof_Interface::mouseReleaseEvent(QMouseEvent *e)
- {
- end=e->pos();
- isDrawing=false;
- paint(image);
-
- }
-
-
- void sof_Interface::paintEvent(QPaintEvent *)
- {
- QPainter p(this);
- if(isDrawing)
- p.drawImage(0,0,tempImage);
- else
- p.drawImage(0,0,image);
-
- if(isOpen=true)
- {
- pic=QPixmap::fromImage(image);
- QPainter painter(this);
- painter.drawPixmap(0,0,748,480,pic);
- }
- }
-
-
- void sof_Interface::paint(QImage &theImage)
- {
- QPainter pp(&theImage);
-
- pp.setCompositionMode(QPainter::CompositionMode_SourceIn);
- pp.setPen(QPen(QBrush(Qt::red), 2, Qt::SolidLine));
- pp.setRenderHint(QPainter::Antialiasing, true);
- QFont font = pp.font();
- font.setPixelSize(20);
- font.setBold(false);
- pp.setFont(font);
-
- if(path_type==type1)
- {
- pp.drawLine(begin,end);
- begin=end;
- }
-
- if(path_type==type2)
- {
- float x1 = begin.x();
- float y1 = begin.y();
- float x2 = end.x();
- float y2 = end.y();
- float l = 10.0;
- float a = 0.5;
- float x3 = x2 - l * cos(atan2((y2 - y1) , (x2 - x1)) - a);
- float y3 = y2 - l * sin(atan2((y2 - y1) , (x2 - x1)) - a);
- float x4 = x2 - l * sin(atan2((x2 - x1) , (y2 - y1)) - a);
- float y4 = y2 - l * cos(atan2((x2 - x1) , (y2 - y1)) - a);
- pp.drawLine(x2,y2,x3,y3);
- pp.drawLine(x2,y2,x4,y4);
- pp.drawLine(begin,end);
- }
-
- if(path_type==type3)
- {
- pp.drawText(begin.x(),begin.y(),Text);
- }
-
- update();
- }
- </span>
main.pp中代码- <span style="font-family:Times New Roman;font-size:14px;">#include "sof_interface.h"
- #include <QApplication>
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- sof_Interface w;
- w.show();
-
- return a.exec();
- }</span>
3.界面运行效果,还可以保存哦,注意图片格式只能加载.jpg.jpeg.png.bmp,需要扩展的自己在代码中添加吧!当然还可以更改界面背景色等,自己去改哦~这只是个demo。
二、将自己第一步写的sof_Interface工程.h和.cpp的代码拷贝到自定义控件工程的.h和.cpp中即可写成自定义控件工程了,注意代码不能完全粘贴复制哦,注意自己建立工程的类名,除非你和我的一样。然后编译后见一下.dll和.lib,并放到相应的designer中,具体可参考【Qt自定义控件的创建与初步使用(一)这篇博客】。
三、这样你再新建立任意一个工程,你都可以在Qt creator或Qt 设计师中找到自己建立的控件,然后拖拽到工程的ui界面中,最后拷贝自定义控件.h和.cpp到这个新工程的工程目录下,注意和第二大步不一样哦~,就可以编译运行啦。再次就不给效果图啦~
源博客地址:http://blog.csdn.net/panshun888/article/details/52074400
阅读全文
0 0
- Qt自定义控件的创建与初步使用(二)之图片上绘制文字、箭头、曲线
- Qt自定义控件的创建与初步使用(二)之图片上绘制文字、箭头、曲线
- Qt自定义控件的创建与初步使用(一)之自定义控件的创建步骤
- Qt自定义控件的创建与初步使用(一)之自定义控件的创建步骤
- Qt自定义控件的创建与初步使用
- Qt自定义委托在QTableView中绘制控件、图片、文字
- Qt自定义委托在QTableView中绘制控件、图片、文字
- Qt自定义委托在QTableView中绘制控件、图片、文字
- Qt自定义委托在QTableView中绘制控件、图片、文字
- Qt自定义委托在QTableView中绘制控件、图片、文字
- Qt自定义委托在QTableView中绘制控件、图片、文字
- Android绘图机制(二)——自定义View绘制形, 圆形, 三角形, 扇形, 椭圆, 曲线,文字和图片的坐标讲解
- Android绘图机制(二)——自定义View绘制形, 圆形, 三角形, 扇形, 椭圆, 曲线,文字和图片的坐标讲解
- QT绘制多边形、渐变线段、文字、带线段的箭头
- 自定义View绘制形, 圆形, 三角形, 扇形, 椭圆, 曲线,文字和图片的坐标讲解
- Android自定义“图片+文字”控件四种实现方法之 二--------个人最推荐的一种
- 自定义控件学习笔记(四)文字的绘制
- Android 自定义控件文字的居中绘制
- 实现一个函数,1打印乘法口诀表(可更改行列),2交换两数,3判断是否闰年,4判断是否素数。
- 为什么GPU对于深度学习如此重要
- Tomcat启动速度慢的优化方法
- 虚拟机网路连接问题
- 互联网的时代 我们应该怎么做?
- Qt自定义控件的创建与初步使用(二)之图片上绘制文字、箭头、曲线
- 欢迎使用CSDN-markdown编辑器
- JAVA面试易错题目总结(一)
- HTML 表单元素(上)
- 利用 Virtualbox 进行 Ironic 环境的搭建和测试
- 在pl/sql developer中如何创建 database link及使用详解
- Ubuntu16.04:Python3+Nginx+Django+mysql+git配置,sublime text
- vsftpd配置文件详解
- Maven的核心笔记(3)常用命令、坐标和仓库