是否可以以仅当所有相关信号都发出后才调用插槽的方式将一个以上的信号连接到插槽?
答案 0 :(得分:0)
使用计数器:
#include <QCoreApplication>
#include <QTimer>
#include <QDebug>
class Task: public QObject{
Q_OBJECT
public:
using QObject::QObject;
void start(int timeout=1000){
QTimer::singleShot(timeout, this, &Task::completed);
}
signals:
void completed();
};
class Manager: public QObject
{
Q_OBJECT
Q_PROPERTY(int numberOfTasks READ numberOfTasks WRITE setNumberOfTasks)
public:
Manager(QObject *parent=nullptr):
QObject(parent), counter(0){}
int numberOfTasks() const{
return m_numberOfTasks;
}
void setNumberOfTasks(int numberOfTasks){
m_numberOfTasks = numberOfTasks;
}
public slots:
void next(){
counter++;
if(counter == m_numberOfTasks){
emit finished();
}
}
signals:
void finished();
private:
int counter;
int m_numberOfTasks;
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Manager manager;
manager.setNumberOfTasks(4);
for(int i=0; i< manager.numberOfTasks(); ++i){
Task *task = new Task(&manager);
QObject::connect(task, &Task::completed, &manager, &Manager::next);
task->start((i+1)*1000);
}
QObject::connect(&manager, &Manager::finished, [](){
qDebug()<< "finished";
QCoreApplication::quit();
});
return a.exec();
}
#include "main.moc"
答案 1 :(得分:0)
您可以先将所有信号发射到另一个插槽,然后在该插槽中接收所有信号,并在所有信号到达时最终将信号发射到您想要的插槽。