安庆师范学院计算机与信息学院
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
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
第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:
QFutureWatcher
#endif//WIDGET_H
widget.cpp
#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(\); QFuture
QtConcurrent::run(loadImage,uri); m_watcher.setFuture(future); }
voidWidget::onFinished() {
m_image=m_watcher.future().result(); if(!m_image.isNull()) {
repaint(); } }
imageloader.qrc
第11页共61页
面向对象程序设计:实验报告
安庆师范学院计算机与信息学院
Anqing teacher school College of computer and information
六、实验结果与分析
第12页共61页
面向对象程序设计:实验报告