画图软件qt实现

  • F4_422496
    了解作者
  • 23.9KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-19 07:45
    上传日期
6.1 演示案例 【任务目标】 实现简单的绘图功能,并支持绘图数据的保存和打开等功能。 通过 Draw 菜单,由用户点击选择不同的图形,按下鼠标左键并拖动鼠标开始绘 图,松开鼠标后保存图形(线条、椭圆、矩形)数据,并更新屏幕显示。 File 菜单下定义文件的基本操作, New 菜单项用于新建绘图文件,如果上一 个文件修改了并没有保存,需要进行提示, Save 用于将绘图结果保存为磁盘文 件,实现持久化存储, Open 用于打开存盘的图形文件。 6.1.1 简单绘图版本
Draw6.rar
  • Draw6
  • rectangle.h
    419B
  • mainwindow.cpp
    6.2KB
  • centerwidget.h
    1.5KB
  • line.cpp
    591B
  • main.cpp
    238B
  • Draw6.pro
    551B
  • shape.h
    336B
  • ellipse.h
    409B
  • rectangle.cpp
    670B
  • mainwindow.h
    983B
  • line.h
    396B
  • shape.cpp
    165B
  • Draw6.pro.user
    17.6KB
  • ellipse.cpp
    655B
  • centerwidget.cpp
    6.3KB
  • Q7XRX7XOK)BVG%6J)HV7EGE.png
    15KB
内容介绍
#include "centerwidget.h" #include<QPainter> #include<QStatusBar> #include "mainwindow.h" #include<QFileDialog> #include<QTextStream> CenterWidget::CenterWidget(QWidget *parent) : QWidget(parent){ //lines.clear(); //ellipses.clear(); //rects.clear(); shapes.clear(); ////////////////////// beginDraw=false; colorType=0; isModified=false; fileName=tr(""); mousePosLabel=new QLabel; mousePosLabel->setText(""); mousePosLabel->setFixedWidth(150); MainWindow *p=(MainWindow *)parent; p->statusBar()->addPermanentWidget(mousePosLabel); setMinimumSize(500,400); setMouseTracking(true); } void CenterWidget::paintEvent(QPaintEvent *){ QPainter p(this); /*for(auto const& line:lines) p.drawLine(line); for(auto const& ellipse:ellipses) p.drawEllipse(ellipse); for(auto const& rect:rects) p.drawRect(rect); if(beginDraw==true){ switch(drawType){ case 0: p.drawLine(p1,p2); break; case 1: p.drawEllipse(QRect(p1,p2)); break; case 2: p.drawRect(QRect(p1,p2)); break; } }*/ QColor c; for(auto shape:shapes){ switch(colorType){ case 0:shape->setColor(Qt::black);c=Qt::black;break; case 1:shape->setColor(Qt::green);c=Qt::green;break; case 2:shape->setColor(Qt::red);c=Qt::red;break; } shape->draw(&p); //在此处内部写入,修改颜色 } Shape *temp=nullptr; switch(drawType){ case 0: temp=new Line(p1,p2,c); break; case 1: temp=new Ellipse(p1,p2,c); break; case 2: temp=new Rectangle(p1,p2,c); break; } temp->draw(&p); delete temp; } void CenterWidget::setDrawType(int type){ drawType=type; } void CenterWidget::setcolorType(int color){ colorType=color; } void CenterWidget::mousePressEvent(QMouseEvent *e){ p1=e->pos(); //鼠标按下得到第一个点 p2=p1; beginDraw=true; } void CenterWidget::mouseReleaseEvent(QMouseEvent *e){ p2=e->pos(); //鼠标松开得到第二个点 beginDraw=false; if(p1==p2) return; //switch (drawType) { //case 0:lines.append(QLine(p1,p2));break; //case 1:ellipses.append(QRect(p1,p2));break; //case 2:rects.append(QRect(p1,p2));break;} Shape *shape=nullptr; switch(drawType){ case 0: shape=new Line(p1,p2); break; case 1: shape=new Ellipse(p1,p2); break; case 2: shape=new Rectangle(p1,p2); break; } shapes.append(shape); isModified=true; update(); } void CenterWidget::mouseMoveEvent(QMouseEvent *e){ mousePosLabel->setText("X:"+QString::number(e->x())+",Y:" +QString::number(e->y())); if(beginDraw==false) return; p2=e->pos(); update(); } bool CenterWidget::getModifiedFlag(){ return isModified; } void CenterWidget::newDrawing(){ //新建绘图 // lines.clear(); //ellipses.clear(); //rects.clear(); //清空图形内容 for(auto shape:shapes) delete shape; shapes.clear(); beginDraw=false; isModified=false; fileName=tr(""); parentWidget()->setWindowTitle(tr("简单绘图程序 未命名")); update(); //更新窗口显示 } void CenterWidget::openDrawing(){ //打开绘图 fileName=QFileDialog::getOpenFileName(this, tr("打开文件对话框"),"/","绘图文件(*.draw);;所有文件(*.*)"); if(fileName==tr("")) return; //lines.clear(); //ellipses.clear(); //rects.clear(); for(auto shape:shapes) delete shape; shapes.clear(); beginDraw=false; isModified=false; openFile(fileName); //存储文件 parentWidget()->setWindowTitle(tr("简单绘图程序 ")+fileName); update(); } void CenterWidget::saveDrawing(){ if(fileName==tr("")){ fileName=QFileDialog::getSaveFileName(this,tr("保存文件对话框"),"/","绘图文件(*draw);;所有文件(*.*)"); if(fileName==tr("")) return ; } saveFile(fileName); parentWidget()->setWindowTitle(tr("绘图程序 ")+fileName); isModified=false; } void CenterWidget::saveFile(QString fileName){ //写文件操作 QFile file(fileName); if(file.open(QFile::WriteOnly|QFile::Truncate)){ QTextStream out(&file); /*out<<lines.length()<<endl; for(QLine line:lines) out<<line.p1().x()<<" "<<line.p1().y()<<" " <<line.p2().x()<<" "<<line.p2().y()<<endl; out<<ellipses.length()<<endl; for(QRect rect:ellipses) out<<rect.topLeft().x()<<" "<<rect.topLeft().y()<<" " <<rect.bottomRight().x() <<" "<<rect.bottomRight().y()<<endl; out<<rects.length()<<endl; for(QRect rect:rects) out<<rect.topLeft().x()<<" "<<rect.topLeft().y()<<" " <<rect.bottomRight().x() <<" "<<rect.bottomRight().y()<<endl;*/ out<<shapes.length()<<endl; for(auto shape:shapes) shape->save(out); file.close(); } } void CenterWidget::openFile(QString fileName){ //读文件操作 QFile file(fileName); if(file.open(QFile::ReadOnly)){ //只读方式打开 QTextStream in(&file); /*int lineNums; in>>lineNums; int x1,y1,x2,y2; for(int i=0;i<lineNums;++i){ in>>x1>>y1>>x2>>y2; lines.append(QLine(x1,y1,x2,y2)); } int ellipseNums; in>>ellipseNums; for(int i=0;i<ellipseNums;++i){ in>>x1>>y1>>x2>>y2; ellipses.append(QRect(QPoint(x1,y1),QPoint(x2,y2))); } int rectNums; in>>rectNums; for(int i=0;i<rectNums;++i){ in>>x1>>y1>>x2>>y2; rects.append(QRect(QPoint(x1,y1),QPoint(x2,y2))); }*/ int nums; in>>nums; int type; Shape *curShape; for(int i=0;i<nums;++i){ in>>type; switch(type){ case 0: curShape=Line::read(in); break; case 1: curShape=Ellipse::read(in); break; case 2: curShape=Rectangle::read(in); break; } shapes.append(curShape); } file.close(); } } CenterWidget::~CenterWidget(){ for(auto shape:shapes) delete shape; }
评论
    相关推荐