QT 多线程信号与槽(一)
来源:互联网 发布:知错改错不认错下一句 编辑:程序博客网 时间:2024/04/28 21:29
1.一个主界面(主线程),一个次线程,次线程通过信号向主线程传递递增变量,主线程将它显示出来
mythread.h
#include <QThread>
#include <QDebug>
class Mythread : public QThread
{
Q_OBJECT
public:
Mythread();
protected:
void run();
private:
int i;
signals:
void signal_add_int(QString);
};mythread.cpp
#include "mythread.h"
#include <QDebug>
Mythread::Mythread()
{
i = 0;
}
void Mythread::run()
{
while(1)
{
i++;
QString str = QString::number(i,10);
emit signal_add_int(str);
sleep(1);
qDebug()<<"run thread:" <<QThread::currentThreadId();
}
}
mainwindow.h
#include <QMainWindow>#include "mythread.h"namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);~MainWindow();private:
Ui::MainWindow *ui;Mythread thread;private slots:void set_lineEdit_text(QString);void start_thread();};mainwindow.cpp
#include "mainwindow.h"#include "ui_mainwindow.h"#include <QDebug>MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow){ui->setupUi(this);connect(&thread,SIGNAL(signal_add_int(QString)),this,SLOT(set_lineEdit_text(QString)),Qt::QueuedConnection);connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(start_thread()));qDebug()<<"MainWindow:" <<QThread::currentThreadId();}MainWindow::~MainWindow()
{delete ui;}void MainWindow::set_lineEdit_text(QString str){ui->lineEdit->setText(str);qDebug()<<"from thread slot:" <<QThread::currentThreadId();}void MainWindow::start_thread(){thread.start();}打印结果:MainWindow: 3062654672 from thread slot: 3062654672 run thread: 3017550736 from thread slot: 3062654672 run thread: 3017550736 from thread slot: 3062654672 run thread: 3017550736 from thread slot: 3062654672结论:
主线程运行thread.start()后,次线程启动,进入到次线程中,次线程发送信号给主线程,主线程接收到该信号,进入槽函数,该槽函数也在主线程中运行。次线程的开始和结束都在run函数中开始和结束
- QT 多线程信号与槽(一)
- QT 多线程信号与槽(一)
- QT 多线程信号与槽(二)
- QT 多线程信号与槽(三)
- QT 多线程信号与槽(二)
- QT 多线程信号与槽(三)
- QT 笔记一(信号与槽)
- QT信号槽实现多线程通信(一)
- QT中的信号-槽函数与多线程
- Qt多线程中的信号与槽
- qt 多线程 信号槽
- QT 的信号与槽机制介绍(一)
- Qt学习(一)--了解信号与槽
- QT学习源网址转载1---QT多线程参数传递(信号与槽)方法
- qt 信号槽(connect)失效,【多线程使用信号槽】
- Qt 信号与信号,信号与槽
- Qt多线程和信号与槽加载多张图片
- Qt实现多线程下的信号与槽通讯
- 如何看一个比较大的程序代码
- JS截取字符串substr 和 substring方法的区别
- day20File类,各方法。递归。删除一带内容的目录。Properties 。记录应用程序运行次数。打印流。切割合并文件。
- 子网划分
- error C2664: “CWnd::MessageBoxW”: 不能将参数 1 从“const char [17]”转换为“LPCTSTR
- QT 多线程信号与槽(一)
- 25个增强iOS应用程序性能的提示和技巧--高级篇
- 高性能 网络编程
- JAVA编程思想笔记【突击1】——初始化,访问权限控制,继承,多态
- JavaScript修改css中style,className,cssText实例
- day21对象流。管道流。RandomAccessFile。DataStream。字节流。常见的编码表。编码解码。联通问题。
- linux 快速安装eclipse,开发Python
- java com.microsoft.sqlserver.jdbc.SQLServerException: 该连接已关闭。
- Android OOM介绍及分析方法