Qt World Summit: Submit your Presentation

Problems connecting to a broker with QtMqtt

  • Hi,

    i'm currently trying to implement a small command line Mqtt-client with QtMqtt. All examples (e.g. "simpleclient") connect absolutely flawlessly to my local mosquitto-server. I'm using Qt 5.10.1 with Ubuntu 18.04.

    I used the code from "simpleclient" as base for my client but own code does not connect to the server. It stays in stage 1 and subscriptions are not possible. Here is a snippet from my code:

    DummyPlugin::DummyPlugin() {
        qDebug() << "Initialising DummyPlugin ...";
        m_client = new QMqttClient(this);
        connect(m_client, &QMqttClient::stateChanged, this, &DummyPlugin::updateLogStateChange);
        QString hostname = QString("");
        int port = 1183;
        connect(m_client, &QMqttClient::messageReceived, this, [this](const QByteArray &message, const QMqttTopicName &topic) {
                const QString content = QDateTime::currentDateTime().toString()
                            + QLatin1String(" Received Topic: ")
                            + topic.name()
                            + QLatin1String(" Message: ")
                            + message;
                qDebug() << content;
    QString DummyPlugin::getPluginName() {
        return "org.projekt-bienhaus.DummyPlugin";
    void DummyPlugin::connectToServer() {
        qDebug() << "Connecting to server: " << m_client->hostname();
        /*auto subscription = m_client->subscribe(QString("myTopic"),0);
        if(!subscription) {
            qDebug() << "Not subscribed!";
    void DummyPlugin::updateLogStateChange()
        const QString content = QDateTime::currentDateTime().toString()
                        + QLatin1String(": State Change ")
                        + QString::number(m_client->state());
        qDebug() << content;
    QCoreApplication a(argc, argv);
    PluginLoader loader;
        QList<PluginInterface *> listOfPlugins = loader.loadPlugin();
        foreach(auto plugin, listOfPlugins) {

    Does anybody know what i'm doing wrong?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Might be silly question but are you sure your plugin is loaded correctly ?

  • Yes, the plugin loads correctly. Before using QtMtqq i tested the plugin system with a QTimer which outputs a string every 2 seconds.

  • @glasen77 not that I'm an expert with MQTT protocol, but comparing your client with Qt simpleclient example, it seems that you're not subscribing to any topic at all.
    From this tutorial it looks like even if you connect successfully to the MQTT broker (or server) then you need to register your client to "publish" or "subscribe" for a particular topic.

  • The problem is that i can't subscribe to any topic because the connection is not completed. Is it possible that i do set the hostname in a wrong way? Or can i only use QtMqtt with QApplication and not QCoreApplication?

    The only difference between my code and the simpleclient example is that my code only uses the command line.

  • Lifetime Qt Champion

    You should also connect the QMqttClient::errorChanged signal to see if you have something going wrong.

  • @SGaist : Also tried that. "errorChanged" does nothing. I've completely run out of ideas.

  • Lifetime Qt Champion

    So basically your sample stays in
    QMqttClient::Connecting state ?

    do you have multiple clients ?
    seems you have multiple plugins.
    you do follow the rule that docs says:
    "The connection request must contain a unique client identifier. "

  • @mrjj :
    At the moment there is only one client running. And setting an identifier is optional because the class will do that for you:

    Each client needs to have a unique ID to be able to connect to an MQTT broker. If no client ID is specified by the user, one will be generated automatically when a connection is established.

    I can run multiple versions of the "simpleclient" example without specifying an identifier.

  • @glasen77 ok, so at this point another difference I see is that you're using your code as a plugin vs the simpleclient being just an app.
    Is there a real need to use a plugin?
    Could you try your code as a regular app to see if it works? This way you could discard coding errors regarding network and just focus on the plugin side...

  • @Pablo-J.-Rogina :

    Yes, i need to use a plugin. But i solved the problem:

    After playing around with the Unit-tests of QtMqtt i found out that you explicitly need to inherit from QObject for your Mqtt-Class. Because i'm already inheriting from QObject in the interface for my PluginClass, it was not possible to inherit another time (Don't ask me why, i'm new to C++).

    I solved my problem by making another class named MqttWrapper which calls all the code to establish the connection to the broker. This class is called from my DummyPlugin-Class. Now the code works perfectly.

Log in to reply