C++实验报告QTPaintPad_第1页
C++实验报告QTPaintPad_第2页
C++实验报告QTPaintPad_第3页
C++实验报告QTPaintPad_第4页
C++实验报告QTPaintPad_第5页
已阅读5页,还剩22页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、面向对象程序设计与C+实验报告年级、专业、班级2012级物联网1班2012级信息安全2班2012级信息安全2班姓名陈点许佳丽鄢然实验题目基于QT的绘图板程序的设计与实现实验时间2013.112013.12.16实验地点DS1404实验成绩实验性质验证性 设计性 综合性教师评价:算法/实验过程正确; 源程序/实验内容提交 程序结构/实验步骤合理;实验结果正确; 语法、语义正确; 报告规范; 其他: 评价教师签名:一、实验目的学习和运用QT,开发GUI程序,实现绘图板功能。二、实验项目内容1、基于QT技术,设计并实现一个绘图板程序,该程序的功能类似于Windows操作系统中的画图程序;2、绘图板程

2、序是图形用户界面,需要添加菜单、按钮、工具栏等GUI组件。【本项目使用Qt5.1.1 请使用同版本的QtCreator打开.Pro文件】3、 实验过程或算法(源程序)【实验过程】(含全部DIY代码)2013.12.08及之前·对Qt的熟悉,查阅资料后,自学、开始编写。完成了绘图板基础功能的实现,主要是PaintArea及其拖动条的自适应以及信号槽的链接。2013.12.10·实现了显示自定义程序主窗口及小窗口图标。·实现了根据绘图工具栏是否存在执行显示/隐藏的单按钮多功能效果。代码如下:void MainWindow:on_action_11_triggered(

3、) /dockWidgetif(ui->dockWidget->isHidden() ui->dockWidget->show();else ui->dockWidget->hide();·实现了鼠标移动到图标显示包含当前图标代表功能注释的小方框/状态栏信息,具体效果如右,实现代码如下:/.hQAction *openAction;QLabel *msgLabel;/.cppmsgLabel = new QLabel;msgLabel->setMinimumSize(msgLabel->sizeHint();msgLabel->s

4、etAlignment(Qt:AlignHCenter);statusBar()->addWidget(msgLabel);2013.12.12·实现了Qt5兼容Qt4的代码重编译Qt4中的许多函数和库文件无法在Qt5中使用,所以Qt4中的PaintArea导入到Qt5出现了许多问题。在Pro中添加“Qt+= widgets”,再对所有包含的库文件重写(在Include文件夹里查找位置后替换)。以及Qcolor函数的改变:Qt:red更改为QColor(Qt:red),还要特别注意新的Qprintsupport库文件。如下:/QT += core gui wi

5、dgets/Source Code_Include#include "paintarea.h"#include <QPainter>#include <QtPrintSupportQPrintDialog>#include <QtPrintSupportQPrinter>·对“线颜色”、“填充色”进行图形化(边框&填充色)·对ComboBox函数进行重写(分为两个ComboBox来画不同的小方块)。代码如下:>> creatColorComboBoxL(ui->penColorComboBox

6、);>> creatColorComboBox(ui->brushColorComboBox);void MainWindow:creatColorComboBox(QComboBox *comboBox) QPixmap pix(16,16); QPainter painter(&pix); painter.fillRect(0,0,16,16,QColor(Qt:black); painter.fillRect(2,2,12,12,QColor(Qt:white); comboBox->addItem(QIcon(pix),tr("无")

7、,QColor(Qt:transparent); /Transparent is no filcolor painter.fillRect(2,2,12,12,QColor(Qt:black); comboBox->addItem(QIcon(pix),tr("黑色"),QColor(Qt:black); painter.fillRect(2,2,12,12,QColor(Qt:red); comboBox->addItem(QIcon(pix),tr("红色"),QColor(Qt:red); painter.fillRect(2,2,12

8、,12,QColor(Qt:green); comboBox->addItem(QIcon(pix),tr("绿色"),QColor(Qt:green); painter.fillRect(2,2,12,12,QColor(Qt:blue); comboBox->addItem(QIcon(pix),tr("蓝色"),QColor(Qt:blue); painter.fillRect(2,2,12,12,QColor(Qt:yellow); comboBox->addItem(QIcon(pix),tr("黄色"),

9、QColor(Qt:yellow); painter.fillRect(2,2,12,12,QColor(Qt:white); comboBox->addItem(QIcon(pix),tr("白色"),QColor(Qt:white);void MainWindow:creatColorComboBoxL(QComboBox *comboBox) QPixmap pix(16,16); QPainter painter(&pix); painter.fillRect(0,0,16,16,QColor(Qt:black); painter.fillRect(2

10、,2,12,12,QColor(Qt:white);/init comboBox->addItem(QIcon(pix),tr("黑色"),QColor(Qt:black); painter.fillRect(0,0,16,16,QColor(Qt:red); painter.fillRect(2,2,12,12,QColor(Qt:white);/init comboBox->addItem(QIcon(pix),tr("红色"),QColor(Qt:red); painter.fillRect(0,0,16,16,QColor(Qt:gr

11、een); painter.fillRect(2,2,12,12,QColor(Qt:white);/init comboBox->addItem(QIcon(pix),tr("绿色"),QColor(Qt:green); painter.fillRect(0,0,16,16,QColor(Qt:blue); painter.fillRect(2,2,12,12,QColor(Qt:white);/init comboBox->addItem(QIcon(pix),tr("蓝色"),QColor(Qt:blue); painter.fillR

12、ect(0,0,16,16,QColor(Qt:yellow); painter.fillRect(2,2,12,12,QColor(Qt:white);/init comboBox->addItem(QIcon(pix),tr("黄色"),QColor(Qt:yellow); painter.fillRect(0,0,16,16,QColor(Qt:white); painter.fillRect(2,2,12,12,QColor(Qt:white);/init comboBox->addItem(QIcon(pix),tr("白色"),Q

13、Color(Qt:white);·Ui界面设计,增加界面图标及菜单栏图标,图标及Ui_Designer截图如下: 2013.12.13·自学了制作Ui背景图的方法以及点阵图Pixmap的编码方式,并且完成了doNewDialog_UI的设计,界面如下:·新建了一个Qt设计类About_us,开始编写“关于我们”。2013.12.14·开始编写About_us 调用函数>>>void MainWindow:on_action_12_triggered() About_us abu; abu.exec();使用QMovie为QLabel赋值

14、gif图像,达成动画效果:(原创)About_us:About_us(QWidget *parent) : QDialog(parent), ui(new Ui:About_us) ui->setupUi(this); QMovie *moviebgd = new QMovie(":/new/prefix1/images/yr/2nd/bgd.gif"); ui->Giflabel->setMovie(moviebgd); QMovie *moviecd = new QMovie(":/new/prefix1/images/yr/2nd/dian

15、.gif"); ui->CDlabel->setMovie(moviecd); QMovie *moviekl = new QMovie(":/new/prefix1/images/yr/2nd/gali2.gif"); ui->KLlabel->setMovie(moviekl); QMovie *movieyr = new QMovie(":/new/prefix1/images/yr/2nd/baizi.jpg"); ui->YRlabel->setMovie(movieyr); moviebgd-&g

16、t;start(); moviecd->start(); moviekl->start(); movieyr->start();2013.12.16·编写实验报告【算法】·简单的迭代·面向对象思想【源程序】在此贴出核心模块的.cpp,其余详见附件源代码/doNewDialog.cpp#include "donewdialog.h"#include "ui_donewdialog.h"#include <QtWidgets>#include <QColorDialog>/edited

17、by Royalmiki 20125209DoNewDialog:DoNewDialog(QWidget *parent) : QDialog(parent), ui(new Ui:DoNewDialog) ui->setupUi(this);backColor = Qt:white;DoNewDialog:DoNewDialog() delete ui;void DoNewDialog:changeEvent(QEvent *e) QDialog:changeEvent(e); switch (e->type() case QEvent:LanguageChange: ui-&g

18、t;retranslateUi(this); break; default: break; int DoNewDialog:getWidth() return ui->widthSpinBox->text().toInt();int DoNewDialog:getHeight() return ui->heightSpinBox->text().toInt();QColor DoNewDialog:getBackColor() return backColor;void DoNewDialog:on_toolButton_clicked() QColor newColo

19、r = QColorDialog:getColor(); /Getcolor if (newColor.isValid() /Valid judgement backColor = newColor; QPalette palette = ui->textBrowser->palette(); /ShowColor palette.setColor(QPalette:Base,backColor); ui->textBrowser->setPalette(palette); update(); /mainwindow.cpp#include "mainwind

20、ow.h"#include "ui_mainwindow.h"#include "donewdialog.h"#include "about_us.h"#include <QMessageBox>#include <QFileDialog>#include <QPainter>#include <QColor>/Edited by okcd00 20125209MainWindow:MainWindow(QWidget *parent) : QMainWindow(parent

21、), ui(new Ui:MainWindow) ui->setupUi(this); resize(700,500); /init area = new PaintArea; scrollArea = new QScrollArea;msgLabel = new QLabel; scrollArea->setBackgroundRole(QPalette:Dark); /Background Color set scrollArea->setWidget(area); /Add paintarea scrollArea->widget()->setMinimum

22、Size(800,600); /init setCentralWidget(scrollArea); /Set Central isSaved = false; curFile = tr("未命名.png"); creatColorComboBoxL(ui->penColorComboBox); /declare of ComboLine creatColorComboBox(ui->brushColorComboBox); /declare of ComboFillmsgLabel->setMinimumSize(msgLabel->sizeHin

23、t();msgLabel->setAlignment(Qt:AlignHCenter);statusBar()->addWidget(msgLabel);MainWindow:MainWindow() delete ui;void MainWindow:changeEvent(QEvent *e) QMainWindow:changeEvent(e); switch (e->type() case QEvent:LanguageChange: ui->retranslateUi(this); break; default: break; void MainWindow:

24、doOpen() if (maybeSave() QString fileName = QFileDialog:getOpenFileName(this, tr("打开文件"), QDir:currentPath(); if (!fileName.isEmpty() area->openImage(fileName); scrollArea->widget()->resize(area->getImageSize(); /Get&Set ScrollArea Size isSaved = true; curFile = fileName; v

25、oid MainWindow:doNew() if(maybeSave() DoNewDialog dlg; if(dlg.exec() = QDialog:Accepted) int width = dlg.getWidth(); int height = dlg.getHeight(); area->setImageSize(width,height); scrollArea->widget()->resize(width,height); area->setImageColor(dlg.getBackColor(); isSaved = false; bool M

26、ainWindow:maybeSave() if(area->isModified() QMessageBox:StandardButton box; box = QMessageBox:warning(this,tr("保存文件"),tr("图片没有保存哦要保存嘛?"), QMessageBox:Yes|QMessageBox:No|QMessageBox:Cancel); if(box = QMessageBox:Yes) return doFileSave(); else if(box = QMessageBox:Cancel) return

27、 false; return true;bool MainWindow:doFileSave() if(isSaved) return saveFile(curFile); else return doFileSaveAs();bool MainWindow:saveFile(QString fileName) if(area->saveImage(fileName,"png") /png is common isSaved = true; return true; else return false;bool MainWindow:doFileSaveAs() QS

28、tring fileName = QFileDialog:getSaveFileName(this,tr("另存为"),curFile); if(fileName.isEmpty() return false; else return saveFile(fileName);void MainWindow:on_action_N_triggered() /New doNew();void MainWindow:on_action_O_triggered() /Open doOpen();void MainWindow:on_action_S_triggered() /Save

29、 doFileSave();void MainWindow:on_action_A_triggered() /Save as doFileSaveAs();void MainWindow:on_action_X_triggered() /Exit if(maybeSave() qApp->quit();void MainWindow:closeEvent(QCloseEvent *event) /Close if(maybeSave() qApp->quit(); else event->ignore();/*void MainWindow:on_action_Undo_tr

30、iggered() /Undo/ area->undo();/ scrollArea->widget()->removeAction();*/ /Failedvoid MainWindow:on_action_4_triggered() /Zoom in area->zoomIn(); scrollArea->widget()->resize(area->getImageSize(); /Get&Set scrollArea Sizevoid MainWindow:on_action_5_triggered() /zoom out area-&

31、gt;zoomOut();void MainWindow:on_action_6_triggered() /Change to orisize area->zoom_1();void MainWindow:on_action_7_triggered() /Roll area->doRotate();void MainWindow:on_action_8_triggered() /Shear area->doShear();void MainWindow:on_action_10_triggered() /Clear area->doClear();void MainWi

32、ndow:on_action_11_triggered() /dockWidgetif(ui->dockWidget->isHidden() ui->dockWidget->show();else ui->dockWidget->hide();void MainWindow:creatColorComboBox(QComboBox *comboBox) QPixmap pix(16,16); QPainter painter(&pix); painter.fillRect(0,0,16,16,QColor(Qt:black); painter.fil

33、lRect(2,2,12,12,QColor(Qt:white); comboBox->addItem(QIcon(pix),tr("无"),QColor(Qt:transparent); /Transparent is no filcolor painter.fillRect(2,2,12,12,QColor(Qt:black); comboBox->addItem(QIcon(pix),tr("黑色"),QColor(Qt:black); painter.fillRect(2,2,12,12,QColor(Qt:red); comboBo

34、x->addItem(QIcon(pix),tr("红色"),QColor(Qt:red); painter.fillRect(2,2,12,12,QColor(Qt:green); comboBox->addItem(QIcon(pix),tr("绿色"),QColor(Qt:green); painter.fillRect(2,2,12,12,QColor(Qt:blue); comboBox->addItem(QIcon(pix),tr("蓝色"),QColor(Qt:blue); painter.fillRe

35、ct(2,2,12,12,QColor(Qt:yellow); comboBox->addItem(QIcon(pix),tr("黄色"),QColor(Qt:yellow); painter.fillRect(2,2,12,12,QColor(Qt:white); comboBox->addItem(QIcon(pix),tr("白色"),QColor(Qt:white);void MainWindow:creatColorComboBoxL(QComboBox *comboBox) QPixmap pix(16,16); QPainter

36、 painter(&pix); painter.fillRect(0,0,16,16,QColor(Qt:black); painter.fillRect(2,2,12,12,QColor(Qt:white);/init comboBox->addItem(QIcon(pix),tr("黑色"),QColor(Qt:black); painter.fillRect(0,0,16,16,QColor(Qt:red); painter.fillRect(2,2,12,12,QColor(Qt:white);/init comboBox->addItem(QI

37、con(pix),tr("红色"),QColor(Qt:black); painter.fillRect(0,0,16,16,QColor(Qt:green); painter.fillRect(2,2,12,12,QColor(Qt:white);/init comboBox->addItem(QIcon(pix),tr("绿色"),QColor(Qt:green); painter.fillRect(0,0,16,16,QColor(Qt:blue); painter.fillRect(2,2,12,12,QColor(Qt:white);/i

38、nit comboBox->addItem(QIcon(pix),tr("蓝色"),QColor(Qt:blue); painter.fillRect(0,0,16,16,QColor(Qt:yellow); painter.fillRect(2,2,12,12,QColor(Qt:white);/init comboBox->addItem(QIcon(pix),tr("黄色"),QColor(Qt:yellow); painter.fillRect(0,0,16,16,QColor(Qt:white); painter.fillRect(

39、2,2,12,12,QColor(Qt:white);/init comboBox->addItem(QIcon(pix),tr("白色"),QColor(Qt:white);void MainWindow:on_shapeComboBox_currentIndexChanged(QString shape) /Shape_Chosen if(shape = tr("无") area->setShape(PaintArea:None); else if(shape = tr("矩形") area->setShape(

40、PaintArea:Rectangle); else if(shape = tr("直线") area->setShape(PaintArea:Line); else if(shape = tr("椭圆") area->setShape(PaintArea:Ellipse);void MainWindow:on_penStyleComboBox_currentIndexChanged(QString style) /Line_Style if(style = tr("实线") area->setPenStyle(Qt

41、:SolidLine); else if(style = tr("点线") area->setPenStyle(Qt:DotLine); void MainWindow:on_penWidthSpinBox_valueChanged(int width) /Width of Line area->setPenWidth(width);void MainWindow:on_penColorComboBox_currentIndexChanged(int index) /Color of Line QColor color = ui->penColorComb

42、oBox->itemData(index,Qt:UserRole).value<QColor>(); area->setPenColor(color);void MainWindow:on_brushColorComboBox_currentIndexChanged(int index) /Color of Filling QColor color = ui->brushColorComboBox->itemData(index,Qt:UserRole).value<QColor>(); area->setBrushColor(color)

43、;void MainWindow:on_action_12_triggered() About_us abu; abu.exec();/PaintArea.cpp 无可自由发挥之处,不断调用函数即可,就不贴在这了。/About_us.cpp 【DIY】纯自己发挥的功能实现#include "about_us.h"#include "ui_about_us.h"#include <QLabel>#include <QMovie>/Edited by CD 20125209About_us:About_us(QWidget *pare

44、nt) : QDialog(parent), ui(new Ui:About_us) ui->setupUi(this); QMovie *moviebgd = new QMovie(":/new/prefix1/images/yr/2nd/bgd.gif"); ui->Giflabel->setMovie(moviebgd); QMovie *moviecd = new QMovie(":/new/prefix1/images/yr/2nd/dian.gif"); ui->CDlabel->setMovie(moviecd

45、); QMovie *moviekl = new QMovie(":/new/prefix1/images/yr/2nd/gali2.gif"); ui->KLlabel->setMovie(moviekl); QMovie *movieyr = new QMovie(":/new/prefix1/images/yr/2nd/baizi.jpg"); ui->YRlabel->setMovie(movieyr); moviebgd->start(); moviecd->start(); moviekl->star

46、t(); movieyr->start();/main.cpp 自动生成,无需编码4、 实验结果及分析和(或)源程序调试过程【实验结果&成果展示】0)主界面总览:1)各个ComboBox的内容:2)菜单栏内容:3)可浮动、可由按钮呼出隐藏的“画图工具栏”4)新建以及退出时的友好提示:5)新建画板设置窗口:6)About_us(关于我们) 动画窗口:7)放大缩小还原大小 功能:8)拉伸 功能:9)翻转 功能:10)保存 功能:11)清空 功能:12.1)打开图片 功能:12.2)打开图片 效果:【调试过程】1、无法用"UTF-8"编码用记事本打开,另存为,更改编

47、码为UTF-8,保存原位置,覆盖确定。2、No such file or directory在P里增加行:QT += widgets3、'setCodecForTr' is not a member of 'QTextCodec'Reason:QT5中删除了此项函数。相见CSDN-Blog:QTextCodec中的setCodecForTr等终于消失了 (Qt5):Qt5与中文: 4、QT5茫茫多的奇葩问题切换到QT4,原来QT和QtCreator的版本是不同的各种失败之后,QtCreator2.6.0为QT4.8.4,Installing.5、卸载程序时删除尽了当前文件夹,全文件丢失,上网也找不到了Failed6、重下载5.1.1,回归原始问题上网查询发现No such file or directory的另一解决方案及原理:Qt5的命名空间更改了,有许多原来在gui、core里的头文件被迁移到了widgets里,所以进行更改#include <QtWidgetsQMainWindow>#include <QtWidgetsQScrollArea>#include <QtWidgetsQComboBox>#in

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论