Sorry, here is some pieces of code :
I use Dbus in a QThread to use its own even loop (As I implement a library, I don't want any blocking call, such exec(), in the main thread).
So, I initialize it in this way:
Worker::Worker()
{
moveToThread(_thread);
_dbus = new DbusHandler();
_dbus->moveToThread(_thread);
_thread->start();
connect(_thread, SIGNAL(started()), _dbus, SLOT(initializeDBus()));
connect(this,
SIGNAL(sig_send
(const QString)), _dbus,
SLOT(send
(const QString)));
}
Worker::Worker()
{
moveToThread(_thread);
_dbus = new DbusHandler();
_dbus->moveToThread(_thread);
_thread->start();
connect(_thread, SIGNAL(started()), _dbus, SLOT(initializeDBus()));
connect(this, SIGNAL(sig_send(const QString)), _dbus, SLOT(send(const QString)));
}
To copy to clipboard, switch view to plain text mode
void DBusHandler::initializeDBus()
{
if ( ! _connection.isConnected())
throw runtime_error("Could not connect to DBUS.");
// register the serviceName
if ( not _connection.registerService(serviceName) )
throw runtime_error("Could not register the service using QDBusConnection::registerService");
// register the current object to the path "/"
if ( not _connection.
registerObject("/",
this,
QDBusConnection::ExportAllSlots)) throw runtime_error("Could not register object using QDBusConnection::registerObject");
}
void DBusHandler::initializeDBus()
{
QDBusConnection _connection = QDBusConnection::sessionBus();
if ( ! _connection.isConnected())
throw runtime_error("Could not connect to DBUS.");
// register the serviceName
if ( not _connection.registerService(serviceName) )
throw runtime_error("Could not register the service using QDBusConnection::registerService");
// register the current object to the path "/"
if ( not _connection.registerObject("/", this, QDBusConnection::ExportAllSlots))
throw runtime_error("Could not register object using QDBusConnection::registerObject");
}
To copy to clipboard, switch view to plain text mode
Then I send the message on Dbus using the same thread using this slot connected to the signal sig_send() of a Worker class (which is in the same thread).
void DBusHandler
::send(const QString message
) {
interface.asyncCall(serviceName, message);
}
void DBusHandler::send(const QString message)
{
QDBusInterface interface(serviceName, "/", "", QDBusConnection::sessionBus());
interface.asyncCall(serviceName, message);
}
To copy to clipboard, switch view to plain text mode
This method allows to emit sig_send signal and send the message to the above method.
void Worker
::send(const QString message
) {
emit sig_send(message);
}
void Worker::send(const QString message)
{
emit sig_send(message);
}
To copy to clipboard, switch view to plain text mode
Finally my main is:
int main(int , char** )
{
Worker* worker = new Worker();
while(true)
worker->send("Hello world!");
}
int main(int , char** )
{
Worker* worker = new Worker();
while(true)
worker->send("Hello world!");
}
To copy to clipboard, switch view to plain text mode
With this code, Dbus is blocking on QDBusInterface introspection (as explained in the previous post)
Strangely, this code seems to work well with Qt 4.6.2 but not with Qt 4.8.4. More amazingly, it works also if I remove the infinite loop in the main and send only one message.
Does anyone have a solution or an explanation to this problem?
Thanks in advance.
Bookmarks