我试图从班级发出信号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
)处理,因为finished()
信号在QEventLoop甚至开始使用exec()
处理事件之前就已发出。
作为一个丑陋的工作环境,您可以在download()
之后通过排队的exec()
(Qt :: QueuedConnection)调用来调用QMetaObject::invokeMethod()
(但我不推荐这样做)。
更新#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;
};
关于c++ - 未检测到发射信号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36396730/