QT概览之1-Qthread

  1. 需要实现虚函数run()
    The thread does not begin executing until start() is called.
    start() begins execution of the thread by calling run().
    run() The starting point for the thread. After calling start(), the newly created thread calls this function. The default implementation simply calls exec().
    A QThread object manages one thread of control within the program. QThreads begin executing in run(). By default, run() starts the event loop by calling exec() and runs a Qt event loop inside the thread.
    You can use worker objects by moving them to the thread using QObject::moveToThread().
Protected Functions:
 int exec()
 virtual void run()

Public Slots:
 void exit(int returnCode = 0)
 void quit()
 void start(QThread::Priority priority = InheritPriority)
 void terminate()
 class WorkerThread : public QThread
 {
     Q_OBJECT
     void run() override {
         QString result;
         /* ... here is the expensive or blocking operation ... */
         emit resultReady(result);
     }
 signals:
     void resultReady(const QString &s);
 };

 void MyObject::startWorkInAThread()
 {
     WorkerThread *workerThread = new WorkerThread(this);
     connect(workerThread, &WorkerThread::resultReady, this, &MyObject::handleResults);
     connect(workerThread, &WorkerThread::finished, workerThread, &QObject::deleteLater);
     workerThread->start();
 }

In that example, the thread will exit after the run function has returned. There will not be any event loop running in the thread unless you call exec().

  1. [slot] void QThread::terminate()
    erminates the execution of the thread. The thread may or may not be terminated immediately, depending on the operating system’s scheduling policies. Use QThread::wait() after terminate(), to be sure.
    Warning: This function is dangerous and its use is discouraged. The thread can be terminated at any point in its code path
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值