資源簡介
6.1 演示案例
【任務目標】 實現簡單的繪圖功能,并支持繪圖數據的保存和打開等功能。
通過 Draw 菜單,由用戶點擊選擇不同的圖形,按下鼠標左鍵并拖動鼠標開始繪
圖,松開鼠標后保存圖形(線條、橢圓、矩形)數據,并更新屏幕顯示。
File 菜單下定義文件的基本操作, New 菜單項用于新建繪圖文件,如果上一
個文件修改了并沒有保存,需要進行提示, Save 用于將繪圖結果保存為磁盤文
件,實現持久化存儲, Open 用于打開存盤的圖形文件。
6.1.1 簡單繪圖版本

代碼片段和文件信息
#include?“centerwidget.h“
#include
#include
#include?“mainwindow.h“
#include
#include
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(500400);
????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(p1p2);?break;
????????case?1:?p.drawEllipse(QRect(p1p2));?break;
????????case?2:?p.drawRect(QRect(p1p2));?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(p1p2c);?break;
????case?1:?temp=new?Ellipse(p1p2c);?break;
????case?2:?temp=new?Rectangle(p1p2c);?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(p1p2));break;
????//case?1:ellipses.append(QRect(p1p2));break;
????//case?2:rects.append(QRect(p1p2));break;}
????Shape?*shape=nullptr;
????switch(drawType){
????case?0:?shape=new?Line(p1p2);?break;
????case?1:?shape=new?Ellipse(p1p2);?break;
????case?2:?shape=new?Rectangle(p1p2);?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();
??
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????6444??2017-12-27?19:48??Draw6\centerwidget.cpp
?????文件???????1535??2017-12-27?11:49??Draw6\centerwidget.h
?????文件????????551??2017-12-28?01:26??Draw6\Draw6.pro
?????文件??????17979??2017-12-28?08:43??Draw6\Draw6.pro.user
?????文件????????655??2017-12-27?10:47??Draw6\ellipse.cpp
?????文件????????409??2017-12-27?10:47??Draw6\ellipse.h
?????文件????????591??2017-12-27?10:47??Draw6\line.cpp
?????文件????????396??2017-12-27?10:19??Draw6\line.h
?????文件????????238??2017-12-27?09:05??Draw6\main.cpp
?????文件???????6397??2017-12-27?20:05??Draw6\mainwindow.cpp
?????文件????????983??2017-12-27?11:32??Draw6\mainwindow.h
?????文件??????15318??2017-12-28?00:55??Draw6\Q7XRX7XOK)BVG%6J)HV7EGE.png
?????文件????????670??2017-12-27?10:47??Draw6\rectangle.cpp
?????文件????????419??2017-12-27?10:47??Draw6\rectangle.h
?????文件????????165??2017-12-27?10:19??Draw6\shape.cpp
?????文件????????336??2017-12-27?10:19??Draw6\shape.h
?????目錄??????????0??2017-12-28?08:43??Draw6
-----------?---------??----------?-----??----
????????????????53086????????????????????17
- 上一篇:windows資源管理系統 自啟工具
- 下一篇:WinEdt 6.0 注冊機
評論
共有 條評論