我试图在服务器上为我的 API 创建一个客户端类,但我的插槽在请求后不起作用。 connect 函数返回 true,这应该意味着信号和插槽相互连接。
header file
#ifndef QRESTCLIENT_H
#define QRESTCLIENT_H
#include <QObject>
#include <QUrl>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
class QRestClient : public QObject
{
Q_OBJECT
public:
explicit QRestClient(QUrl root_url, QObject *parent = nullptr);
bool ping();
private:
QUrl root_url;
private slots:
void ping_finished(QNetworkReply * response);
};
#endif // QRESTCLIENT_H
cpp file
#include "qrestclient.h"
QRestClient::QRestClient(QUrl root_url, QObject *parent)
: QObject{parent}
{
this->root_url = root_url;
}
void QRestClient::ping_finished(QNetworkReply * response) {
qDebug() << "It is here 3";
qDebug() << response->readAll();
};
bool QRestClient::ping()
{
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
qDebug() << connect(manager, &QNetworkAccessManager::finished,
this, &QRestClient::ping_finished);
qDebug() << "It is here 1";
manager->get(QNetworkRequest(this->root_url));
qDebug() << "It is here 2";
return true;
};
我试着在
mainwindow.cpp
文件中写同样的东西,它工作正常。对于来自班级ping()
函数的请求,我在服务器日志中看不到任何请求。如果 mainwindow.cpp
的一切都相同,我会在日志中看到请求。问题是什么,为什么它不发送请求?