I have a Qt console application. In this application, there is an object of type "my_client". "my_client" objects have an slot named "messageSlot". This slot is connected to a DBUS message.
So the main function of this qt app is as follows :
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
if (!QDBusConnection::sessionBus().isConnected())
{
fprintf(stderr, "Cannot connect to the D-Bus session bus.\n"
"To start it, run:\n"
"\teval `dbus-launch --auto-syntax`\n");
return 1;
}
if (!QDBusConnection::sessionBus().registerService("org.brlapi.server"))
{
fprintf(stderr, "%s\n",
qPrintable(QDBusConnection::sessionBus().lastError().message()));
std::cout << qPrintable(QDBusConnection::sessionBus().lastError().message())
<< std::endl;
exit(1);
}
my_client client;
new myClassAdaptor(&client);
QDBusConnection::sessionBus().registerObject("/", &client);
QDBusConnection::sessionBus().connect("org.my.server",
"/",
"org.my.server",
"message",
&client,
SLOT(my_client::messageSlot(QString, QString)));
return a.exec();
}
And my_client is as follows :
class my_client : public QObject
{
Q_OBJECT
public:
my_client()
{
}
private slots:
void messageSlot(QString msg1, QString msg2)
{
std::cout << "CLIENT : I've received a message : " << msg1.toStdString()
<< "," << msg2.toStdString() << std::endl;
QDBusMessage msg = QDBusMessage::createSignal("/", "org.my.server", "message");
msg << "Hello!!!" << "Are you ok?";
QDBusConnection::sessionBus().send(msg);
}
};
But with this code I can't receive DBUS message. I think that the problem is that client object hasn't a main loop and therefore can't receive signals.
Am I right? If so, how can we add client object to main loop? and if not, what is the problem with this code? how can I receive dbus messages and handle them with QT signal/slot?