QT实验报告 - 图文(3)

2019-08-31 12:41

安庆师范学院计算机与信息学院

Anqing teacher school College of computer and information

实验二:高阶多线程QtConcurrent的使用:

ImageLoader

实验时间:实验地点: A603

一、实验目的

1.了解高阶多线程QtConcurrent的意义 2.掌握使用高阶多线程QtConcurrent的方法

二、实验内容

1.Widget类重写paintEvent(),用来绘制QtConcurrent加载的图片

三、实验要求

1.开启应用程序后加载图片并显示

四、实验步骤

1.使用QT应用程序向导创建GUI工程 2.使用QT Designer设计图形用户界面 ? 拖曳控件 ? 调整布局 ? 设置属性 ? 信号与槽映射 3.使用QT Create编写代码

第8页共61页

面向对象程序设计:实验报告

安庆师范学院计算机与信息学院

Anqing teacher school College of computer and information

4.编译 5.构建 6.调试

五、实验源码

main.cpp

#include\

#include #include

intmain(intargc,char*argv[]) {

QApplicationa(argc,argv); QFontf=a.font(); f.setPixelSize(60); a.setFont(f); Widgetw; w.show();

returna.exec(); }

widget.h

#ifndefWIDGET_H #defineWIDGET_H

#include #include #include

#include

第9页共61页

面向对象程序设计:实验报告

安庆师范学院计算机与信息学院

Anqing teacher school College of computer and information

classWidget:publicQWidget {

Q_OBJECT

public:

Widget(QWidget*parent=0); //~Widget();

protected:

voidpaintEvent(QPaintEvent*e);

protectedslots: voidonLoad();

voidonFinished();

private:

QFutureWatcherm_watcher; QImagem_image; };

#endif//WIDGET_H

widget.cpp

#include\

#include #include

#include #include #include

QImageloadImage(constQString&uri) {

QImageimage(uri); returnimage; }

Widget::Widget(QWidget*parent) :QWidget(parent) ,m_watcher(this)

第10页共61页

面向对象程序设计:实验报告

安庆师范学院计算机与信息学院

Anqing teacher school College of computer and information

{

connect(&m_watcher,SIGNAL(finished()), this,SLOT(onFinished()));

QTimer::singleShot(5000,this,SLOT(onLoad())); }

voidWidget::paintEvent(QPaintEvent*e) {

QPainterpainter(this); if(m_image.isNull()) {

painter.drawText(rect(),Qt::AlignCenter,\); } else {

painter.drawImage(rect(),m_image); } }

voidWidget::onLoad() {

QStringuri(\); QFuturefuture=

QtConcurrent::run(loadImage,uri); m_watcher.setFuture(future); }

voidWidget::onFinished() {

m_image=m_watcher.future().result(); if(!m_image.isNull()) {

repaint(); } }

imageloader.qrc

airplay.gif

第11页共61页

面向对象程序设计:实验报告

安庆师范学院计算机与信息学院

Anqing teacher school College of computer and information

六、实验结果与分析

第12页共61页

面向对象程序设计:实验报告


QT实验报告 - 图文(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:劳动法期中作业答案(2)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: