深圳网站建设-中国互联,宁波网站建设公司推荐哪家,上海服装外贸公司,企业网站有哪些举例QT使用线程的两种方式
1.案例进度条
案例解析#xff1a;
如图由组件一个进度条和三个按钮组成#xff0c;当点击开始的时候进度条由0%到100%#xff0c;点击暂停#xff0c;进度条保持之前进度#xff0c;再次点击暂停变为继续#xff0c;点击停止按钮进度条停止。
案…QT使用线程的两种方式
1.案例进度条
案例解析
如图由组件一个进度条和三个按钮组成当点击开始的时候进度条由0%到100%点击暂停进度条保持之前进度再次点击暂停变为继续点击停止按钮进度条停止。
案例流程
1.创建qwidget工程
2.添加四个控件转到槽函数2.使用线程方式一
2.1创建一个类继承QThread重写run方法
2.2mythread.cpp
#include mythread.h
#include QDebug
MyThread::MyThread()
{}
void MyThread::stop()
{runningfalse;
}
//暂停继续
void MyThread::threadStop(bool flag)
{pauseflag;
}
//重写run方法
void MyThread::run()
{qDebug()线程id:currentThreadId();while (1) {//触发信号while(running){while (pause) {msleep(100);}if(value100)value0;emit valChage(value);msleep(100);}exit(0);}
}2.3mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include QThread
#include QDebug
//继承QThread重写run方法
class MyThread : public QThread
{Q_OBJECT
public slots:void stop();void threadStop(bool flag);
signals:void valChage(int);
public:MyThread();void run();
private:int value0;bool runningtrue;bool pausefalse;
};#endif // MYTHREAD_H
2.4widget.h
#ifndef WIDGET_H
#define WIDGET_H#include QWidget
#include QProgressBar
#includeQThread
#include mythread.h
namespace Ui {
class Widget;
}class Widget : public QWidget
{Q_OBJECTpublic:explicit Widget(QWidget *parent 0);~Widget();
signals:void stop();void threadStop(bool);
private slots:void on_pushButton_clicked();void on_pushButton_3_clicked();void on_pushButton_2_clicked();private:Ui::Widget *ui;MyThread *mythread;
};
#endif // WIDGET_H
2.5widget.cpp
#include widget.h
#include ui_widget.h
Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui-setupUi(this);}Widget::~Widget()
{delete ui;
}void Widget::on_pushButton_clicked(){mythread new MyThread();//绑定信号与槽函数connect(mythread,MyThread::valChage,ui-progressBar,QProgressBar::setValue);//延时connect(mythread,MyThread::finished,mythread, QObject::deleteLater);connect(this,Widget::stop,mythread,MyThread::stop);connect(this,Widget::threadStop,mythread,MyThread::threadStop);mythread-start();ui-pushButton-setEnabled(false);}void Widget::on_pushButton_3_clicked()
{emit stop();
}
void Widget::on_pushButton_2_clicked()
{static bool flagtrue;if(flag){emit threadStop(true);ui-pushButton_2-setText(继续);flagfalse;}else{emit threadStop(false);ui-pushButton_2-setText(暂停);flagtrue;}
}
3.使用线程方式二
3.1创建类继承QObject 3.2qworker.h
这里的槽函数实现直接写在.h文件中不够规范只便与学习观看切不要效仿。
#ifndef QWORKER_H
#define QWORKER_H
#include QObject
#include QThread
#include QApplication
class qworker : public QObject
{Q_OBJECT
public:explicit qworker(QObject *parent nullptr);signals:void dataChanged(int);
public slots:void doWorking(){while (!sFlag) {if(current100)current0;while (pFlag) {QThread::msleep(10);//接收来自外部进程的事件否则收不到信号QApplication::processEvents();}emit dataChanged(current);QThread::msleep(10);QApplication::processEvents();}sFlagfalse;current0;}void pause(bool flag){pFlagflag;}void stop(){sFlagtrue;}
private://进度条int current0;//暂停bool pFlagfalse;//停止bool sFlagfalse;
};
#endif // QWORKER_H3.3widget.h
#ifndef WIDGET_H
#define WIDGET_H#include QWidget
//引入qworker.h
#include qworker.hnamespace Ui {
class Widget;
}class Widget : public QWidget
{Q_OBJECTpublic:explicit Widget(QWidget *parent 0);~Widget();
signals:void working();void pause(bool);void stop();
private slots:void on_pushButton_clicked();void on_pushButton_2_clicked();void on_pushButton_3_clicked();
c
private:Ui::Widget *ui;qworker *worker;QThread thread;
};
#endif // WIDGET_H
3.4widget.cpp
#include widget.h
#include ui_widget.hWidget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui-setupUi(this);workernew qworker();//移动到线程中worker-moveToThread(thread);//开始connect(this,Widget::working,worker,qworker::doWorking);//暂停connect(this,Widget::pause,worker,qworker::pause);//停止connect(this,Widget::stop,worker,qworker::stop);connect(worker,qworker::dataChanged,ui-progressBar,QProgressBar::setValue);thread.start();
}
Widget::~Widget()
{delete ui;
}
void Widget::on_pushButton_clicked()
{emit working();
}void Widget::on_pushButton_2_clicked()
{static bool flagtrue;if(flag){emit pause(true);flagfalse;ui-pushButton_2-setText(继续);}else{emit pause(false);flagtrue;ui-pushButton_2-setText(暂停);}
}
void Widget::on_pushButton_3_clicked()
{emit stop();
}4.总结
两种方式都可以完成案例需求
方式一
1.通过继承QThread类重写run方法
2.重写类MyThread的虚函数void run();即新建一个函数protected void run()然后对其进行定义。
3.在需要用到多线程的地方实例MyThread然后调用函数MyThread::start()后则开启一条线程自动运行函数run()。
4.当停止线程时调用MyThread::wait()函数等待线程结束并且回收线程资源。
方式二
1.继承QObject类创建对象。
2.通过moveToThread将派生类对象移动到一个线程中。
3.通过信号连接派生类的槽函数将耗时的工作放到这个槽函数中运行。
4.用信号QThread::finished绑定槽函数QThread::deleteLatater()在线程退出时自动销毁该线程和相关资源。
5.通过QThread的start()函数开启多线程。 文章转载自: http://www.morning.flhnd.cn.gov.cn.flhnd.cn http://www.morning.rfpxq.cn.gov.cn.rfpxq.cn http://www.morning.kxryg.cn.gov.cn.kxryg.cn http://www.morning.fphbz.cn.gov.cn.fphbz.cn http://www.morning.rylr.cn.gov.cn.rylr.cn http://www.morning.sdamsm.com.gov.cn.sdamsm.com http://www.morning.mqfw.cn.gov.cn.mqfw.cn http://www.morning.jhrkm.cn.gov.cn.jhrkm.cn http://www.morning.gqnll.cn.gov.cn.gqnll.cn http://www.morning.ckwxs.cn.gov.cn.ckwxs.cn http://www.morning.wnqbf.cn.gov.cn.wnqbf.cn http://www.morning.lfdzr.cn.gov.cn.lfdzr.cn http://www.morning.sdkaiyu.com.gov.cn.sdkaiyu.com http://www.morning.gqbks.cn.gov.cn.gqbks.cn http://www.morning.mjmtm.cn.gov.cn.mjmtm.cn http://www.morning.mumgou.com.gov.cn.mumgou.com http://www.morning.ttdbr.cn.gov.cn.ttdbr.cn http://www.morning.dbrdg.cn.gov.cn.dbrdg.cn http://www.morning.znpyw.cn.gov.cn.znpyw.cn http://www.morning.lwcgh.cn.gov.cn.lwcgh.cn http://www.morning.tpwrm.cn.gov.cn.tpwrm.cn http://www.morning.hmxb.cn.gov.cn.hmxb.cn http://www.morning.xbxks.cn.gov.cn.xbxks.cn http://www.morning.nlbhj.cn.gov.cn.nlbhj.cn http://www.morning.fgrcd.cn.gov.cn.fgrcd.cn http://www.morning.xjqrn.cn.gov.cn.xjqrn.cn http://www.morning.elsemon.com.gov.cn.elsemon.com http://www.morning.nlkhr.cn.gov.cn.nlkhr.cn http://www.morning.mhfbf.cn.gov.cn.mhfbf.cn http://www.morning.lbbyx.cn.gov.cn.lbbyx.cn http://www.morning.xsctd.cn.gov.cn.xsctd.cn http://www.morning.ytbr.cn.gov.cn.ytbr.cn http://www.morning.rhdln.cn.gov.cn.rhdln.cn http://www.morning.ylpwc.cn.gov.cn.ylpwc.cn http://www.morning.ruifund.com.gov.cn.ruifund.com http://www.morning.lbxcc.cn.gov.cn.lbxcc.cn http://www.morning.fhrgk.cn.gov.cn.fhrgk.cn http://www.morning.ctlbf.cn.gov.cn.ctlbf.cn http://www.morning.qkgwz.cn.gov.cn.qkgwz.cn http://www.morning.xhhqd.cn.gov.cn.xhhqd.cn http://www.morning.rttkl.cn.gov.cn.rttkl.cn http://www.morning.klzt.cn.gov.cn.klzt.cn http://www.morning.srwny.cn.gov.cn.srwny.cn http://www.morning.mjjty.cn.gov.cn.mjjty.cn http://www.morning.nyqxy.cn.gov.cn.nyqxy.cn http://www.morning.ypwlb.cn.gov.cn.ypwlb.cn http://www.morning.zcsyz.cn.gov.cn.zcsyz.cn http://www.morning.phjyb.cn.gov.cn.phjyb.cn http://www.morning.mzzqs.cn.gov.cn.mzzqs.cn http://www.morning.lqypx.cn.gov.cn.lqypx.cn http://www.morning.wmmqf.cn.gov.cn.wmmqf.cn http://www.morning.sftpg.cn.gov.cn.sftpg.cn http://www.morning.fbmjw.cn.gov.cn.fbmjw.cn http://www.morning.xpmhs.cn.gov.cn.xpmhs.cn http://www.morning.dglszn.com.gov.cn.dglszn.com http://www.morning.qxlgt.cn.gov.cn.qxlgt.cn http://www.morning.hksxq.cn.gov.cn.hksxq.cn http://www.morning.frtt.cn.gov.cn.frtt.cn http://www.morning.zxqqx.cn.gov.cn.zxqqx.cn http://www.morning.nxbsq.cn.gov.cn.nxbsq.cn http://www.morning.rwyw.cn.gov.cn.rwyw.cn http://www.morning.bwfsn.cn.gov.cn.bwfsn.cn http://www.morning.drnfc.cn.gov.cn.drnfc.cn http://www.morning.jbmsp.cn.gov.cn.jbmsp.cn http://www.morning.fhrt.cn.gov.cn.fhrt.cn http://www.morning.htbgz.cn.gov.cn.htbgz.cn http://www.morning.rdlfk.cn.gov.cn.rdlfk.cn http://www.morning.wftrs.cn.gov.cn.wftrs.cn http://www.morning.pangucheng.cn.gov.cn.pangucheng.cn http://www.morning.rlsd.cn.gov.cn.rlsd.cn http://www.morning.ohmyjiu.com.gov.cn.ohmyjiu.com http://www.morning.ckfyp.cn.gov.cn.ckfyp.cn http://www.morning.xhxsr.cn.gov.cn.xhxsr.cn http://www.morning.bpmdn.cn.gov.cn.bpmdn.cn http://www.morning.gpfuxiu.cn.gov.cn.gpfuxiu.cn http://www.morning.yjmns.cn.gov.cn.yjmns.cn http://www.morning.dtlnz.cn.gov.cn.dtlnz.cn http://www.morning.fflnw.cn.gov.cn.fflnw.cn http://www.morning.fypgl.cn.gov.cn.fypgl.cn http://www.morning.mlyq.cn.gov.cn.mlyq.cn