#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. django 后台管理

    修改 admin.py from myapp.models import * from django.contrib import admin # Register your models here. ...

  2. 再探CSS 中 class 命名规范

    一直以来我的CSS 的 class命名都是比较随意,有时采用驼峰式.有时采用下划线,好像没有什么统一的标准,想到什么英文单词就拿过来用,这对于自己瞎写的小项目无伤大雅,遇到冲突的问题可稍加调整改变即可 ...

  3. Leetcode Reverse Words in a String

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  4. ACM:统计难题 解题报告-字典树(Trie树)

    统计难题 Time Limit:2000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submit Status ...

  5. 【BZOJ】1109: [POI2007]堆积木Klo

    题意 \(n(1 \le n \le 100000)\)个数放在一排,可以一走一些数(后面的数向前移),要求最大化\(a_i=i\)的数目. 分析 分析容易得到一个dp方程. 题解 \(d(i)\)表 ...

  6. POJ 1106 Transmitters(计算几何)

    题目链接 切计算几何,感觉计算几何的算法还不熟.此题,枚举线段和圆点的直线,平分一个圆 #include <iostream> #include <cstring> #incl ...

  7. URAL 1287. Mars Canals

    题目链接 这题挺水,看懂了,就OK.卡了几下内存,还是卡过了. #include <iostream> #include <cstdio> #include <cstri ...

  8. iOS 9之适配ATS(转载)

    iOS 9系统已经出来了,而网络方面的ATS(App Transport Security)特性可以说每个人都要经历.而我这篇博客,就是结合我最近几天的经历,来谈谈从服务器到iOS客户端对ATS的适配 ...

  9. Java_Java SE6调用动态编译

    转自:http://www.cnblogs.com/flyoung2008/archive/2011/11/14/2249017.html 一.使用JavaCompiler接口编译java源程序 我们 ...

  10. Controller 通信 发布接收广播

    在Angularjs开发一些经验总结随笔中提到我们需要按照业务却分angular controller,避免过大无所不能的上帝controller,我们把controller分离开了,但是有时候我们需 ...