




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年深海矿产资源勘探技术装备创新与市场潜力报告
- 2025年高中数学概率统计知识冲刺押题试卷
- 2025年房地产估价师考试房地产估价实务冲刺试卷
- 2025年考研英语(一)阅读理解技巧精讲 阅读理解高分策略
- 2025年公共营养师二级考试专项训练试卷及真题解析
- 现代化家具知识培训总结课件
- 2025年环境影响评价工程师考试案例分析试卷 环评报告专项强化
- 现代农业知识培训课件
- 测试工程师的岗位职责
- 新重庆市劳动合同范本
- GB/T 40073-2021潜水器金属耐压壳外压强度试验方法
- GB/T 3624-2010钛及钛合金无缝管
- GB/T 14153-1993硬质塑料落锤冲击试验方法通则
- (完整版)人教版八年级下册《道德与法治》期末测试卷及答案【新版】
- 维护新疆稳定 实现长治久安课件
- 北京大学人民医院-医疗知情同意书汇编
- 档案管理员述职报告9篇
- 舞台灯光基础知识教学课件
- 牙体牙髓病最全课件
- 脑卒中的功能锻炼课件
- 护理质控简报
评论
0/150
提交评论