Qt C++ Slot is not called when Signal is sent - c++

After debugging, I'm sure that the replyFinish() slot is not called when I call this->getNetReply(). These are my files, in the main() fumction I call the getNetReply this way: Networking a; a.getNetReply();
I did add QT+=network to my qmake.
Please help me. Thank you very much.
my networking.cpp file
#include "networking.h"
#include <QUrl>
#include <QNetworkRequest>
// constructor
void Networking::getNetReply(){
QNetworkAccessManager *man = new QNetworkAccessManager(this);
QObject::connect(man, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));
qDebug() << "connected";
QNetworkRequest req;
req.setUrl(QUrl("http://www.google.com"));
man->get(req);
}
// this method not called
void Networking::replyFinished(QNetworkReply *reply){
QByteArray data = reply->readAll();
QString str = QString(data);
this->netRep = str;
code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
}
my networking.h header file:
#ifndef NETWORKING_H
#define NETWORKING_H
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
class Networking : public QObject
{
Q_OBJECT
public:
QString netRep;
int code;
explicit Networking(QObject *parent = 0);
void getNetReply();
public slots:
void replyFinished(QNetworkReply*);
};
#endif // NETWORKING_H

The get() function returns a QNetworkReply object. Connect to the error signal of that object and it will tell you if an error happens (and which).

Related

Qt C++ QNetworkRequest not making any requests

I'm trying to fetch some data from an API using QNetworkRequest following this video (https://youtu.be/G06jT3X3H9E)
I have a RoR server running on localhost:3000 and I'm trying to fetch something from it.
.h file:
#ifndef WORKER_H
#define WORKER_H
#include <QObject>
#include <QDebug>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QAuthenticator>
#include <QNetworkProxy>
class Worker : public QObject
{
Q_OBJECT
public:
explicit Worker(QObject *parent = nullptr);
signals:
public slots:
void get(QString code);
private slots:
void readyRead();
void authenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator);
void encrypted(QNetworkReply *reply);
void finished(QNetworkReply *reply);
void networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility accessible);
void preSharedKeyAuthenticationRequired(QNetworkReply *reply, QSslPreSharedKeyAuthenticator *authenticator);
void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator);
void sslErrors(QNetworkReply *reply, const QList<QSslError> &errors);
private:
QNetworkAccessManager manager;
};
#endif // WORKER_H
.cpp file:
void Worker::get(QString code)
{
qInfo() << "Getting something from the server...";
QNetworkReply* reply = manager.get(QNetworkRequest(QUrl(QString("http://localhost:3000/api/v1/circle_gift_cards/scan?codes=" + code))));
connect(reply, &QNetworkReply::readyRead, this, &Worker::readyRead);
}
void Worker::readyRead()
{
qInfo() << "ReadReady";
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
if(reply) qInfo() << reply->readAll();
}
The result from:
Worker worker;
worker.get("abc");
is: "Getting something from the server..."
it should print "ReadReady" when the request is ready, but I don't think the request is being made, there's nothing in the console either.
[Edit 1]
What is calling the worker is this
void MainWindow::on_lineEditScanCard_textChanged(QString code) {
Worker worker(this->site);
worker.get(code);
}
It's an Edit field(where the user is supposed to type a code
[Edit 2]
I removed all app code and did this:
#include <QApplication>
#include "Forms/mainwindow.h"
#include "Requests/worker.h"
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
Worker worker;
worker.get("abc");
return a.exec();
}
And it worked... Why it does not work when called when the Edit text is changed?
Remember the concepts of scope, life cycle, and local variables? In your case worker is a local variable that will be destroyed instantly so the slot is not invoked, use
Worker * worker = new Worker;
worker->get("abc"); //remember to delete the memory when you no longer use it

QT request "QObject::connect: No such slot BackEnd::RequestReceived"

Im trying to make a request to get some data with QT.
my backend.h
#ifndef BACKEND_H
#define BACKEND_H
#include <QNetworkAccessManager>
#include <QObject>
#include <QString>
#include <QNetworkReply>
class BackEnd : public QObject
{
Q_OBJECT
Q_PROPERTY(QString userName READ userName WRITE setUserName NOTIFY userNameChanged)
public:
explicit BackEnd(QObject *parent = nullptr);
QString userName();
void setUserName(const QString &userName);
signals:
void userNameChanged();
private:
QString m_userName;
QNetworkAccessManager *manager;
//also tried: void RequestReceived(QNetworkReply* reply); << without space after QNetworkReply
void RequestReceived(QNetworkReply * reply);
};
#endif // BACKEND_H
my .cpp
#include "backend.h"
#include <string>
#include <iostream>
#include <QtNetwork>
BackEnd::BackEnd(QObject *parent) :
QObject(parent)
{
manager = new QNetworkAccessManager(this);
}
void BackEnd::RequestReceived(QNetworkReply * reply){
QByteArray rawData = reply->readAll();
QString textData(rawData);
qDebug() << textData;
}
QString BackEnd::userName()
{
return m_userName;
}
void BackEnd::setUserName(const QString &userName)
{
if (userName == m_userName)
return;
m_userName = userName;
connect(manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(RequestReceived(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl("https://google.com")));
emit userNameChanged();
}
I found similair questions but almost all answers say Q_OBJECT should be added (which I have). Im very new to qt but if I understand correctly the error indicates that I do not have a
BackEnd::RequestReceived(QNetworkReply*)
method, which I do have. Any help is welcome.
You need to mark void RequestReceived(QNetworkReply * reply); as a slot:
Q_SLOT void RequestReceived(QNetworkReply * reply);

How to get text from website in Qt (QNetworkAccessManager not working)?

I am trying to get text from a website using the QNetworkAccessManager class, but it's not working. When the replyFinished slot above is called, the parameter it takes is the QNetworkReply object containing the downloaded data as well as meta-data (headers, etc.). When the slot gets called, I append the gotten text to a QString and print it out, but it prints an empty string. Here is my code:
//.pro file
TEMPLATE = app
QT += qml quick
QT += network
CONFIG += c++11
//xmlparser.h
#ifndef XMLPARSER_H
#define XMLPARSER_H
#include <QObject>
#include <QNetworkAccessManager>
class XMLParser : public QNetworkAccessManager
{
Q_OBJECT
public:
~XMLParser()=default;
static XMLParser &Instance();
Q_INVOKABLE void readXML();
signals:
finished(QNetworkReply* reply);
public slots:
void replyFinished(QNetworkReply* reply);
private:
XMLParser()=default;
XMLParser(XMLParser const&)=delete;
void operator = (XMLParser const&)=delete;
QByteArray m_text;
};
#endif // XMLPARSER_H
//xmlparser.cpp
#include <QUrl>
#include <QDebug>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QEventLoop>
#include "xmlparser.h"
XMLParser &XMLParser::Instance()
{
static XMLParser instance;
return instance;
}
void XMLParser::readXML()
{
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl("http://qt-project.org")));
}
void XMLParser::replyFinished(QNetworkReply *reply)
{
QString text;
text.append(reply->readAll());
qDebug () << "Text: " << text; //Prints out emtpy string?
}

Connect qnetworkaccessmanager to slot

Now I have this code:
requester.h
#ifndef REQUESTER_H
#define REQUESTER_H
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtCore/QtCore>
#include <QVector>
#include <QObject>
class Requester
{
Q_OBJECT
public:
Requester();
~Requester();
QString get_last_reply();
void send_request();
private:
QNetworkAccessManager *manager;
QVector<QString> replies;
public slots:
void get_reply(QNetworkReply *reply);
};
#endif // REQUESTER_H
requester.cpp
#include "requester.h"
Requester::Requester()
{
manager = new QNetworkAccessManager;
}
Requester::~Requester() {
delete manager;
}
void Requester::get_reply(QNetworkReply *reply) {
QByteArray res = reply->readAll();
QString data = res.data();
replies.push_back(data);
}
QString Requester::get_last_reply() {
QString res = replies.back();
replies.pop_back();
return res;
}
void Requester::send_request() {
QObject::connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(get_reply(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl("http://google.com")));
}
And this error:
no matching function for call to 'QObject::connect(QNetworkReply*&, const char*, Requester* const, const char*)'
What's wrong? I've tried to use just connect instead of QObject::connect, but there was an error about the impossibility of converting QNetworkAccessmanager to socket.
The problem is that you are not inheriting QObject, so naturally: you cannot get slots handled in that class.
You should write something like this:
requester.h
class Requester : public QObject
{
Q_OBJECT
public:
explicit Requester(QObject *parent);
...
requester.cpp
#include "requester.h"
Requester::Requester(QObject *p)
: QObject(p)
, manager(new QNetworkAccessManager)
{
}
...
Also, there is little to no point in this case to construct the QNetworkAccessManager on the heap as opposed to the stack. You could just have a QNetworkAccessManager m_networkAccessManager; member without the allocation, construction, and deletion, but this is just an additional information for the future.

QT QNetworkAccessManager and Signals

I have a class (RequestHandler) that I call from my main Application class to call various web services. Once a service has completed the class sends a Signal back to the calling class to say that it has finished(possibly with QString message). I have the Signal working in a test method but the QNetworkAccessManager is not working at all. I am quite new to QT and C++. Thanks for your help.
// RequestHandler.h
#ifndef REQUESTHANDLER_H_
#define REQUESTHANDLER_H_
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QOBJECT>
#include "MainAppClass.hpp"
class RequestHandler : public QObject
{
Q_OBJECT
public:
explicit RequestHandler(QObject *parent = 0);
void validateRegistration(QString reg);
void onStatusUpdateCompleted();
void sayHi();
signals:
void sendSignal(QString txt);
private:
QNetworkAccessManager* manager;
QObject* thisObj;
public slots:
void onRequestCompleted();
};
#endif /* REQUESTHANDLER_H_ */
RequestHandler.cpp
#include "RequestHandler.h"
RequestHandler::RequestHandler(QObject *parent) : QObject(parent)
{
thisObj= parent;
}
void RequestHandler::validateRegistration(QString reg) {
QNetworkRequest request;
request.setUrl(QUrl("the_registration_url"));
manager = new QNetworkAccessManager();
QNetworkReply *reply = manager->get(request);
connect(reply, SIGNAL(finished()), this, SLOT(onRequestCompleted()));
}
void RequestHandler::onRequestCompleted() {
// not getting here at all
}
void RequestHandler::sayHi()
{
// this is working
QObject::connect(this, SIGNAL(sendSignal(QString)), thisCellCast, SLOT(recieveValue(QString)));
emit sendSignal("HERES THE SIGNAL");
}
I am calling this class like this:
// test slots and reg
RequestHandler rh(this);
//working
rh.sayHi();
// not working
rh.validateRegistration("test");
Thanks for your help.
You typically should be using QNetworkAccessManager by connecting to error() and
finished() signals, so that you will be notified on error occurred.
This is really bad idea to construct QNetworkAccessManager object in validateRegistration() as it will cause memory leak and you need only one object of such
kind. So do it in RequestHandler's constructor.
void RequestHandler::validateRegistration(QString reg)
{
QUrl url("the_registration_url");
QNetworkRequest request(url);
QNetworkReply *reply = manager->get(request);
connect(reply, SIGNAL(finished()), SLOT(onRequestCompleted()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
SLOT(onError(QNetworkReply::NetworkError)));
}
void RequestHandler::onRequestCompleted()
{
qDebug() << "Request succeeded";
}
void RequestHandler::onError(QNetworkReply::NetworkError code)
{
qError() << "Request failed with code " << code;
}
And also make sure you have QApplication::exec() called somewhere, so you have the main event loop running.