Qt线程的简单使用
2015-09-09 15:55
423 查看
#include <QtWidgets/QApplication>
#include "threaddialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ThreadDialog *threaddialog = new ThreadDialog;
threaddialog->exec();
return a.exec();
}
#ifndef THREADDIALOG_H
#define THREADDIALOG_H
#include <QPushButton>
#include <QDialog>
#include <QCloseEvent>
#include "thread.h"
class ThreadDialog : public QDialog
{
Q_OBJECT
public:
ThreadDialog(QWidget *parent=0);
protected:
void closeEvent(QCloseEvent *event);
private slots:
void startOrStopThreadA();
void startOrStopThreadB();
void close();
private:
Thread threadA;
Thread threadB;
QPushButton *threadAButton;
QPushButton *threadBButton;
QPushButton *quitButton;
};
#endif // THREADDIALOG_H
#include "threaddialog.h"
ThreadDialog::ThreadDialog(QWidget *parent) : QDialog(parent)
{
threadA.setMessage("A");
threadB.setMessage("B");
threadAButton = new QPushButton(tr("Start A"), this);
threadAButton->setGeometry(10, 30, 80, 30);
threadBButton = new QPushButton(tr("Start B"),this);
threadBButton->setGeometry(110, 30, 80, 30);
quitButton = new QPushButton(tr("Quit"), this);
quitButton->setGeometry(210, 30, 80, 30);
quitButton->setDefault(true);
connect(threadAButton, SIGNAL(clicked()), this, SLOT(startOrStopThreadA()));
connect(threadBButton, SIGNAL(clicked()), this, SLOT(startOrStopThreadB()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
}
void ThreadDialog::startOrStopThreadA()
{
if(threadA.isRunning())
{
threadAButton->setText(tr("Stop A"));
threadA.stop();
threadAButton->setText(tr("Start A"));
}
else
{
threadAButton->setText(tr("Start A"));
threadA.start();
threadAButton->setText(tr("Stop A"));
}
}
void ThreadDialog::startOrStopThreadB()
{
if(threadB.isRunning())
{
threadBButton->setText(tr("Stop B"));
threadB.stop();
threadBButton->setText(tr("Strat B"));
}
else
{
threadBButton->setText(tr("Start B"));
threadB.start();
threadBButton->setText(tr("Stop B"));
}
}
void ThreadDialog::closeEvent(QCloseEvent *event)
{
threadA.stop();
threadB.stop();
threadA.wait();
threadB.wait();
event->accept();
}
void ThreadDialog::close()
{
exit(0);
}
#pragma once
#include <QThread>
class Thread :public QThread
{
Q_OBJECT
public:
Thread(void);
void setMessage(QString message);
void stop();
protected:
void run();
void printMessage();
private:
QString messageStr;
volatile bool stopped;
};
#include "thread.h"
#include <QDebug>
Thread::Thread()
{
stopped = false;
}
void Thread::run()
{
while(!stopped)
{
printMessage();
}
stopped = false;
}
void Thread::stop()
{
stopped = true;
}
void Thread::setMessage(QString message)
{
messageStr = message;
}
void Thread::printMessage()
{
qDebug()<<messageStr;
sleep(1);
}
#include "threaddialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ThreadDialog *threaddialog = new ThreadDialog;
threaddialog->exec();
return a.exec();
}
#ifndef THREADDIALOG_H
#define THREADDIALOG_H
#include <QPushButton>
#include <QDialog>
#include <QCloseEvent>
#include "thread.h"
class ThreadDialog : public QDialog
{
Q_OBJECT
public:
ThreadDialog(QWidget *parent=0);
protected:
void closeEvent(QCloseEvent *event);
private slots:
void startOrStopThreadA();
void startOrStopThreadB();
void close();
private:
Thread threadA;
Thread threadB;
QPushButton *threadAButton;
QPushButton *threadBButton;
QPushButton *quitButton;
};
#endif // THREADDIALOG_H
#include "threaddialog.h"
ThreadDialog::ThreadDialog(QWidget *parent) : QDialog(parent)
{
threadA.setMessage("A");
threadB.setMessage("B");
threadAButton = new QPushButton(tr("Start A"), this);
threadAButton->setGeometry(10, 30, 80, 30);
threadBButton = new QPushButton(tr("Start B"),this);
threadBButton->setGeometry(110, 30, 80, 30);
quitButton = new QPushButton(tr("Quit"), this);
quitButton->setGeometry(210, 30, 80, 30);
quitButton->setDefault(true);
connect(threadAButton, SIGNAL(clicked()), this, SLOT(startOrStopThreadA()));
connect(threadBButton, SIGNAL(clicked()), this, SLOT(startOrStopThreadB()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
}
void ThreadDialog::startOrStopThreadA()
{
if(threadA.isRunning())
{
threadAButton->setText(tr("Stop A"));
threadA.stop();
threadAButton->setText(tr("Start A"));
}
else
{
threadAButton->setText(tr("Start A"));
threadA.start();
threadAButton->setText(tr("Stop A"));
}
}
void ThreadDialog::startOrStopThreadB()
{
if(threadB.isRunning())
{
threadBButton->setText(tr("Stop B"));
threadB.stop();
threadBButton->setText(tr("Strat B"));
}
else
{
threadBButton->setText(tr("Start B"));
threadB.start();
threadBButton->setText(tr("Stop B"));
}
}
void ThreadDialog::closeEvent(QCloseEvent *event)
{
threadA.stop();
threadB.stop();
threadA.wait();
threadB.wait();
event->accept();
}
void ThreadDialog::close()
{
exit(0);
}
#pragma once
#include <QThread>
class Thread :public QThread
{
Q_OBJECT
public:
Thread(void);
void setMessage(QString message);
void stop();
protected:
void run();
void printMessage();
private:
QString messageStr;
volatile bool stopped;
};
#include "thread.h"
#include <QDebug>
Thread::Thread()
{
stopped = false;
}
void Thread::run()
{
while(!stopped)
{
printMessage();
}
stopped = false;
}
void Thread::stop()
{
stopped = true;
}
void Thread::setMessage(QString message)
{
messageStr = message;
}
void Thread::printMessage()
{
qDebug()<<messageStr;
sleep(1);
}
相关文章推荐
- QtSoap向webservice传递结构体或类参数
- qt 多线程访问数据库问题
- qt多线程编程之QThread
- QT最简单的打包发布技巧
- qt4解决中文乱码方法
- QtSoap传递多种类型参数
- QT 如何创建一个N行N列的控件
- 利用QT和libvlc写RTSP播放器
- QT5 动态链接库的创建和使用
- MAC下QT5 + Xcode 4.6.3 使用小结
- 用Qt开发第一个Hello World程序
- qt 多线程开发
- Qt安装后配置环境变量(Mac)
- Qt、Qte、Qtopia、Qvfb、Qpe概念区别
- 转:Qt 嵌入式开发环境搭建
- 学习Qt5(5)
- qt使用
- PyQt4 QPixmap.load/loadFromData 使用py2exe 打包后无法显示JPEG,gif等图片的问题
- 关于Qt在子线程中使用QMessageBox的折衷方法
- Qt设置窗口背景透明