QThread:在线程仍在运行时被销毁 - 由QPushbutton调用

时间:2015-01-25 17:26:01

标签: c++ qt

我几个小时试图将此错误减少到一个最小的例子,但无法成功。

我有一个QPushButton importPCDButton的GUI。它应该打开QFileDialog并导入所选文件。我想使用一个线程,希望一旦我向GUI发送更新信号,对话框就会消失。所以我有一个ImportPCD工人类派生自QObject,实现名为process()的公共广告位。在这里,我打开QFileDialog等等:

 void ImportPCD::process() {
     std::cout << "foo \n";
     QString file_abs = QFileDialog::getOpenFileName(
         &*(getControl()->getGuiPtr()),
         tr("OpenFile"),
         "../data/",
         tr("Point Cloud File(*pcd);;ASCII - File(*.asc);;All Files(*)"));
     int index = file_abs.lastIndexOf("/");
     int size = file_abs.size();
     int position = size-index-1;    
     QString file = file_abs.right(position);
     index = file.indexOf(".");
     file = file.left(index);
     getControl()->setTreeID(file.toStdString());
     QString abort;
     if(file!=abort)
     {
         this->path = file_abs.toStdString();

         cloudRGB = import ();
         emit updateUI();
         computeNormals (cloudRGB);
         emit updateUI();
         principalCurvatures = computeCurvature ();

         setRGB ();
         getControl()->setCloudPtr(getCloudRGB());
    }

    emit finished();
}

它还实现了广告位updateUI()finished()

我的GUI中有一个公共广告位importPCDFile()

void PCLViewer::importPCDFile() {
    boost::shared_ptr<QThread> thread_ptr (new QThread);
    boost::shared_ptr<ImportPCD> importWorker (new ImportPCD(control));
    importWorker->moveToThread(&*thread_ptr);
//  connect (ui->importPCDButton  , SIGNAL(clicked()), &*thread_ptr, SLOT(start()));
    connect (&*thread_ptr, SIGNAL(started()), &*importWorker, SLOT(process()));
//  connect (ui->importPCDButton  , SIGNAL(clicked()), &*importWorker, SLOT(process()));
    connect (&*importWorker, SIGNAL(updateUI()), this, SLOT(updateUI()));
    connect (&*importWorker, SIGNAL(finished()), this, SLOT(updateUI()));
    connect (&*importWorker, SIGNAL(finished()),&*thread_ptr, SLOT(quit()));
//  connect (&*importWorker, SIGNAL(finished()),&*thread_ptr, SLOT(quit()));
    connect (&*thread_ptr, SIGNAL(finished()), &*thread_ptr, SLOT(deleteLater()));
//  connect (&*thread_ptr, SIGNAL(finished()), &*importWorker, SLOT(deleteLater()));
    thread_ptr->start();

}

当我按下按钮时调用此方法,我收到错误:

QThread: Destroyed while thread is still running

0 个答案:

没有答案