Есть прокся, инкапсулирует в себе работу N менеджеров.
Надо в проксю передать сторонний объект и его слот, для подписки на сигнал от конкретного менеджера.
Что то я завис.
#include <QCoreApplication>
#include <QVector>
#include <QDebug>
#include <QUuid>
class Manager : public QObject
{
public:
explicit Manager(QObject* p = nullptr) : QObject(p){}
~Manager() = default;
signals:
void finished(const QUuid& id);
};
class Proxy : public QObject
{
public:
explicit Proxy(ushort count, QObject* p = nullptr)
: QObject(p)
{
while (count--)
m_managers.push_back(new Manager(this));
}
~Proxy() = default;
template<typename Slot> void subscribeFinished(const QObject* receiver, Slot slot, ushort index)
{
if (m_managers.size() <= index)
return;
QObject::connect(m_managers[index], &Manager::finished, receiver, slot);
}
private:
QVector<Manager*> m_managers;
};
class Worker : public QObject
{
public:
explicit Worker (QObject* p = nullptr) : QObject(p){}
~Worker() = default;
void doWork()
{
m_proxy = new Proxy(4, this);
m_proxy->subscribeFinished(this, &Worker::onFinished, 2);
}
private slots:
void onFinished(const QUuid& id)
{
qDebug() << id;
}
private:
Proxy* m_proxy;
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Worker worker(&a);
worker.doWork();
return a.exec();
}
На строке
QObject::connect(m_managers[index], &Manager::finished, receiver, slot);
main.cpp:35: error: invalid conversion from ‘const QObject*’ to ‘const Object*’ {aka ‘const Worker*’} [-fpermissive]
QObject::connect(m_managers[index], &Manager::finished, receiver, slot);
^~~~~~~~