当前位置: 首页 > news >正文

广东省住房和建设网站东莞有哪些大公司

广东省住房和建设网站,东莞有哪些大公司,小企业网站怎么做,网站开发入什么科目一个应用程序一般只有一个线程#xff0c;一个线程内的操作是顺序执行的#xff0c;如果有某个比较消耗时间的计算或操作#xff0c;比如网络通信中的文件传输#xff0c;在一个线程内操作时#xff0c;用户界面就可能会冻结而不能及时响应。这种情况下#xff0c;可以创…一个应用程序一般只有一个线程一个线程内的操作是顺序执行的如果有某个比较消耗时间的计算或操作比如网络通信中的文件传输在一个线程内操作时用户界面就可能会冻结而不能及时响应。这种情况下可以创建一个单独的线程来执行比较消耗时间的操作并与主线程之间处理好同步与数据交互这就是多线程应用程序。 Qt 为多线程操作提供了完整的支持。QThread 是线程类是实现多线程操作的核心类一般从QThread 继承定义自己的线程类。 线程之间的同步是其交互的主要问题Qt 提供了 QMutex、QMutexLocker、QReadWriteLock、QwaitCondition、QSemaphore 等多种类用于实现线程之间的同步。 Qt 还有 Qt Concurrent 模块提供一些高级的 API 实现多线程编程而无需使用 QMutex、QwaitCondition 和QSemaphore 等基础操作。使用Qt Concurrent 实现的多线程程序可以自动根据处理器内核个数调整线程个数。 文章目录 1. QThread 类功能简介2. 掷骰子的线程QDiceThread3. 掷骰子的多线程应用程序3.1 可视化UI设计3.2 代码分析3.3 程序结构及源码3.3.1 程序结构3.3.2 qdicethread.h3.3.3 qdicethread.cpp3.3.4 dialog.h3.3.5 dialog.cpp 1. QThread 类功能简介 QThread会起一个子线程并可以通过信号槽将变量传递到主线程中。 QThread 类提供不依赖于平台的管理线程的方法。一个 QThread 类的对象管理一个线程一般从QThread 继承一个自定义类并重定义虚函数run()在run()函数里实现线程需要完成的任务。 将应用程序的线程称为主线程额外创建的线程称为工作线程。一般在主线程里创建工作线程并调用 start()开始执行工作线程的任务。start()会在内部调用 run()函数进入工作线程的事件循环在 run()函数里调用 exit()或 quit()可以结束线程的事件循环或在主线程里调用 terminate()强制结束线程。 QThread 类的主要接口函数、信号和槽函数见下表。 QThread 是 QObject 的子类所以可以使用信号与槽机制。QThread 自身定义了 started()和finished()两个信号started()信号在线程开始执行之前发射也就是在 run()函数被调用之前finished0信号在线程就要结束时发射。 在进行本章的学习前建议先复习C新特性中对应部分Qt这里就是对C11的语法进行封装。 2. 掷骰子的线程QDiceThread 作为实例定义一个掷骰子的线程类QDiceThread类的声明部分如下 #ifndef QDICETHREAD_H #define QDICETHREAD_H#include QThreadclass QDiceThread : public QThread {Q_OBJECT private:int m_seq0;//掷骰子次数序号int m_diceValue;//骰子点数bool m_Pausedtrue; //掷一次骰子bool m_stopfalse; //停止线程 protected:void run() Q_DECL_OVERRIDE; //线程任务 public:QDiceThread();void diceBegin();//掷一次骰子void dicePause();//暂停void stopThread(); //结束线程 signals:void newValue(int seq,int diceValue); //产生新点数的信号 };#endif // QDICETHREAD_H重载虚函数 run()在此函数里完成线程的主要任务。 自定义 diceBegin()、dicePause()、stopThread()3 个公共函数用于线程控制这3 个函数由主线程调用。 定义了一个信号 newValue(int seq,int diceValue) 用于在掷一次子得到新的点数之后发射此信号由主线程的槽函数响应以获取值。 QDiceThread 类的实现代码如下: #include qdicethread.h #include QTimeQDiceThread::QDiceThread() {}void QDiceThread::diceBegin() { //开始掷骰子m_Pausedfalse; }void QDiceThread::dicePause() {//暂停掷骰子m_Pausedtrue; }void QDiceThread::stopThread() {//停止线程m_stoptrue; }void QDiceThread::run() {//线程任务m_stopfalse;//启动线程时令m_stopfalsem_seq0; //掷骰子次数qsrand(QTime::currentTime().msec());//随机数初始化qsrand是线程安全的while(!m_stop)//循环主体{if (!m_Paused){m_diceValueqrand(); //获取随机数m_diceValue(m_diceValue % 6)1;m_seq;emit newValue(m_seq,m_diceValue); //发射信号}msleep(500); //线程休眠500ms}// 在 m_stoptrue时结束线程任务quit();//相当于 exit(0),退出线程的事件循环 }其中run()是线程任务的实现部分线程开始就执行 run()函数。run()函数一般是事件循环过程根据各种条件或事件处理各种任务。当run()函数退出时线程的事件循环就结束了。 在run()函数里初始化变量 m_stop 和m_seq用qsrand()函数对随机数种子初始化。run()函数的主循环体是一个 while循环在主线程调用 stopThread()函数使 m_stop 为 true,才会退出 while循环调用quit()之后结束线程。 在 while 循环体内又根据 m_Paused 判断当前是否需要掷子如果需要掷骰子则用随机函数生成一次子的点数 m_diceValue然后发射信号 newValue()将 m seq和m diceValue作为信号参数传递出去。主线程可以设计槽函数与此信号关联获取这两个值并进行显示。 3. 掷骰子的多线程应用程序 使用QDiceThread 类设计一个应用程序 samp13_1程序运行界面如下图所示。 窗体上方的几个按钮用于控制线程的启动与停止控制开始与暂停掷骰子。中间的文本框显示次数和点数右边根据点数显示资源文件里面的一个图片图片存储在项目的资源文件里。下方的一个标签根据QDiceThread 的 started()和finished()两个信号显示线程的状态。 3.1 可视化UI设计 3.2 代码分析 窗口类是从QDialog 继承的类 Dialog其类定义如下(省略了按钮槽函数的定义): #ifndef DIALOG_H #define DIALOG_H#include QDialog#include qdicethread.hnamespace Ui { class Dialog; }class Dialog : public QDialog {Q_OBJECTprivate:QDiceThread threadA;protected:void closeEvent(QCloseEvent *event);public:explicit Dialog(QWidget *parent 0);~Dialog();private slots: ...private:Ui::Dialog *ui; };#endif // DIALOG_H 这里定义了一个QDiceThread 类型的变量 threadA重定义了 closeEvent()事件自定义了3个槽函数。 Dialog类的构造函数代码如下: Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) {//构造函数ui-setupUi(this);connect(threadA,SIGNAL(started()),this,SLOT(onthreadA_started()));connect(threadA,SIGNAL(finished()),this,SLOT(onthreadA_finished()));connect(threadA,SIGNAL(newValue(int,int)),this,SLOT(onthreadA_newValue(int,int))); }构造函数主要是将 threadA 的 3 个信号与 Dialog 自定义的3 个槽函数相关联这3 个槽函数的代码如下: void Dialog::onthreadA_started() {//线程的started()信号的响应槽函数ui-LabA-setText(Thread状态thread started); }void Dialog::onthreadA_finished() {//线程的 finished()信号的响应槽函数ui-LabA-setText(Thread状态thread finished); }void Dialog::onthreadA_newValue(int seq,int diceValue) {//QDiceThread的newValue()信号的响应槽函数显示骰子次数和点数QString strQString::asprintf(第 %d 次掷骰子点数为%d,seq,diceValue);ui-plainTextEdit-appendPlainText(str);QPixmap pic; //图片显示QString filenameQString::asprintf(:/dice/images/d%d.jpg,diceValue);pic.load(filename);ui-LabPic-setPixmap(pic); }started()信号发射时表示线程开始执行在标签里显示状态文字。 finished()信号发射时表示线程结束执行在标签里显示状态文字。 newValue()是 QDiceThread 定义的信号在掷一次骰子获得新的点数后发射将掷假子的次数和点数传递过来。槽函数onthreadA_newValue()获取这两个值并显示在文本框里再根据点数从资源文件里获取相应的图片并显示。 窗口上5个按钮的代码如下 void Dialog::on_btnStartThread_clicked() {//启动线程 按钮threadA.start();ui-btnStartThread-setEnabled(false);ui-btnStopThread-setEnabled(true);ui-btnDiceBegin-setEnabled(true);ui-btnDiceEnd-setEnabled(false); }void Dialog::on_btnDiceBegin_clicked() {//开始 掷骰子按钮threadA.diceBegin();ui-btnDiceBegin-setEnabled(false);ui-btnDiceEnd-setEnabled(true); }void Dialog::on_btnDiceEnd_clicked() {//暂停 掷骰子按钮threadA.dicePause();ui-btnDiceBegin-setEnabled(true);ui-btnDiceEnd-setEnabled(false); }void Dialog::on_btnStopThread_clicked() {//结束线程 按钮threadA.stopThread();//结束线程的run()函数执行threadA.wait();//ui-btnStartThread-setEnabled(true);ui-btnStopThread-setEnabled(false);ui-btnDiceBegin-setEnabled(false);ui-btnDiceEnd-setEnabled(false); }void Dialog::on_btnClear_clicked() { //清空文本 按钮ui-plainTextEdit-clear(); } “启动线程”按钮调用线程的 start()函数start()函数会内部调用 run()函数开始线程任务的执行。run()函数将内部变量 m_Paused 初始化为true所以启动线程后并不会立即开始掷散子。 “开始”按钮调用 diceBegin()函数使 threadA 线程内部变量 m_Paused 变为 false那么run()函数里就开始每隔 500 毫秒产生一次骰子点数并发射信号 newValue()。 “暂停”按钮调用 dicePause()函数使 threadA 线程内部变量 m_Paused 变为 truerun()函数里不再掷骰子但是 run()函数并没有结束也就是线程并没有结束。 “结束线程”按钮调用 stopThread()函数使threadA 线程内部的 m_stop 变为 truerun()函数体的 while 循环结束执行 quit()后线程结束。所以线程结束就是 run()函数执行退出。 重载closeEvent()事件在窗口关闭时确保线程被停止代码如下: void Dialog::closeEvent(QCloseEvent *event) { //窗口关闭事件必须结束线程if (threadA.isRunning()){threadA.stopThread();threadA.wait();}event-accept(); }3.3 程序结构及源码 3.3.1 程序结构 3.3.2 qdicethread.h #ifndef QDICETHREAD_H #define QDICETHREAD_H#include QThreadclass QDiceThread : public QThread {Q_OBJECT private:int m_seq0;//掷骰子次数序号int m_diceValue;//骰子点数bool m_Pausedtrue; //掷一次骰子bool m_stopfalse; //停止线程 protected:void run() Q_DECL_OVERRIDE; //线程任务 public:QDiceThread();void diceBegin();//掷一次骰子void dicePause();//暂停void stopThread(); //结束线程 signals:void newValue(int seq,int diceValue); //产生新点数的信号 };#endif // QDICETHREAD_H 3.3.3 qdicethread.cpp #include qdicethread.h #include QTimeQDiceThread::QDiceThread() {}void QDiceThread::diceBegin() { //开始掷骰子m_Pausedfalse; }void QDiceThread::dicePause() {//暂停掷骰子m_Pausedtrue; }void QDiceThread::stopThread() {//停止线程m_stoptrue; }void QDiceThread::run() {//线程任务m_stopfalse;//启动线程时令m_stopfalsem_seq0; //掷骰子次数qsrand(QTime::currentTime().msec());//随机数初始化qsrand是线程安全的while(!m_stop)//循环主体{if (!m_Paused){m_diceValueqrand(); //获取随机数m_diceValue(m_diceValue % 6)1;m_seq;emit newValue(m_seq,m_diceValue); //发射信号}msleep(500); //线程休眠500ms}// 在 m_stoptrue时结束线程任务quit();//相当于 exit(0),退出线程的事件循环 } 3.3.4 dialog.h #ifndef DIALOG_H #define DIALOG_H#include QDialog#include qdicethread.hnamespace Ui { class Dialog; }class Dialog : public QDialog {Q_OBJECTprivate:QDiceThread threadA;protected:void closeEvent(QCloseEvent *event);public:explicit Dialog(QWidget *parent 0);~Dialog();private slots:void onthreadA_started();void onthreadA_finished();void onthreadA_newValue(int seq, int diceValue);void on_btnClear_clicked();void on_btnDiceEnd_clicked();void on_btnDiceBegin_clicked();void on_btnStopThread_clicked();void on_btnStartThread_clicked();private:Ui::Dialog *ui; };#endif // DIALOG_H 3.3.5 dialog.cpp #include dialog.h #include ui_dialog.hvoid Dialog::closeEvent(QCloseEvent *event) { //窗口关闭事件必须结束线程if (threadA.isRunning()){threadA.stopThread();threadA.wait();}event-accept(); }Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) {//构造函数ui-setupUi(this);connect(threadA,SIGNAL(started()),this,SLOT(onthreadA_started()));connect(threadA,SIGNAL(finished()),this,SLOT(onthreadA_finished()));connect(threadA,SIGNAL(newValue(int,int)),this,SLOT(onthreadA_newValue(int,int))); }Dialog::~Dialog() {delete ui; }void Dialog::onthreadA_started() {//线程的started()信号的响应槽函数ui-LabA-setText(Thread状态thread started); }void Dialog::onthreadA_finished() {//线程的 finished()信号的响应槽函数ui-LabA-setText(Thread状态thread finished); }void Dialog::onthreadA_newValue(int seq,int diceValue) {//QDiceThread的newValue()信号的响应槽函数显示骰子次数和点数QString strQString::asprintf(第 %d 次掷骰子点数为%d,seq,diceValue);ui-plainTextEdit-appendPlainText(str);QPixmap pic; //图片显示QString filenameQString::asprintf(:/dice/images/d%d.jpg,diceValue);pic.load(filename);ui-LabPic-setPixmap(pic); }void Dialog::on_btnClear_clicked() { //清空文本 按钮ui-plainTextEdit-clear(); }void Dialog::on_btnDiceEnd_clicked() {//暂停 掷骰子按钮threadA.dicePause();ui-btnDiceBegin-setEnabled(true);ui-btnDiceEnd-setEnabled(false); }void Dialog::on_btnDiceBegin_clicked() {//开始 掷骰子按钮threadA.diceBegin();ui-btnDiceBegin-setEnabled(false);ui-btnDiceEnd-setEnabled(true); }void Dialog::on_btnStopThread_clicked() {//结束线程 按钮threadA.stopThread();//结束线程的run()函数执行threadA.wait();//ui-btnStartThread-setEnabled(true);ui-btnStopThread-setEnabled(false);ui-btnDiceBegin-setEnabled(false);ui-btnDiceEnd-setEnabled(false); }void Dialog::on_btnStartThread_clicked() {//启动线程 按钮threadA.start();ui-btnStartThread-setEnabled(false);ui-btnStopThread-setEnabled(true);ui-btnDiceBegin-setEnabled(true);ui-btnDiceEnd-setEnabled(false); }详细可见附带源码文件
http://www.zqtcl.cn/news/29686/

相关文章:

  • 极限优化wordpress温州seo建站
  • 东莞免费做网站公司织梦网站栏目如何做下拉
  • 网站建设费用计算依据wordpress主题4mudi
  • 建网站去哪里备案电子商务网站建设与维护的主要内容
  • qq空间网站域名怎么做的佛山网页设计师培训
  • 西安那里做网站展示型网站一样做seo优化
  • 哪家公司网站做的好深圳龙华天气预报
  • 做学校的网站推广发展前景做自己的外贸网站怎样赚钱
  • 西安淘宝网站建设公司哪家好综合电子商务型企业网站
  • 做网站必要吗采购系统erp软件
  • 北京移动端网站公司网站关键词优化代码
  • 建设工程网站贴吧未来做那些网站致富
  • 中国空间站的意义科技类公司网站设计
  • 迷你世界怎么做网站期网站做竞价对seo有影响吗
  • 做淘客网站 知乎重庆 seo
  • 下载宝硬盘做网站新公司网站怎么做推广
  • 电商门户网站建设方案广东省备建设项目影响备案网站
  • 哪个网站可以做一对一老师德清网站建设
  • 哈尔滨住房和城乡建设局网站白云区做网站
  • 网站维护的方式包括域名备案是什么
  • 西安机械加工网站建设可以做网站挂在百度上吗
  • 权威迷失传奇新开网站手机适配网站
  • 兰溪市住房和城乡建设局网站wordpress注册跳转
  • 甘南网站建设公司优化seo搜索排名
  • 潍坊建设公司网站做网站有兼职吗
  • 无锡网站排名公司app下载应用
  • 网站建设 坚持实用原则拼团系统开发
  • 网站开发技术可以做什么工作国外做电商平台的网站有什么
  • 中小企业建站国外做电商平台的网站还有什么
  • 福建网站建设推广上海建筑设计公司平台