2013-04-14 53 views
1

来源:https://mega.co.nz/#!xUMiSRYC!Y8Sxz2fEFb6yEIrnKiGx9n2zeK4YTUwUrCByaAkcOPI线程拒绝退出

我遇到的QThread :: currentThread()的一个问题 - )>退出(;

如果你打开我的myobject.cpp并转到第13行,quit()调用应该退出。但似乎quit()调用不起作用,它只是跳过它。看不到问题。 :(

main.cpp中:

#include "mainwindow.h" 
#include <QApplication> 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    MainWindow w; 
    w.show(); 

    QObject::connect(&a, SIGNAL(aboutToQuit()), 
        &w, SLOT(cleanUp())); 

    return a.exec(); 
} 

mainwindow.cpp:

#include "mainwindow.h" 
#include "ui_mainwindow.h" 
#include "myobject.h" 
#include <QMessageBox> 

MyObject* cObject; 

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

    cThread = new QThread(this); 
    cObject = new MyObject(); 
    cObject->moveToThread(cThread); 

    QObject::connect(ui->pushButton_2, SIGNAL(clicked()), 
        this, SLOT(close())); 

    QObject::connect(cThread, SIGNAL(started()), 
        cObject, SLOT(doWork())); 

    QObject::connect(ui->pushButton, SIGNAL(clicked()), 
        this, SLOT(runThreadSlot())); 

    QObject::connect(cThread, SIGNAL(finished()), 
        cThread, SLOT(deleteLater())); 

    QObject::connect(cThread, SIGNAL(finished()), 
        cObject, SLOT(deleteLater())); 

    QObject::connect(cObject, SIGNAL(setStatusBarSignal(QString)), 
        this, SLOT(setStatusBarSlot(QString))); 

    ui->lineEdit->setValidator(new QIntValidator(this)); 
    ui->lineEdit->setFocus(Qt::OtherFocusReason); 
    ui->statusBar->showMessage("Waiting for input..."); 
} 

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

void MainWindow::runThreadSlot() 
{ 
    cThread->start(); 
    // I've trimmed some code here. 
} 

void MainWindow::setStatusBarSlot(QString text) 
{ 
    ui->statusBar->showMessage(text); 
} 

void MainWindow::cleanUp() 
{ 
    qDebug() << "Cleaning up!"; 
    cObject->stage = 0; 
    cThread->wait(); 
} 

myobject.cpp:

#include "myobject.h" 
#include <QPixmap> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/imgproc/imgproc.hpp> 

MyObject::MyObject(QObject *parent) : 
    QObject(parent) 
{ 
} 

void MyObject::doWork() 
{ 
    QThread::currentThread()->quit(); // It doesn't stop here. This is my issue. 
    // I've trimmed the code here. 
} 

mainwindow.h:

#ifndef MAINWINDOW_H 
#define MAINWINDOW_H 

#include <QMainWindow> 

namespace Ui { 
class MainWindow; 
} 

class MainWindow : public QMainWindow 
{ 
    Q_OBJECT 

public: 
    explicit MainWindow(QWidget *parent = 0); 
    ~MainWindow(); 

private slots: 
    void runThreadSlot(); 
    void setStatusBarSlot(QString); 
    void cleanUp(); 

private: 
    Ui::MainWindow *ui; 
    QThread* cThread; 
}; 

#endif // MAINWINDOW_H 

myobject.h:

#ifndef MYOBJECT_H 
#define MYOBJECT_H 

#include <QtCore> 

class MyObject : public QObject 
{ 
    Q_OBJECT 
public: 
    explicit MyObject(QObject *parent = 0); 
    QFile path; 
    qint32 waitSecs; 
    int stage; 

signals: 
    void setStatusBarSignal(QString); 

public slots: 
    void doWork(); 

}; 

#endif // MYOBJECT_H 
+0

你应该发布你的代码,而不是提供一个链接来下载它 – ashley

+0

@ashley:它需要文件,但资源。 – user2279882

+0

您应该在此处发布代码的_relevant_部分,或者更好地发布[SSCCE](http://sscce.org/)。这里没有人会通过那个盲目的链接并从那个可疑的来源下载。 – Mat

回答

1

QThread::quit()是异步的。你退出后立即添加QThread::currentThread()->wait()