请问使用继承QThread 创建多线程任务,应该如何正确delete这个 new出来的对象

baidu_28726667 2019-05-21 03:56:16
#ifndef MYQTHREAD_H
#define MYQTHREAD_H

#include <QThread>

class MyQThread : public QThread
{
public:
MyQThread();

virtual void run();
};

#endif // MYQTHREAD_H



#include "myqthread.h"
#include <QDebug>

MyQThread::MyQThread()
{

}

void MyQThread::run()
{
qDebug()<<"MyQThread Running";
//delete(this);
deleteLater();
}


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QDebug"
#include "myqthread.h"

void cplusThread()
{
qDebug()<<"cplusThread Running";
}


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{
// MyQThread *pMyQThread = new MyQThread();
// pMyQThread->start();

std::thread t1(cplusThread);
t1.detach();
}



点击按钮一次就创建一条线程去执行任务,允许点击多次按钮创建多条线程,但是这个MyQThread 如果不用new创建, 直接就析构了, 用new的话,不知道如何回收..在run里面delete(this)会崩溃. 倒是使用 std::thread调用类外的函数,分离后可以一直愉快的跑.
...全文
460 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
baidu_28726667 2019-05-22
  • 打赏
  • 举报
回复
引用 8 楼 hzh_Beyond 的回复:
当然,你可以使用C++的方式,自己实现。CThread.
目前弄了两个来处理任务,一个 std::thread,一个QThread , 先测试一下. 谢谢
浮一大白开水 2019-05-22
  • 打赏
  • 举报
回复
当然,你可以使用C++的方式,自己实现。CThread.
浮一大白开水 2019-05-22
  • 打赏
  • 举报
回复
qt的一大核心价值就是信号槽,不想用就不要使用qt咯。
baidu_28726667 2019-05-22
  • 打赏
  • 举报
回复
引用 2 楼 走好每一步 的回复:
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();
}
https://doc.qt.io/qt-5/qthread.html
逃不掉的信号槽....
baidu_28726667 2019-05-22
  • 打赏
  • 举报
回复
引用 4 楼 hzh_Beyond 的回复:
可以主动调用exit() or quit(),或者线程run退出时,会触发finished()信号,从而自动释放对象,记得在析构函数中翻译相关资源。
信号槽还是逃不过啊...
浮一大白开水 2019-05-21
  • 打赏
  • 举报
回复
可以主动调用exit() or quit(),或者线程run退出时,会触发finished()信号,从而自动释放对象,记得在析构函数中翻译相关资源。
浮一大白开水 2019-05-21
  • 打赏
  • 举报
回复
QThread will notifiy you via a signal when the thread is started() and finished(), or you can use isFinished() and isRunning() to query the state of the thread. You can stop the thread by calling exit() or quit(). In extreme cases, you may want to forcibly terminate() an executing thread. However, doing so is dangerous and discouraged. Please read the documentation for terminate() and setTerminationEnabled() for detailed information. connect(workerThread, &WorkerThread::finished, workerThread, &QObject::deleteLater);
走好每一步 2019-05-21
  • 打赏
  • 举报
回复
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();
}
https://doc.qt.io/qt-5/qthread.html
走好每一步 2019-05-21
  • 打赏
  • 举报
回复
看到楼主这样提问,觉得楼主需要恶补下c++的知识。 《windows核心编程》好好看下。

16,229

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧