gpt4 book ai didi

c++ - 未检测到发射信号

转载 作者:行者123 更新时间:2023-11-28 05:45:00 24 4
gpt4 key购买 nike

我试图从我的类(class)发出信号 finished()。但是当我将信号连接到我的插槽时,它什么也没做。

我的类(class)名称是 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() 处理事件。作为一个丑陋的解决方法,您可以在 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;
};

关于c++ - 未检测到发射信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36396730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com