#ifndef MYTHREAD_H
#define MYTHREAD_H
#include<QThread>

class MyThread : public QThread
{
    Q_OBJECT
public:
    explicit MyThread(QObject *parent = 0);
    void run();
    bool Stop;

signals:
    void NumberChanged(int);
public slots:

};

#endif // MYTHREAD_H

  

#ifndef DIALOG_H
#define DIALOG_H #include "mythread.h"
#include <QDialog>
#include<QLabel> namespace Ui {
class Dialog;
} class Dialog : public QDialog
{
Q_OBJECT public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
MyThread *mThread; private:
Ui::Dialog *ui; public slots:
void onNumberChanged(int);
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
}; #endif // DIALOG_H

  

#include "dialog.h"
#include "ui_dialog.h"
#include<QLabel> Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
mThread = new MyThread(this);
connect(mThread,SIGNAL(NumberChanged(int)),this, SLOT(onNumberChanged(int))); } Dialog::~Dialog()
{
delete ui;
} void Dialog::onNumberChanged(int Number)
{
ui->label->setText(QString::number(Number));
} void Dialog::on_pushButton_clicked()
{
//started
mThread->start();
} void Dialog::on_pushButton_2_clicked()
{
mThread->Stop = true;
}

  

#include "dialog.h"
#include <QApplication> int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show(); return a.exec();
}

  

#include "mythread.h"
#include <QThread>
MyThread::MyThread(QObject *parent) : QThread(parent)
{ } void MyThread::run()
{
this->Stop = false;
for(int i = 0; i < 10000;i++)
{
QMutex mutex;
mutex.lock();
if(this->Stop) break;
mutex.unlock(); emit NumberChanged(i); this->msleep(1000);
}
}

  

QThread的更多相关文章

  1. Qt - QThread(翻译帮助文档)

    QThread Class 详细描述 QThread 类提供一个平台无关的方法来管理线程. 一个QThread对象管理一个程序中的控制线程.QThread在run()中开始执行任务.默认地,run() ...

  2. pyqt4:在线程Qthread中使用定时器Qtimer

    GUI main 部分,主app类中的__init__初始化方法中添加 实例化线程 self.s2_thread=Worker2() 初始化一个定时器 self.log_get=QtCore.QTim ...

  3. Qt QThread 多线程使用

    一.继承QThread 使用方法 1.创建个继承QThread的类. #ifndef MYTHREAD_H #define MYTHREAD_H #include <QObject> #i ...

  4. Pyqt Smtplib实现Qthread多线程发送邮件

    一. smtplib 的介绍 smtplib.SMTP([host[, port[, local_hostname[, timeout]]]])   SMTP类构造函数,表示与SMTP服务器之间的连接 ...

  5. Qt线程(2) QThread中使用WorkObject

    一般继承QThread的WorkThread都会在重载的run()中创建临时的WorkObject,这样能确定这个WorkObject在该thread中使用 那如果这个WorkObject是个Sing ...

  6. [转] - QThread应用探讨

    QThread 似乎是很难的一个东西,特别是信号和槽,有非常多的人(尽管使用者本人往往不知道)在用不恰当(甚至错误)的方式在使用 QThread,随便用google一搜,就能搜出大量结果出来.无怪乎Q ...

  7. Qt之QThread(深入理解)

    简述 为了让程序尽快响应用户操作,在开发应用程序时经常会使用到线程.对于耗时操作如果不使用线程,UI界面将会长时间处于停滞状态,这种情况是用户非常不愿意看到的,我们可以用线程来解决这个问题. 前面,已 ...

  8. 【转】Qt多线程操作界面---在QThread更新QProgressBar

    #include <QApplication> #include <QThread> #include <QMainWindow> #include <QPr ...

  9. QThread与QWidget使用

    本文主要内容: 在任务一中,用 四 种方式实现:点击界面按钮,开线程运行一段程序,结果显示在一个Label上.1. 用不正确的方式得到看似正确的结果2. 用Qt Manual 和 例子中使用的方法3. ...

随机推荐

  1. Fetch from Upstream 变灰失效

    Team——>Remote——>Configure Fetch from Upstream… Team——>Remote——>Configure Push to  Upstre ...

  2. BZOJ4657 : tower

    显然只有横向和纵向的两个炮塔才有可能冲突. 考虑最小割,将每个炮塔所有能攻击到的位置建点,相邻之间连无穷的边,表示前缀和关系,即选了一个点,就必须要选所有比它近的点. 属于横向炮塔的点向$S$连边,容 ...

  3. BZOJ3726 : PA2014Final Wykladzina

    从上到下枚举下底边,维护$a[i]$表示$i$向上延伸多少距离里面没有坏点,$b[i]$表示$i$向上延伸多少距离里面最多只有1个坏点. 设$l0[i],r0[i]$表示以$a[i]$为最小值,往左往 ...

  4. [R语言]forecast.Arima中使用xreg报错

    问题: 使用forecast.Arima对带xreg的arima模型进行预测,报xreg Error pre.m4x <- forecast.Arima(m4x, h = 20, xreg = ...

  5. 对只转发结果集的无效操作 first

    今天只用jdbc连接Oracle查询结果时,出现了一个: 对只转发结果集的无效操作 first 的错误java.sql.sqlexception. 出现这个结果的原因是:使用 Statement st ...

  6. Web标准中用于改善Web应用程序性能的各种方法总结

    提起Web应用程序中的性能改善,广大开发者们可能会想到JavaScript与DOM访问等基于各种既存技术的性能改善方法.最近,各种性能改善方法被汇总成为一个Web标准. 本文对Web标准中所包含的各种 ...

  7. div基础

    1. 写在后面的样式优于前面,会把前面的覆盖掉! 2.三角形的造法:width:0; height:0;然后设置border-left   border-right  border-top  bord ...

  8. 【CodeVS】p1299 切水果

    题目描述 Description 简单的说,一共N个水果排成一排,切M次,每次切[L,R]区间的所有水果(可能有的水果被重复切),每切完一次输出剩下水果数量 数据已重新装配,不会出现OLE错误 时限和 ...

  9. HTML5中video 和 ogg

    HTML5中 的ogg 从网上学习HTML5之video时看到了下面的代码,不太清楚ogg是什么,于是搜索了一些知识点供了解.

  10. windows开机启动项

    原来就一个命令呀:msconfig 1.在开始菜单中输入 msconfig 命令,回车 2.在弹出的对话框中取消不想启动的程序 3.点击应用->确定->不启动