emqx / qmqtt

MQTT client for Qt
https://www.emqx.com
Other
698 stars 337 forks source link

Qmqtt can not connect to any ip address #211

Closed dbsxdbsx closed 4 years ago

dbsxdbsx commented 4 years ago

I've compiled it under vs2017_64 with qt 5.13.2. But in demo example, I just found it can never establish a connection.

part of my code:

#include "mainwindow.h"
#include "ui_mainwindow.h"

//#include<qnetwork.h>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    client = new QMQTT::Client();
    ui->pushButton->setEnabled(false);
    ui->pushButton_2->setEnabled(false);

    connect(client, &QMQTT::Client::connected, this, &MainWindow::doConnected);
    connect(client, &QMQTT::Client::disconnected, this, &MainWindow::doDisconnected);
    //connect(client, &QMQTT::Client::subscribed, this, &MainWindow::onSubscribed);
    connect(client, &QMQTT::Client::received, this, &MainWindow::doDataReceived);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pb_connect_clicked()
{

    if(!client->isConnectedToHost())
    {
        //client->setHost(QHostAddress(ui->lineEdit_ip->text()));
        //client->setPort(ui->lineEdit_port->text().toInt());
        client->setHost(QHostAddress("test.mosquitto.org"));
        client->setPort(1883);
        client->setClientId("MQTT_FX_Client");
        //client->setUsername("test");
        //client->setPassword("123456");
        client->connectToHost();
    }
    else {
        client->disconnectFromHost();
    }

}

void MainWindow::doConnected()
{
    ui->rb_status->setChecked(true);
    ui->pb_connect->setText("Disconnect");
    ui->pushButton->setEnabled(true);
    ui->pushButton_2->setEnabled(true);
}

void MainWindow::doDisconnected()
{
    ui->rb_status->setChecked(false);
    ui->pb_connect->setText("Connect");
    ui->pushButton->setEnabled(false);
    ui->pushButton_2->setEnabled(false);
}

void MainWindow::doDataReceived(Message message)
{
    QString mes = QString(message.id())+" "+QString(message.qos())+" "+message.topic()+" "+message.payload()+"\n";
    ui->te_log->append(mes);
}

void MainWindow::on_pushButton_clicked()
{
    QString topic = ui->le_pb_topic->text().trimmed();
    QString payload = ui->le_pu_payload->text().trimmed();
    if(topic.isEmpty() || payload.isEmpty())
    {
        qDebug()<<"pub topic and payload is empty!"<<endl;
        return;
    }
    QMQTT::Message message(136,topic,payload.toUtf8());

    client->publish(message);
}

void MainWindow::on_pushButton_2_clicked()
{
    QString topic = ui->le_sub_topic->text().trimmed();
    if(topic.isEmpty())
    {
        qDebug()<<"sub topic and payload is empty!"<<endl;
        return;
    }
    qDebug()<<topic<<endl;
    client->subscribe(topic);
}

void MainWindow::on_pushButton_3_clicked()
{
    QString topic = ui->le_sub_topic->text().trimmed();
    if(topic.isEmpty())
    {
        qDebug()<<"sub topic and payload is empty!"<<endl;
        return;
    }
    client->unsubscribe(topic);

}

I just don't know why it cannot establish connection--- Here doConnected is never touched

ejvr commented 4 years ago

Your problem is this: QHostAddress("test.mosquitto.org"). QHostAddress cannot convert a host name to an IP address. You should pass an IP-address to this constructor. If you want to use a hostname, you should use one of the QMQTT::Client constructors that have QString hostName as argument.

dbsxdbsx commented 4 years ago

Thanks.