My goal is to create a library using the Qt's DBus bindings.
I tried to create a Qt application without launching the QEventLoop
(provided by the QCoreApplication
class) in the main thread.
Here is a minimalistic application sample, working fine using QT-4.6.2 version but blocking on introspection using QT-4.8 or higher.
DBusHandler.hpp
#pragma once
#include <iostream>
#include <QtCore/QThread>
#include <QtCore/QtCore>
#include <QtDBus/QDBusInterface>
class DBusHandler : public QThread
{
Q_OBJECT;
private:
void run(void)
{
QDBusConnection connection = QDBusConnection::sessionBus();
connection.registerService("my.qdbus.example");
connection.registerObject("/", this, QDBusConnection::ExportAllSlots);
exec();
}
public:
DBusHandler(void) {}
virtual ~DBusHandler(void) {}
void stop(void)
{
QDBusConnection connection = QDBusConnection::sessionBus();
connection.unregisterObject("/");
connection.unregisterService("my.qdbus.example");
connection.disconnectFromBus(connection.name());
QThread::quit();
}
public slots:
void remoteCall(QByteArray message)
{
std::cout << "Message size: " << message.size() << std::endl;
}
};
main.cpp
#include "DBusHandler.hpp"
int main(int ac, char **av)
{
QCoreApplication app(ac, av);
DBusHandler handler;
handler.moveToThread(&handler);
handler.start();
while (not handler.isRunning());
// app.exec();
sleep(10); // Gives time to call using the command line: "qdbus my.qdbus.example / local.DBusHandler.remoteCall a_message"
handler.stop();
while (handler.isRunning());
}
As you can see in the main.cpp
file, app.exec()
is commented out, but makes the application working fine on QT-4.8 or higher versions (5.3.0).
My question is the following: Is it possible to use the Qt's DBus bindings calling app.exec()
in an other thread than the main one, on Qt-4.8 or 5.3 ?
app.exec()
call in the main thread on Qt-4.8 or 5.3 ?". Shouldn't it be with, according to what he has demonstrated? – Subheadapp.exec()
in the main thread, the code works with Qt 4.6.2. And the solution expected is some way to use Qt EventLoops without blocking the main thread. Isn't it possible to use Qt as a library starting at version 4.8.4 without blocking the main thread and using QEventLoops (to make signals/slots working) ? – Roomerapp.exec()
call in the main thread". Sorry I didn't mean to play on words, I just want to make sure what the problem is. Because the case withoutapp.exec()
has already been demonstrated and worked on 4.8, why should we still ask for without again? – Subhead