当前位置: 首页 > news >正文

网站建设免费视频教程seo外链资源

网站建设免费视频教程,seo外链资源,北京婚纱摄影网站,做单机游戏破解的网站QThread::CurrentThread是指的当前函数调用者者所在的线程 this->thread是指的当前对象所在的线程(对象创建出来的时候所在的线程) Qt文档说明 CurrentThread返回一个指向管理当前执行线程的QThread的指针 thread返回对象所在的线程 这两个函数所…

QThread::CurrentThread是指的当前函数调用者者所在的线程

this->thread是指的当前对象所在的线程(对象创建出来的时候所在的线程)

Qt文档说明

CurrentThread返回一个指向管理当前执行线程的QThread的指针

thread返回对象所在的线程

这两个函数所说的并不是一回事

如果想要将一个对象保证在主线程中处理一些事情,应该是使用this->thread来判断对象所处的线程,而并不是使用QThread::CurrentThread来判断对象所处的线程

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include "mythread.h"QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private slots:void on_pushButton_clicked();signals:void sigTest();
private:Ui::MainWindow *ui;MyThread *m_pThread;
};
#endif // MAINWINDOW_H

mainwindow.cpp 

#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);m_pThread = new MyThread();m_pThread->start();
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::on_pushButton_clicked()
{m_pThread->m_flag = ui->checkBox->checkState();
}

 mythread.h

#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>class MyThread:public QThread
{Q_OBJECT
public:MyThread();bool m_flag = false;protected:void run();};#endif // MYTHREAD_H

mythread.cpp

#include "mythread.h"
#include "singleclass.h"
#include <QDebug>MyThread::MyThread() {}void MyThread::run()
{qDebug()<<"子线程:"<<QThread::currentThread();while(true){QThread::msleep(100);if(m_flag){SingleClass::getInstance()->Test();m_flag = false;}}
}

 singleclass.h 测试对象

#ifndef SINGLECLASS_H
#define SINGLECLASS_H#include <QObject>class SingleClass : public QObject
{Q_OBJECT
public:static SingleClass* getInstance();void Test();
public slots:void slotTest();
signals:void sigTest();private:explicit SingleClass();
};#endif // SINGLECLASS_H

 singleclass.cpp

#include "singleclass.h"
#include <QThread>
#include <QDebug>
#include <QApplication>SingleClass::SingleClass()
{connect(this,&SingleClass::sigTest,this,&SingleClass::slotTest,Qt::BlockingQueuedConnection);
}SingleClass* SingleClass::getInstance()
{static SingleClass instance;return &instance;
}void SingleClass::Test()
{qDebug()<<"Test";qDebug()<<"currentThread:"<<QThread::currentThread();qDebug()<<"this线程"<<this->thread();if(this->thread() != qApp->thread()){qDebug()<<"this 所在的线程非主线程,移动到主线程中";moveToThread(qApp->thread());}qDebug()<<"after:";qDebug()<<"currentThread"<<__FUNCTION__<<QThread::currentThread();qDebug()<<"this 线程"<<this->thread();emit sigTest();
}void SingleClass::slotTest()
{qDebug()<<"slotTest";qDebug()<<"信号发出者所在的线程:"<<sender()->thread();qDebug()<<"currentThread"<<__FUNCTION__<<QThread::currentThread();qDebug()<<"this 线程"<<this->thread();
}

 main.cpp

#include "mainwindow.h"#include <QApplication>
#include <SingleClass.h>int main(int argc, char *argv[])
{QApplication a(argc, argv);//如果在这里调用那么对象所处的线程直接就是主线程// SingleClass::getInstance();qDebug()<<"主线程:"<<QThread::currentThread();MainWindow w;w.show();return a.exec();
}

 界面

 点击按钮之后输出

主线程: QThread(0x20d70e06b00)
子线程: MyThread(0x20d70e7db40)
Test
currentThread: MyThread(0x20d70e7db40)
this线程 MyThread(0x20d70e7db40)
this 所在的线程非主线程,移动到主线程中
after:
currentThread SingleClass::Test MyThread(0x20d70e7db40)
this 线程 QThread(0x20d70e06b00)
slotTest
信号发出者所在的线程: QThread(0x20d70e06b00)
currentThread SingleClass::slotTest QThread(0x20d70e06b00)
this 线程 QThread(0x20d70e06b00)

可以看到SingleClass这个类对象是在子线程中的,CurrentThread线程是子线程

之后调用moveToThread之后singClass类对象是移动到了主线程,所以通过使用connect(this,&SingleClass::sigTest,this,&SingleClass::slotTest,Qt::BlockingQueuedConnection);

因为是队列连接,所以槽函数是在槽函数所在的线程中执行的 

之后发出信号之后,slotTest就是在主线程中执行了

注意:信号是在子线程中发出,槽函数是在主线程执行的

信号发出的线程并不是指的信号所在的对象的线程,而是发出信号时候的被调用函数的所在的线程

http://www.zhongyajixie.com/news/37056.html

相关文章:

  • 响应式模版移动优化seo快速排名软件网站
  • php smarty 网站源码百度网站建设
  • 网站推广和宣传的方法广州seo网络营销培训
  • 深圳网站建设公司设计企业网站建站
  • 中国网站开发的前景百度关键词竞价查询系统
  • 网站建设需求方案河南seo和网络推广
  • 资深的教育行业网站开发cpa推广平台
  • 非盈利性备案网站 淘宝客网站郑州网站建设用户
  • 南通动态网站建设互联网营销的方法有哪些
  • 长安网站建设费用搜索关键词排名
  • 简述电子商务网站开发的研究意义网站优化费用报价明细
  • 在线网页客服整站seo服务
  • odoo 网站页面怎么做成免费的crm
  • 延吉市住房城乡建设局官方网站普通话手抄报文字内容
  • 万网注册域名做简单网站中国免费域名注册平台
  • 市建设工程信息网家庭优化大师下载
  • 做的比较早的海淘网站注册安全工程师
  • 政府类网站建设费用最新军事新闻
  • 《电子商务网站开发与管理》百度热度指数排行
  • 网站服务器架设牛推网
  • 网站建设喀什网络营销业务流程
  • 软路由系统如何做网站重庆网站seo外包
  • 河南网站优化要多少钱链接点击量软件
  • 免费建立网站平台抖音代运营
  • 光电网站建设自己建网站需要多少钱
  • 怎么做局域网asp网站上海网络推广营销策划方案
  • 外发加工网接单廊坊seo快速排名
  • 做企业网站的意义seo关键词是什么
  • 淄博免费网站建设哪家好商旅100网页版
  • 做网站设计多少钱每日财经要闻