How to emit a signal passed to a function
I am passing a signal to a method using the SIGNAL macro, and at some point I need to emit that signal.
Something like the below:
Code:
void Network::get(const QNetworkRequest &request, const char *signal)
{
QNetworkReply *reply = m_manager->get(request);
m_requests.insert(reply, signal);
}
the Network class encapsulates QNetworkAccessManager, so in the method above m_manager is QNetworkAccessManager.
The Network class has the following signal/slot connection:
Code:
connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onResponse(QNetworkReply*)));
the onResponse SLOT is:
Code:
void Network::onResponse(QNetworkReply *reply)
{
const char *signal = m_requests.take(reply);
// emit signal somehow
qDebug() << "and the signal is: " << signal;
}
m_request is: QHash<QNetworkReply *, const char *> m_requests;
The idea is that various other classes of the application can use the Network class to make http requests. Each of those classes calls void Network::get(const QNetworkRequest &request, const char *signal) passing its own signal that should be emitted once the request is finished.
I prototype a small concept of the application using PySide and this mechanism of passing the signal worked perfectly.
But in the C++ version the return of the SIGNAL macro is a const char *, is there a way i can turn the const char * back to an emittable signal?
Re: How to emit a signal passed to a function
Why are you trying to reinvent this wheel? Each caller of QNetworkAccessManager::get() is given a QNetworkReply object. They can connect that object's QNetworkReply::finished() signal to any slot they want executed, or to any signal they want emitted, independent of any other reply object.
Re: How to emit a signal passed to a function
I am not trying to reinvent the wheel, I just did not realized that QNetworkReply is also emitting the finished signal.
Re: How to emit a signal passed to a function
In case you ever need this in a different context, don't pass a signal but a slot.
If you have receiver object and a slot name you can easily use QMetaObject::invokeMethod() to call that slot.
Even with arguments.
Cheers,
_
Re: How to emit a signal passed to a function
If I'm not mistaken a signal is just a member function, so you should able to store pointers to the signals instead of their const char* representation.
See this for information: http://stackoverflow.com/a/15176860/4206247
You also need the instance of the QObject that is emitting it.