我有一个发出 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()));
如果存在,内存处理程序将删除发送者。这种方法有什么缺点,什么可能更好?
请注意,对象在完成时只会发出这些信号之一!