我有一个指向类型X的指针的QVector,其项目,即X自己的QProcesses。这些过程可以在任意时间终止。现在,我在进程结束时在X类中构建了信号槽连接。但是,我想将它传播到具有X * QVector作为成员的处理程序类。这样做的优雅方式是什么?
答案 0 :(得分:2)
您可以将信号连接到信号,将源信号隐藏为实现细节:
class MyInterface : public QObject {
Q_OBJECT
...
QProcess m_process;
public:
Q_SIGNAL void processEnded();
MyInterface(QObject * parent = 0) : QObject(parent) {
connect(&QProcess, &QProcess::finished, this, &MyInterface::processEnded);
...
}
};
处理程序类可以侦听这些信号并对它们执行某些操作。
class Handler : public QObject {
Q_OBJECT
QVector<MyInterface*> m_ifaces; // owned by QObject, not by the vector
void addInterface(MyInterface* ifc) {
ifc->setParent(this);
connect(ifc, &MyInterface::processEnded, this, [this, ifc]{
processEnded(ifc);
});
m_ifaces.append(ifc);
}
void processEnded(MyInterface* ifc) {
// handle the ending of a process
...
}
...
};