I have a class ConnectionManager
with method get_wifi_ssids()
that must return a list of SSIDs. The problem is that to get those SSIDs signals and slots need to be used, but I can't figure out a way to retrieve that information without first exiting the method.
Here is the hierarchy of the classes used from lowest-level to highest.
/** Controls wireless network card by commanding a software component "connman" via DBus. */
class WifiController : QObject {
Q_OBJECT
public:
void scan();
}
/** Low level interface to network interfaces. */
class NetworkController : QObject {
Q_OBJECT
public:
void scan_for_wifi() {
wifi_controller.scan();
// When scan is finished it sends the
// NetworkTechnology::scanFinished signal.
}
// Gets info from cache. This cache is updated when a `scan()` happens.
QList<AccessPointInfo> get_available_access_points;
private:
WifiController wifi_controller;
}
/** High level interface to network interfaces. */
class ConnectionManager {
public:
QList<QString> get_wifi_ssids() {
netCtrlr.scan();
// PROBLEM HERE: How do I wait for the `scanFinished` signal here, then
// continue execution and return the SSIDs from the recently-updated
// cache?
QList<AccessPointInfo> APs { netCtrlr.get_available_access_points() };
QList<QSitrng> ssids { parseAPInfo(APs) };
return ssids;
}
private:
NetworkController netCtrlr;
}
The entirety of my application is in a single thread. "connman" is commanded by WifiConroller
via DBus, and it is a separate process so obviously in a separate thread. The GUI runs in a separate process and my app communicates with it via DBus.
A QEventLoop
is a bad solution because it's not meant to be used in production and is more of a hack, according to the comments in this answer.