QThread

#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的更多相关文章
- Qt - QThread(翻译帮助文档)
QThread Class 详细描述 QThread 类提供一个平台无关的方法来管理线程. 一个QThread对象管理一个程序中的控制线程.QThread在run()中开始执行任务.默认地,run() ...
- pyqt4:在线程Qthread中使用定时器Qtimer
GUI main 部分,主app类中的__init__初始化方法中添加 实例化线程 self.s2_thread=Worker2() 初始化一个定时器 self.log_get=QtCore.QTim ...
- Qt QThread 多线程使用
一.继承QThread 使用方法 1.创建个继承QThread的类. #ifndef MYTHREAD_H #define MYTHREAD_H #include <QObject> #i ...
- Pyqt Smtplib实现Qthread多线程发送邮件
一. smtplib 的介绍 smtplib.SMTP([host[, port[, local_hostname[, timeout]]]]) SMTP类构造函数,表示与SMTP服务器之间的连接 ...
- Qt线程(2) QThread中使用WorkObject
一般继承QThread的WorkThread都会在重载的run()中创建临时的WorkObject,这样能确定这个WorkObject在该thread中使用 那如果这个WorkObject是个Sing ...
- [转] - QThread应用探讨
QThread 似乎是很难的一个东西,特别是信号和槽,有非常多的人(尽管使用者本人往往不知道)在用不恰当(甚至错误)的方式在使用 QThread,随便用google一搜,就能搜出大量结果出来.无怪乎Q ...
- Qt之QThread(深入理解)
简述 为了让程序尽快响应用户操作,在开发应用程序时经常会使用到线程.对于耗时操作如果不使用线程,UI界面将会长时间处于停滞状态,这种情况是用户非常不愿意看到的,我们可以用线程来解决这个问题. 前面,已 ...
- 【转】Qt多线程操作界面---在QThread更新QProgressBar
#include <QApplication> #include <QThread> #include <QMainWindow> #include <QPr ...
- QThread与QWidget使用
本文主要内容: 在任务一中,用 四 种方式实现:点击界面按钮,开线程运行一段程序,结果显示在一个Label上.1. 用不正确的方式得到看似正确的结果2. 用Qt Manual 和 例子中使用的方法3. ...
随机推荐
- BZOJ4584 : [Apio2016]赛艇
首先将值域离散化成$O(n)$个连续段. 设$f[i][j][k]$表示第$i$个学校派出的数量在第$j$个连续段,在第$j$个连续段一共有$k$个学校的方案数.用组合数以及前缀和转移即可. 时间复杂 ...
- 在Spring-Mybatis-Restful中配置多数据源的properties文件
在Spring的配置文件中,如下: <?xml version="1.0" encoding="utf-8"?> <beans xmlns=& ...
- sublime注释插件与javascript注释规范
前言 代码中注释是不可少的,即使是自己写的代码,过了一段时间之后再重看,如果没有注释记录的话,可能会想不到当初是这样实现的,尤其是在业务逻辑比较复杂的项目,注释变得尤为重要.怎么优雅的写有用的注释呢? ...
- C++11 feature: move constructor
There are heaps of good articles out there about C++ features including this move constructor. Howev ...
- CF 335B. Palindrome(DP)
题目链接 挺好玩的一个题,1Y... #include <cstdio> #include <cstring> #include <iostream> using ...
- SQL笔记----在一个关系表中操作列
使用alter关键字,可以为一个表添加新的列. 比如: 给Persons的表中添加一列,名字为Birthday,类型是date. ALTER TABLE Persons ADD Birthday da ...
- HDU 5877 dfs+ 线段树(或+树状树组)
1.HDU 5877 Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...
- NHibernate one-to-one
NHibernate里面one-to-one有两种方式:主键关联和唯一外健关联 主键关联: 两个表拥有相同的主键字段,值相同的关联在一起.典型的应用是一个对象的属性太多,将常用的属性跟不常用的附加属性 ...
- 纪念逝去的岁月——C/C++快速排序
快速排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...
- Linux多线程实例练习 - pthread_exit() 与 pthread_join()
Linux多线程实例练习 - pthread_exit 与 pthread_join pthread_exit():终止当前线程 void pthread_exit(void* retval); pt ...