2016-04-04 92 views
0

我試圖從我的班級發出信號finished()。但是當我將信號連接到我的插槽時,它什麼也沒有做。未檢測到發射信號

我的班級的名字是blend_install,我宣佈它爲blendinstaller並試圖將其連接到QEventLoop。

.... 
QEventLoop ac; 
connect(&blendinstaller, SIGNAL(finished()), &ac, SLOT(quit())); 

blendinstaller.show_progress(); 
blendinstaller.download(); // this will execute everything and in the end emit finished() 

ac.exec(); 
.... 

download()功能:

current_prog = BLEND_INSTALL_NONE; 
emit progress_changed(current_prog); 

manager = new QNetworkAccessManager; 

file_handler = new QFile(downloadTo); 

file_handler->open(QFile::WriteOnly); 
.... handle error .... // each of this (error handling) will emit finished() signal and return; 

.... // each of this will represent the process of reporting event changes (for logging), emit a SIGNAL() 

QNetworkRequest request; 
request.setUrl(QUrl(downloadFrom)); 

reply = manager->get(request); 
event = new QEventLoop; 
connect(reply,SIGNAL(finished()),event,SLOT(quit())); 
connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(downloadError(QNetworkReply::NetworkError))); 
connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadProgressL(qint64,qint64))); 


event->exec(); 

.... handle error .... 

.... write reply.readAll() to file .... 

.... 

// these are instruction for a custom QProcess instance 
proc.setProgram(extractWith); 
proc.setArguments(ar); 
proc.setWorkingDirectory(downloadIn); 

event = new QEventLoop; 
connect(&proc,SIGNAL(finished(int)),event,SLOT(quit())); 
connect(&proc,SIGNAL(error(QProcess::ProcessError)),this,SLOT(extractError(QProcess::ProcessError))); 
connect(&proc,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(extractFinished(int,QProcess::ExitStatus))); 

proc.start(); 
proc.open_console(); 

event->exec(); 

.... handle error .... 

.... 

.... attempt to find output of QProcess (extract an archive) .... 

.... handle error, output of QProcess not found .... 

.... 

emit installed(installOn); 
emit finished(); // the SIGNAL I want to get. 

qDebug("It's finished installing!"); 

所以,TL; DR每個錯誤處理將返回從功能,而且還發射finished()和在功能結束(假定沒有錯誤)它會發出finished()

它不會退出循環。

有什麼想法?

+2

它看起來罰款。你可以發佈你的'download()'實現嗎?我只能想象你的'download()'方法過早地發出'finished()'信號。更好地說:它不應該從它被調用。通常某種專用插槽'onDownloadComplete()'會發出'finished()'信號。 – mfreiholz

+0

@mfreiholz我已經上傳了,我會嘗試你的建議... –

+0

它總是打印「它已經完成安裝!」消息,但仍不會發出'finished()'信號。 –

回答

1

您的download()方法的問題是,它已經是一個同步方法。你不需要這個事件循環。您已經在download()方法中的事件循環中做了所有事情。

附註:你似乎有一些內存泄漏,因爲你創建QEventLoop沒有父母,並從不刪除它。

更新#1:finished()事件不是由外QEventLoop(ac)處理,因爲finished()信號時發出的QEventLoop甚至開始處理事件與exec()之前。 作爲一個醜陋的workarround你可以調用download()exec()與排隊QMetaObject::invokeMethod()(Qt :: QueuedConnection)調用(但我不會推薦它)。

更新#2 這裏是一個小例子,這是不完美的,當然:P

class BlendDownloader 
{ 
    Q_OBJECT 

public: 
    BlenDownloader() : 
    _file(NULL) 
    { 
    } 

    void download() 
    { 
    _file = new QFile("C:/myfile.blubb"); 

    QNetworkRequest req("your url here"); 
    QNetworkReply* reply = _mgr.get(req); 
    QObject::connect(reply, SIGNAL(finished()), this, SLOT(onDownloadFinished())); 
    // TODO: Also handle error callback here 
    } 

private slots: 
    void onDownloadFinished() 
    { 
    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender()); 
    reply->deleteLater(); 

    // Write response data to file. 
    // Note: You might get problems with big files, 
    // since this buffers the entire response of QNetworkReply 
    // in internal buffer. It's better to use the "readyRead()" 
    // signal and write incrementally. 
    _file->write(reply->readAll()); 

    // Do your parsing stuff now and emit "finished()" at the end. 
    // ... parsing, validation here ... 

    // Clean up 
    _file->close(); 
    delete _file; 
    _file = NULL; 

    emit finished(); 
    } 

private: 
    QNetworkManager _mgr; 
    QFile* _file; 
}; 
+0

這是一堆' QEventLoop'中的QEventLoop',因爲我需要它等到一切都完成了。但這可能是因爲內存泄漏。我在我班的私人部分聲明瞭QEventLoop,那是不是很糟糕? –

+0

@TitoNovelianto當你發出一個'完成的'信號時,爲什麼你需要等到一切都完成?只需將'finished'信號連接到某個位置,然後在'loop-> exec'之後執行所做的操作(這看起來好像你幾乎已經在做)。 – thuga

+0

我用一個小例子更新了我的答案(更新#2) – mfreiholz