gifts<〈g_gifts[qrand()%7];
BellEvent*ev=newBellEvent(
g_vistors[qrand()%5],gifts);
QCoreApplication:
:
postEvent(
m_thread-〉m_receiver,ev);
QTimer:
:
singleShot((qrand()%8+2)*1000
this,SLOT(onTimeout()));
}
Master:
:
Master(QWidget*parent)
:
QWidget(parent),m_visitorLabel(newQLabel)
m_movie(newQLabel)
{
BellThread*t=newBellThread(this);
connect(t,SIGNAL(finished()),t,SLOT(deleteLater()));
t—>start();
QVBoxLayout*layout=newQVBoxLayout;
layout—〉addWidget(m_visitorLabel);
QMovie*gif=newQMovie(":
/eye。
gif");
m_movie->setMovie(gif);
layout->addWidget(m_movie);
m_movie-〉setVisible(false);
layout-〉addStretch
(1);
setLayout(layout);
}
Master:
:
~Master()
{
}
boolMaster:
:
event(QEvent*e)
{
if(e—〉type()==BellEvent:
:
eventType())
{
BellEvent*ev=(BellEvent*)e;
QStringstr=QString(”%1,gitfs[%2]”)
.arg(ev—〉m_visitor)
.arg(ev—>m_gifts。
join(”;"));
m_visitorLabel-〉setText(str);
if(ev-〉m_gifts。
contains("iPhone6”)
||ev-〉m_gifts.contains(”KindleFireHD"))
{
m_movie—〉setVisible(true);
m_movie—〉movie()—〉start();
}
else
{
m_movie->setVisible(false);
m_movie—>movie()—〉stop();
}
returntrue;
}
returnQWidget:
:
event(e);
}
doorbell。
qrc
〈RCC>
〈file〉eye.gif〈/file〉
〈/RCC>
六、实验结果与分析
七、教师评价
实验二:
高阶多线程QtConcurrent的使用:
ImageLoader
实验时间:
实验地点:
A603
一、实验目的
1。
了解高阶多线程QtConcurrent的意义
2。
掌握使用高阶多线程QtConcurrent的方法
二、实验内容
1。
Widget类重写paintEvent(),用来绘制QtConcurrent加载的图片
三、实验要求
1.开启应用程序后加载图片并显示
四、实验步骤
1.使用QT应用程序向导创建GUI工程
2。
使用QTDesigner设计图形用户界面
Ø拖曳控件
Ø调整布局
Ø设置属性
Ø信号与槽映射
3。
使用QTCreate编写代码
4.编译
5。
构建
6.调试
五、实验源码
main。
cpp
#include”widget。
h”
#include
#include〈QFont>
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〈QImage〉
#include
#include〈QFutureWatcher>
classWidget:
publicQWidget
{
Q_OBJECT
public:
Widget(QWidget*parent=0);
//~Widget();
protected:
voidpaintEvent(QPaintEvent*e);
protectedslots:
voidonLoad();
voidonFinished();
private:
QFutureWatcher〈QImage〉m_watcher;
QImagem_image;
};
#endif//WIDGET_H
widget。
cpp
#include"widget.h”
#include
#include〈QFuture>
#include
#include〈QPainter〉
#include〈QTimer〉
QImageloadImage(constQString&uri)
{
QImageimage(uri);
returnimage;
}
Widget:
:
Widget(QWidget*parent)
:
QWidget(parent)
m_watcher(this)
{
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,"Loading。
。
。
");
}
else
{
painter.drawImage(rect(),m_image);
}
}
voidWidget:
:
onLoad()
{
QStringuri(":
/airplay.gif”);
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〈/file>
〈/qresource〉
〈/RCC>
六、实验结果与分析
七、教师评价
实验三:
HTTP编程:
httpTestTool
实验时间:
实验地点:
A603
一、实验目的
1。
了解Qt使用QNetworkAccessManager进行http编程的基本方法
2.熟悉用QNetworkAccessManager进行http编程的注意事项
3。
熟悉QNetworkAccessManager、QNetworkRequest、QNetworkReply和QUrl这四个类,以及它们的相互关系
二、实验内容
1.使用QNetworkAccessManager、QNetworkRequest、QNetworkReply和QUrl这四个类创建一个http访问器
三、实验要求
1.实现HEAD、GET、POST、DELETE四个方法
四、实验步骤
1。
使用QT应用程序向导创建GUI工程
2.使用QTDesigner设计图形用户界面
Ø拖曳控件
Ø调整布局
Ø设置属性
Ø信号与槽映射
3。
使用QTCreate编写代码
4。
编译
5。
构建
6。
调试
五、实验源码
main。
cpp
#include”widget。
h”
#include〈QApplication>
intmain(intargc,char*argv[])
{
QApplicationa(argc,argv);
Widgetw;
w。
show();
returna.exec();
}
widget.h
#ifndefWIDGET_H
#defineWIDGET_H
#include
#include〈QLineEdit〉
#include
#include
#include〈QLabel〉
#include
#include#include〈QCheckBox>
#include
#include〈QNetworkAccessManager〉
#include〈QNetworkReply>
#include#includeclassWidget:
publicQWidget
{
Q_OBJECT
public:
Widget(QWidget*parent=0);
~Widget();
protectedslots:
voidonAddHttpHeaderValue();
voidonStart();
voidonMethodChanged(constQString&method);
voidonMetaDataChanged();
voidonDownloadProgress(qint64bytesReceived,qint64bytesTotal);
voidonUploadProgress(qint64bytesSent,qint64bytesTotal);
voidonReadyRead();
voidonFinished();
protected:
voidsetupGUI();
voiddisableControls();
voidenableControls();
private:
QStringrequestString(QNetworkRequest*req);
QStringresponseString(QNetworkReply*reply);
voidresetState();
boolopenFile(boolsave,QString&errorString);
protected:
QGroupBox*m_headerValueGroup;
QLabel*m_headerKeyLabel;
QLineEdit*m_headerKeyEdit;
QLabel*m_headerValueLabel;
QLineEdit*m_headerValueEdit;
QPushButton*m_addHttpHeaderValueButton;
QGroupBox*m_stateGroup;
QTextEdit*m_stateText;
QProgressBar*m_progress;
QGroupBox*m_paramGroup;
QLabel*m_urlLabel;
QLineEdit*m_urlEdit;
QLabel*m_methodLabel;
QComboBox*m_methodCombo;
QPushButton*m_startButton;
QNetworkAccessManagerm_nam;
QNetworkReply*m_reply;
QNetworkRequest*m_request;
QStringm_strFilePath;
QFile*m_file;
};
#endif//WIDGET_H
widget.cpp
#include"widget。
h”
#include〈QGridLayout>
#include〈QVBoxLayout〉
#include
#include#include〈QByteArray〉
#include〈QFileDialog>
#include
staticconstchar*g_ext_to_content_type[]={
"。
001","application/x-001”
”。
301”,"application/x—301”
".323","text/h323”
,”.906","application/x—906”
,”.907”,”drawing/907”
".a11","application/x-a11”
,”.acp","audio/x-mei—aac”
,".ai”,"application/postscript"
”.aif","audio/aiff”
,”。
aifc","audio/aiff"
,”.aiff”,”audio/aiff"
".anv”,”application/x-anv”
,”。
asa",”text/asa”
”.asf",”video/x-ms-asf"
”。
asp”,"text/asp”
,”。
asx”,"video/x-ms-asf”
”.au",”audio/basic"
,”.avi”,”video/avi”
"。
awf",”application/vnd。
adobe.workflow"
,"。
biz","text/xml"
,".bmp”,”application/x—bmp"
”.bot”,”application/x-bot”
,".c4t",”application/x—c4t"
,".c90",”application/x-c90”
”.cal”,"application/x—cals"
,".cat",”application/vnd。
ms-pki。
seccat"
”。
cdf”,"application/x-netcdf"
,”.cdr”,”application/x-cdr”
”。
cel",”application/x-cel”
,”。
cer”,”application/x-x509-ca-cert”
”.cg4","application/x-g4"
,”。
cgm",”application/x-cgm"
,”。
cit”,”application/x—cit"
"。
class”,”java/*"
,"。
cml",”text/xml”
".cmp",”application/x—cmp”
".cmx”,"application/x—cmx"
".cot”,”application/x—cot"
”。
conf",”text/plain”
"。
crl”,"application/pkix-crl”
”.crt”,"a