未检测到发射信号



我尝试从类中发出信号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()

它不会退出循环。

知道吗?

download()方法的问题是,它已经是一个同步方法。您不需要此事件循环。您已经在download()方法内的事件循环中完成了所有操作。

旁注:而且您似乎有一些内存泄漏,因为您在没有父级的情况下创建了QEventLoop,并且从未删除它。

更新#1:您的finished()事件没有由外部QEventLoop(ac)处理,因为在QEventLoop开始用exec()处理事件之前就已经发出了finished()信号。作为一个丑陋的解决方案,您可以在exec()之后用排队的QMetaObject::invokeMethod()(Qt::QueuedConnection)调用调用download()(但我不建议这样做)。

更新#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;
};

相关内容

  • 没有找到相关文章

最新更新