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

北京外贸网站设计备案seo公司是什么

北京外贸网站设计备案,seo公司是什么,哪里制作企业网站,柬埔寨网站建设文章目录 多线程实现方式一:继承QThread类方式二: 使用QObject::moveToThread()方法 多线程实现 在Qt中,实现多线程编程有两种常见的方式,它们分别是通过继承QThread类和使用QObject::moveToThread()方法。 方式一:继…

文章目录

  • 多线程实现
    • 方式一:继承QThread类
    • 方式二: 使用QObject::moveToThread()方法

多线程实现

在Qt中,实现多线程编程有两种常见的方式,它们分别是通过继承QThread类和使用QObject::moveToThread()方法。

方式一:继承QThread类

通过继承QThread类并重写其run()方法,可以在新线程中执行特定的任务。

具体步骤

  1. 创建自定义线程类:继承QThread类,并重写run()方法。在run()方法中编写需要在新线程中执行的代码。

  2. 创建线程对象:在主线程中创建自定义线程类的实例。

  3. 启动线程:调用线程对象的start()方法启动线程。此时,Qt会创建一个新的线程,并在该线程中调用run()方法。

  4. 线程间通信:可以通过信号和槽机制在主线程和子线程之间进行通信。自定义线程类可以发射信号,主线程可以连接这些信号到相应的槽函数,以处理线程返回的数据或状态。

示例代码
https://github.com/BinaryAI-1024/QtStudy/tree/master/thread/createThread1

// mythread.h
#include <QThread>
#include <QDebug>class Mythread: public QThread{Q_OBJECT
public:explicit Mythread(QObject *parent = nullptr);void run();};
// mythread.cpp
#include "Mythread.h"Mythread::Mythread(QObject *parent) : QThread(parent)
{}void Mythread::run(){// 在新线程中执行的代码for (int i = 0; i < 5; ++i) {qDebug() << "Worker thread is running... (" << i << ")";QThread::sleep(1); // 模拟耗时操作}
}
//main.cpp
#include <QCoreApplication>
#include "Mythread.h"
int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);Mythread thread1;thread1.start();for (int i = 0; i < 5; ++i) {qDebug() << "Main thread is running... (" << i << ")";QThread::sleep(1); // 模拟耗时操作}thread1.wait(); // 等待直到thread1线程结束return a.exec();
}

运行结果:

Main thread is running... ( 0 )
Worker thread is running... ( 0 )
Worker thread is running... ( 1 )
Main thread is running... ( 1 )
Worker thread is running... ( 2 )
Main thread is running... ( 2 )
Worker thread is running... ( 3 )
Main thread is running... ( 3 )
Main thread is running... ( 4 )
Worker thread is running... ( 4 )

优缺点分析

  • 优点:实现简单,适合需要在新线程中执行单一任务的场景。

  • 缺点

    • 强制将业务逻辑与线程控制代码耦合在一起,不利于代码的复用和维护。
    • 需要手动管理线程的生命周期,增加了代码的复杂性。

方式二: 使用QObject::moveToThread()方法

通过将QObject(或其子类)的实例移动到新线程中,可以在该线程中执行该对象的方法。这种方法更加灵活,不需要继承QThread类。

具体步骤

  1. 创建工作对象:定义一个继承自QObject的类,并在其中定义需要在新线程中执行的方法(槽函数)。

  2. 创建线程对象:在主线程中创建一个QThread实例。

  3. 移动对象到线程:调用工作对象的moveToThread()方法,将其移动到新创建的线程中。

  4. 启动线程并调用槽函数

    • 调用线程对象的start()方法启动线程。
    • 使用信号和槽机制,在主线程中发射信号,触发工作对象在新线程中执行的槽函数。
  5. 线程间通信:同样可以通过信号和槽机制在主线程和工作线程之间进行通信。工作线程可以发射信号,主线程连接这些信号到相应的槽函数,以处理线程返回的数据或状态。

示例代码
https://github.com/BinaryAI-1024/QtStudy/tree/master/thread/createThread2

//myworker.h
#include <QObject>
class MyWorker : public QObject
{
public:explicit MyWorker(QObject *parent = nullptr);void working();};
//myworker.cpp
#include <QDebug>
#include <QThread>
#include "myworker.h"MyWorker::MyWorker(QObject *parent) : QObject(parent)
{}void MyWorker:: working(){// 在新线程中执行的代码for (int i = 0; i < 5; ++i) {qDebug() << "Worker thread is running... (" << i << ")";QThread::sleep(1); // 模拟耗时操作}}
//main.cpp
#include <QCoreApplication>
#include <QThread>
#include <QDebug>
#include "myworker.h"int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);QThread thread1;MyWorker worker;// 将 worker 对象移动到 thread1 线程中,以便 working 函数在 thread1 中执行worker.moveToThread(&thread1);// 连接 QThread 的 started 信号到 MyWorker 的 working 槽函数// 当 thread1 启动时,worker 的 working 函数会被调用QObject::connect(&thread1, &QThread::started, &worker, &MyWorker::working);// 启动 thread1 线程thread1.start();for (int i = 0; i < 5; ++i) {qDebug() << "Main thread is running... (" << i << ")";QThread::sleep(1); // 模拟耗时操作}thread1.wait();return a.exec();
}

运行结果:

Main thread is running... ( 0 )
Worker thread is running... ( 0 )
Worker thread is running... ( 1 )
Main thread is running... ( 1 )
Worker thread is running... ( 2 )
Main thread is running... ( 2 )
Worker thread is running... ( 3 )
Main thread is running... ( 3 )
Main thread is running... ( 4 )
Worker thread is running... ( 4 )

优缺点分析

  • 优点

    • 将业务逻辑与线程控制代码分离,提高了代码的复用性和可维护性。
    • 可以方便地在多个线程之间移动对象,实现复杂的线程间交互。
  • 缺点:实现相对复杂一些,需要额外管理对象在新线程中的生命周期和信号槽连接。

http://www.dtcms.com/wzjs/168336.html

相关文章:

  • 武汉手机网站建设市场千万不要学网络营销
  • 用网站做微信公众号成都最新热门事件
  • java网站开发数据库连接菏泽seo
  • 网站开发能不能用win7系统搜索引擎排名影响因素有哪些
  • 什么是网站子目录站长之家查询
  • 深圳专业网站优化公司报价防晒霜营销软文
  • 邢台同城交友鸡西网站seo
  • 传奇手游网页网站优化方案模板
  • 大型购物网站建设友情链接怎么弄
  • wordpress编辑和作者的权限区别郑州seo网站关键词优化
  • .net网站开发的例子搜索关键词然后排名怎样提升
  • asp网站手机模版推广策略及推广方式
  • win7版本的wordpress专业seo网络营销公司
  • 百度站长平台链接提交手机百度最新正版下载
  • 物联网设计seo目标关键词优化
  • 手机网站有什么区别沧州网站建设公司
  • 怎样用阿里云服务器做网站黑帽seo工具
  • 24什么网站建设软文代写费用
  • 管理咨询合同优化整站
  • 电子产品外观设计长春seo培训
  • 网站开发经验简历百度竞价排名是什么方式
  • jsp开发网站城关网站seo
  • 深圳建设公司网站semseo
  • 网站地址免费做国外网站
  • 网站备注查询合肥seo软件
  • 社区网站搭建aso关键词优化计划
  • 有网站代码怎么建站b站入口2024已更新
  • 吉林网站建设找哪家网上销售平台有哪些
  • 制作网站合同需注意爆款引流推广软件
  • 常州微网站建设信息推广的方式有哪些