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. ...
随机推荐
- HDU3870 Catch the Theves(平面图最小割转最短路)
题目大概说给一个n×n的方格,边有权值,问从求(1,1)到(n,n)的最小割. 点达到了160000个,直接最大流不好.这题的图是平面图,求最小割可以转化成求其对偶图的最短路,来更高效地求解: 首先源 ...
- CF#335 Board Game
Board Game time limit per test 2.5 seconds memory limit per test 256 megabytes input standard input ...
- HDU 3074 (线段树+模P乘法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3074 题目大意:单点更新.维护序列乘法.mod 1000000007. 解题思路: 10000000 ...
- 'libxml/HTMLparser.h' file not found in ASIHTTPRequest 解决方法
首先导入libxml2.dylib,具体怎么导入跟导入frameworks一样 然后在Build Setting中的Header Search Paths to: 添加 ${SDK_DIR}/usr/ ...
- ACM: 继续畅通工程-并查集-最小生成树-解题报告
继续畅通工程 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Descri ...
- [Leetcode] Word BreakII
Question: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence w ...
- osg中遇到的问题
osg中遇到的问题 今天写程序的时候, 需要把键盘和鼠标消息转发出来, 就直接写了接口用signal丢出来了. 程序写的很多, 测试的时候却崩溃了.... 在场景中拖拽鼠标左键的时候, 会发现在扔出鼠 ...
- SDL实战,小游戏
http://www.cppblog.com/sandy/archive/2005/12/28/2219.html sdl1教学 http://kelvmiao.info/sdl-tutorial-c ...
- Nginx_查看并发连接数
通过查看Nginx的并发连接,我们可以更清除的知道网站的负载情况.Nginx并发查看有两种方法(之所以这么说,是因为笔者只知道两种),一种是通过 web界面,一种是通过命令,web查看要比命令查看显示 ...
- Node.js的DES加解密和MD5加密
最基本的就是经常用的md5加密算法 代码如下 var MD5=function (data) { var _encrymd5 = require('crypto').createHas ...