I have a problem using a library for a mqtt-client i got from GitHub (https://github.com/emqtt/qmqtt).
我在为GitHub (https://github.com/emqtt/qmqtt)获得的mqtt客户机使用库时遇到了一个问题。
Im using Qt Creator 4.0.3 with Qt Version Qt 5.7.0 and compile with MinGW 5.3.0 on Windows 10.
我使用Qt Creator 4.0.3和Qt Version 5.7.0,在Windows 10上使用mingw5.3.0进行编译。
I have already looked up some other answers on the Internet but they are mostly about compile or linking errors.
My problem is that the code is just not doing what it is supposed to (it does kind of nothing).
我已经在网上查了一些其他的答案,但它们主要是关于编译或链接错误。我的问题是,代码只是不做它应该做的事情(它什么都不做)。
I'm already having problems with the example on the title screen (link above) which connects to a server/broker. It does simply nothing, i don't even get error messages or any other feedback.
我已经对标题屏幕上连接到服务器/代理的示例(上面的链接)产生了问题。它什么都不做,我甚至没有收到错误消息或任何其他反馈。
My Code: I have a Smartpointer to a Client Object
我的代码:我有一个指向客户端对象的Smartpointer
QScopedPointer<QMQTT::Client> client(new QMQTT::Client(QHostAddress("192.168.8.50"), 1883));
The Address and Port are correct, I already tested this with MQTTlens for Google Chrome.
地址和端口是正确的,我已经用MQTTlens对谷歌Chrome进行了测试。
Then I have a seperate class to handle my input and output (I use Multithreading). This class sends & receives signals so it can control the Client / give feedback to the user (through Console Output).
然后我有一个独立的类来处理我的输入和输出(我使用多线程)。这个类发送和接收信号,以便它能够控制客户端/给用户反馈(通过控制台输出)。
class MainIO : public QObject
{
Q_OBJECT
public:
explicit MainIO(QString clientId = "", QObject *parent = 0);
void mainMenue();
private:
QVector<QString> m_mainMenueStrings;
QString m_clientId;
signals:
void connectToHost();
void disconnectFromHost();
void subscribe(const QString &topic, const quint8 qos);
void unsubscribe(const QString &topic);
void publish(const QMQTT::Message &message);
public slots:
void onClientConnected();
void onClientDisconnected();
void onClientPublished();
void onClientError(const QMQTT::ClientError error);
void onClientReceived(const QMQTT::Message &message);
void add();
void subtract();
};
I move an object of the class to a thread in my main
我将类的对象移动到主线程中
QThread mainIOThread;
IoTClient::MainIO control(clientId);
control.moveToThread(&mainIOThread);
and then i connect the signals with slots before starting the thread
然后在启动线程之前,我将信号与插槽连接起来
QObject::connect(&mainIOThread, &QThread::started
, &control, &IoTClient::MainIO::mainMenue);
/* Control -> Client */
QObject::connect(&control, &IoTClient::MainIO::connectToHost
, client.data(), &QMQTT::Client::connectToHost);
QObject::connect(&control, &IoTClient::MainIO::subscribe
, client.data(), &QMQTT::Client::subscribe);
QObject::connect(&control, &IoTClient::MainIO::unsubscribe
, client.data(), &QMQTT::Client::unsubscribe);
QObject::connect(&control, &IoTClient::MainIO::publish
, client.data(), &QMQTT::Client::publish);
/* Client -> Control */
QObject::connect(client.data(), &QMQTT::Client::connected
, &control, &IoTClient::MainIO::onClientConnected);
QObject::connect(client.data(), &QMQTT::Client::disconnected
, &control, &IoTClient::MainIO::onClientDisconnected);
QObject::connect(client.data(), &QMQTT::Client::error
, &control, &IoTClient::MainIO::onClientError);
QObject::connect(client.data(), &QMQTT::Client::published
, &control, &IoTClient::MainIO::onClientPublished);
QObject::connect(client.data(), &QMQTT::Client::received
, &control, &IoTClient::MainIO::onClientReceived);
mainIOThread.start();
The mainMenue-Method looks as follows:
主要方法如下:
void MainIO::mainMenue()
{
std::system("cls");
qDebug().noquote().nospace() << (tr("clients/") + m_clientId +
tr("/state"));
/* Print Menue */
qDebug().noquote().nospace() << "QMQTT-Client";
for (int i = 0; i < m_mainMenueStrings.size(); i++)
{
qDebug().noquote().nospace() << "\t(" << i+1 << ") " <<
m_mainMenueStrings[i];
}
qDebug().noquote().nospace() << "Ihre Auswahl: ";
/* select choice */
int option = -1;
while ((option >= m_mainMenueStrings.size()) || (option < 0)) {
std::cin >> option;
}
option--;
switch(option) {
case 0: emit this->connectToHost();
break;
case 1: add();
break;
case 2: subtract();
break;
case 3: emit this->disconnectFromHost();
break;
default:
qDebug().noquote().nospace() << "Fehlerhafte Eingabe";
}
}
The Console output looks kinda like following: (1) connect (2) add ... your input:_
控制台输出如下:(1)connect (2) add…输入:_
after I send some input (1 for connect) nothing happens. i dont get any error (i have connected the error signal from the client to a error slot) or other signals.
在我发送一些输入(1 for connect)之后,什么都没有发生。我没有得到任何错误(我已经将错误信号从客户端连接到错误槽)或其他信号。
I have already tested this connection in the source code and can't come to a conclusion, because it just makes no sense (to me) that the client won't connect to the broker.
我已经在源代码中测试了这个连接,不能得出结论,因为(对我来说)客户端不连接代理是没有意义的。
The code only fails with Windows, it does work with Linux.
这段代码只在Windows上失败,在Linux上确实有效。
1 个解决方案
#1
0
I have solved this problem. It was, that the qmake version in the project did not work properly after deployment (on Windows). I used the Qbs version, but this one could not be deployed. I built it and copied the generated files needed (.dll and so on) into the corresponding Qt directories on my machine.
我已经解决了这个问题。在部署(在Windows上)之后,项目中的qmake版本没有正常工作。我使用了Qbs版本,但是无法部署这个版本。我构建它并复制所生成的文件(。dll等)进入我的机器上相应的Qt目录。
#1
0
I have solved this problem. It was, that the qmake version in the project did not work properly after deployment (on Windows). I used the Qbs version, but this one could not be deployed. I built it and copied the generated files needed (.dll and so on) into the corresponding Qt directories on my machine.
我已经解决了这个问题。在部署(在Windows上)之后,项目中的qmake版本没有正常工作。我使用了Qbs版本,但是无法部署这个版本。我构建它并复制所生成的文件(。dll等)进入我的机器上相应的Qt目录。