qt多线程编程之QThread
2015-09-09 13:43
453 查看
#include <QApplication>
#include <QThread>
#include <QMainWindow>
#include <QProgressBar>
#include <QPushButton>
class RenderThread : public QThread
{
Q_OBJECT
signals:
void notify(int); //参数可以自己定义,个数不限,但是要注意和接受的曹的要一致
public:
RenderThread(QObject *parent = 0): QThread(parent)
{
};
void test()
{
start(HighestPriority);
};
protected:
void run()
{
int i =0;
while (i<101)
{
msleep(50);
i++;
emit notify(i);
}
};
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0)
{
resize(600, 400);
centralWidget = new QWidget(this);
progressBar = new QProgressBar(centralWidget);
progressBar->setGeometry(QRect(130, 180, 321, 23));
progressBar->setValue(0);
pushButton = new QPushButton("test",centralWidget);
pushButton->setGeometry(QRect(110, 100, 75, 23));
QObject::connect(pushButton, SIGNAL(clicked()), this, SLOT(OnClicked()));
this->setCentralWidget(centralWidget);
};
~MainWindow(){};
private:
QProgressBar *progressBar;
QPushButton *pushButton;
QWidget *centralWidget;
RenderThread render;
public slots:
void OnClicked()
{
connect(&render,SIGNAL(notify(int)),this,SLOT(OnNotify(int))); //要注意参数一致,在现成类的头上一定要有:signal:这个标志啊
render.test();
};
void OnNotify(int i)
{
progressBar->setValue(i);
};
};
#include <QThread>
#include <QMainWindow>
#include <QProgressBar>
#include <QPushButton>
class RenderThread : public QThread
{
Q_OBJECT
signals:
void notify(int); //参数可以自己定义,个数不限,但是要注意和接受的曹的要一致
public:
RenderThread(QObject *parent = 0): QThread(parent)
{
};
void test()
{
start(HighestPriority);
};
protected:
void run()
{
int i =0;
while (i<101)
{
msleep(50);
i++;
emit notify(i);
}
};
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0)
{
resize(600, 400);
centralWidget = new QWidget(this);
progressBar = new QProgressBar(centralWidget);
progressBar->setGeometry(QRect(130, 180, 321, 23));
progressBar->setValue(0);
pushButton = new QPushButton("test",centralWidget);
pushButton->setGeometry(QRect(110, 100, 75, 23));
QObject::connect(pushButton, SIGNAL(clicked()), this, SLOT(OnClicked()));
this->setCentralWidget(centralWidget);
};
~MainWindow(){};
private:
QProgressBar *progressBar;
QPushButton *pushButton;
QWidget *centralWidget;
RenderThread render;
public slots:
void OnClicked()
{
connect(&render,SIGNAL(notify(int)),this,SLOT(OnNotify(int))); //要注意参数一致,在现成类的头上一定要有:signal:这个标志啊
render.test();
};
void OnNotify(int i)
{
progressBar->setValue(i);
};
};
相关文章推荐
- 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设置窗口背景透明
- Qt 信号槽机制解析三 补充解释源代码
- QT之线程
- 转载:QT QTableView用法小结