QT实验报告.docx

上传人:b****3 文档编号:4000542 上传时间:2022-11-27 格式:DOCX 页数:62 大小:350.87KB
下载 相关 举报
QT实验报告.docx_第1页
第1页 / 共62页
QT实验报告.docx_第2页
第2页 / 共62页
QT实验报告.docx_第3页
第3页 / 共62页
QT实验报告.docx_第4页
第4页 / 共62页
QT实验报告.docx_第5页
第5页 / 共62页
点击查看更多>>
下载资源
资源描述

QT实验报告.docx

《QT实验报告.docx》由会员分享,可在线阅读,更多相关《QT实验报告.docx(62页珍藏版)》请在冰豆网上搜索。

QT实验报告.docx

QT实验报告

面向对象程序设计2

实验报告

 

专业:

计算机科学与技术

年级:

2013级

班级:

工科1班

学号:

070613047

姓名:

黄剑波

 

实验一:

线程通信:

门铃

实验时间:

实验地点:

A603

一、实验目的

1.熟悉QT的多线程编程

2.学会使用QThread类创建线程

3.掌握使用QT事件进行跨线程通信的方法

二、实验内容

1.使用QThread类和Qt事件跨线程通信配合完成一个门铃实验。

2.门铃示例创建一个工作线程,随机发送门铃事件给GUI线程中的主人,事件中携带客人和礼物的信息,主人如果觉得客人和礼物都满意,就两眼放光,否则就假装不在。

三、实验要求

1.熟悉QT的多线程编程,了解Qt事件跨线程通信的方法

四、实验步骤

1.使用QT应用程序向导创建GUI工程

2.使用QTDesigner设计图形用户界面

拖曳控件

调整布局

设置属性

信号与槽映射

3.使用QTCreate编写代码

4.编译

5.构建

6.调试

五、实验源码

main.cpp

#include"doorbell.h"

#include

intmain(intargc,char*argv[])

{

QApplicationa(argc,argv);

Masterw;

w.show();

returna.exec();

}

doorbell.h

#ifndefDOORBELL_H

#defineDOORBELL_H

#include

#include

#include

#include

#include

#include

#include

classBellEvent:

publicQEvent

{

public:

BellEvent(constQString&visitor,constQStringList&gifts);

QStringm_visitor;

QStringListm_gifts;

staticTypeeventType();

protected:

staticTypem_evType;

};

classBellThread;

classQTimer;

classRinger:

publicQObject

{

Q_OBJECT

public:

Ringer(BellThread*t);

publicslots:

voidonTimeout();

private:

BellThread*m_thread;

};

classBellThread:

publicQThread

{

friendclassRinger;

public:

BellThread(QObject*receiver);

protected:

voidrun();

private:

QPointerm_receiver;

};

classMaster:

publicQWidget

{

Q_OBJECT

public:

Master(QWidget*parent=0);

~Master();

protected:

boolevent(QEvent*e);

private:

QLabel*m_visitorLabel;

QLabel*m_movie;

};

#endif//DOORBELL_H

doorbell.cpp

#include"doorbell.h"

#include

#include

#include

#include

#include

#include

QEvent:

:

TypeBellEvent:

:

m_evType=QEvent:

:

None;

BellEvent:

:

BellEvent(constQString&visitor,

constQStringList&gifts)

:

QEvent(eventType()),m_visitor(visitor)

m_gifts(gifts)

{

}

QEvent:

:

TypeBellEvent:

:

eventType()

{

if(m_evType==QEvent:

:

None)

{

m_evType=(QEvent:

:

Type)registerEventType();

}

returnm_evType;

}

//

BellThread:

:

BellThread(QObject*receiver)

:

m_receiver(receiver)

{

}

voidBellThread:

:

run()

{

qDebug()<<"BellThread,tid-"

<

:

currentThreadId();

QScopedPointerfirer(newRinger(this));

exec();

}

constchar*g_vistors[5]={

"Mary","Peter","John",

"Haali","Mike"

};

constchar*g_gifts[7]={

"apple","iPadmini2","banana",

"egg","iPhone6","Bambook",

"KindleFireHD"

};

Ringer:

:

Ringer(BellThread*t):

m_thread(t)

{

QTimer:

:

singleShot(3000,this,SLOT(onTimeout()));

}

voidRinger:

:

onTimeout()

{

if(m_thread->m_receiver.isNull())

{

m_thread->quit();

return;

}

qsrand(QDateTime:

:

currentDateTime().toTime_t());

QStringListgifts;

gifts<

gifts<

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

eye.gif

6、实验结果与分析

7、教师评价

实验二:

高阶多线程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

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

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"widget.h"

#include

#include

#include

#include

#include

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

六、实验结果与分析

七、教师评价

实验三:

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

intmain(intargc,char*argv[])

{

QApplicationa(argc,argv);

Widgetw;

w.show();

returna.exec();

}

widget.h

#ifndefWIDGET_H

#defineWIDGET_H

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

classWidget:

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

#include

#include

#include

#include

#include

#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","applicati

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1