zoukankan      html  css  js  c++  java
  • Qt线程—QThread的使用--run和movetoThread的用法

    Qt使用线程主要有两种方法:

    方法一:继承QThread,重写run()的方法

    QThread是一个非常便利的跨平台的对平台原生线程的抽象。启动一个线程是很简单的。让我们看一个简短的代码:生成一个在线程内输出"hello"并退出的线程。

     // hellothread/hellothread.h
     class HelloThread : public QThread
     {
         Q_OBJECT
     private:
         void run();
     };
    

    我们从QThread派生出一个类,并重新实现run方法。

    // hellothread/hellothread.cpp
     void HelloThread::run()
     {
          qDebug() << "hello from worker thread " << thread()->currentThreadId();
     }
    

    run方法中包含将在另一个线程中运行的代码。在本例中,一个包含线程ID的消息被打印出来。 QThread::start()将在另一个线程中被调用。

    int main(int argc, char *argv[])
     {
         QCoreApplication app(argc, argv);
         HelloThread thread;
         thread.start();
         qDebug() << "hello from GUI thread " << app.thread()->currentThreadId();
         thread.wait();  // do not exit before the thread is completed!
         return 0;
     }
    
    另一种方法:moveToThread的方法

    其实,这个方法太简单,太好用了。定义一个普通的QObject派生类,然后将其对象move到QThread中。使用信号和槽时根本不用考虑多线程的存在。也不用使用QMutex来进行同步,Qt的事件循环会自己自动处理好这个。

    /*!
    * file main.cpp
    *
    * Copyright (C) 2010, dbzhang800
    * All rights reserved.
    *
    */
    
    #include <QtCore/QCoreApplication> 
    #include <QtCore/QObject> 
    #include <QtCore/QThread> 
    #include <QtCore/QDebug> 
     
    class Dummy:public QObject 
    { 
        Q_OBJECT 
    public: 
        Dummy(QObject* parent=0):QObject(parent)     {} 
    public slots: 
        void emitsig() 
        { 
            emit sig(); 
        } 
    signals: 
        void sig(); 
    }; 
     
    class Object:public QObject 
    { 
        Q_OBJECT 
    public: 
        Object(){} 
    public slots: 
        void slot() 
        { 
            qDebug()<<"from thread slot:" <<QThread::currentThreadId(); 
        } 
    }; 
     
    #include "main.moc" 
     
    int main(int argc, char *argv[]) 
    { 
        QCoreApplication a(argc, argv); 
        qDebug()<<"main thread:"<<QThread::currentThreadId(); 
        QThread thread; 
        Object obj; 
        Dummy dummy; 
        obj.moveToThread(&thread); 
        QObject::connect(&dummy, SIGNAL(sig()), &obj, SLOT(slot())); 
        thread.start(); 
        dummy.emitsig(); 
        return a.exec(); 
    }
    

    运行结果,slot不在主线程

    main thread: 0x1a5c 
    from thread slot: 0x186c
    

    其基本用法包含下面几步:

        QThread *thread = new QThread;
    	threads *thread_slot = new threads;
    
    	//移动到另一线程
    	thread_slot->moveToThread(thread);
    	thread->start();
    
    	//两个类之间的相互通信
    	QObject::connect(this, SIGNAL(trans_signal()), thread_slot, SLOT(th_trans_code()));
    

    【更多参考】

  • 相关阅读:
    【学习总结】 小白CS成长之路
    Java程序员面试题收集(1)
    ECSTORE2.0 去页面底部版权
    vue-cli安装
    linux下安装nodejs
    Access denied for user 'root'@'localhost' (using password: YES)的解决
    想说的话
    十三:CSS之CSS的四种使用方法和优先级
    十二:CSS之基础语法
    十一:HTML之实现基本网页结构
  • 原文地址:https://www.cnblogs.com/ZY-Dream/p/10636614.html
Copyright © 2011-2022 走看看