gpt4 book ai didi

c++ - 在单独的插槽中删除发件人

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:47:34 25 4
gpt4 key购买 nike

我有一个发出 3 种不同信号的对象,我想传输这些信号并删除它们的发送者。该对象位于另一个对象中,该对象将充当这些信号的发射器。

通常我会这样:

void SomeClass::someSideFunction() {
Request* request = _StorageProvider.insert("somedata");

connect(request, SIGNAL(succeded()), this, SLOT(handleSucceded()));
connect(request, SIGNAL(failed()), this, SLOT(handleFailed()));
connect(request, SIGNAL(alreadyExists()), this, SLOT(handleAlreadyExists()));
}

void SomeClass::handleSucceded() {
Request* request = qobject_cast<Request*>(sender());
if(request != NULL) request ->deleteLater();
emit succeded();
}

void SomeClass::handleFailed() {
Request* request = qobject_cast<Request*>(sender());
if(request != NULL) request ->deleteLater();
emit failed();
}

void SomeClass::handleAlreadyExists() {
Request* request = qobject_cast<Request*>(sender());
if(request != NULL) request ->deleteLater();
emit alreadyExists();
}

有更好的方法吗?即使“请求”是 storageProvider 的子项,我也有很多请求可以等待删除它们,直到父项死亡。

我在想这样的解决方案:

connect(request, SIGNAL(succeded()),        this, SIGNAL(succeded()));
connect(request, SIGNAL(failed()), this, SIGNAL(failed()));
connect(request, SIGNAL(alreadyExists()), this, SIGNAL(alreadyExists()));
connect(request, SIGNAL(succeded()), this, SLOT(memoryHnadler()));
connect(request, SIGNAL(failed()), this, SLOT(memoryHnadler()));
connect(request, SIGNAL(alreadyExists()), this, SLOT(memoryHnadler()));

内存处理程序将删除发件人(如果存在)的位置。这种方法的缺点是什么,什么可能更好?

请注意,对象在完成时只会发出这些信号之一!

最佳答案

一个选项是让请求使用 QObject::deleteLater() 安排它自己的删除。像这样:

void Request::emitSucceeded() {
emit succeeded();
deleteLater();
}

void Request::emitFailed(int errorCode, const QString& errorString ) {
m_errorCode = errorCode;
m_errorString = errorString;
emit failed();
deleteLater();
}

例如,KDELib 的异步操作基类,KJob , 正在使用此模式(除非通过 setAutoDelete(false) 明确禁用)。

关于c++ - 在单独的插槽中删除发件人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10577269/

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