Hi I am trying to send an HTTP GET request and receive the result with a function. But I am getting this error:
no matching function for call to 'HttpNetworkRequest::connect(QNetworkAccessManager*&, const char*, RequestFinishFunction&, const
char*)'
the connect function is in this HttpNetworkRequest.cpp file
#include "HttpNetworkRequest.hpp"
#include <QObject>
#include <QIODevice>
#include <QDir>
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/XmlDataModel>
#include <RequestFinishFunction.hpp>
using namespace bb::cascades;
HttpNetworkRequest::HttpNetworkRequest(bb::cascades::Application *app)
: QObject(app)
{
QNetworkRequest networkRequest = QNetworkRequest();
networkRequest.setUrl(QUrl("http://www.blackberry.com"));
RequestFinishFunction req;
QNetworkAccessManager *networkAccessManager = new QNetworkAccessManager;
bool res = connect(networkAccessManager,SIGNAL(finished(QNetworkReply*)),
req,SLOT(requestFinished()));
Q_ASSERT(res);
Q_UNUSED(res);
networkAccessManager->get(networkRequest);
}
The included RequestFinishFunction.hpp
#ifndef REQUESTFINISHFUNCTION_HPP_
#define REQUESTFINISHFUNCTION_HPP_
#include <bb/cascades/ActivityIndicator>
#include <QObject>
#include <QObject>
class RequestFinishFunction : QObject
{
public slots:
void requestFinished(QNetworkReply* reply);
};
#endif /* REQUESTFINISHFUNCTION_HPP_ */
The RequestFinishFunction.cpp file is
#include <bb/cascades/ActivityIndicator>
#include <QObject>
#include <QObject>
#include <RequestFinishFunction.hpp>
void RequestFinishFunction::requestFinished(QNetworkReply* reply)
{
reply->deleteLater();
}
Syntax of connect method
bool QObject::connect ( const QObject * sender, const char * signal, const QObject * receiver, const char * method, Qt::ConnectionType type = Qt::AutoConnection )
We have to pass references of both sender & receiver.
You didn't send reference of receiver. You have two options to do so.
1) Send reference of req using & operator in connect method call
Replace
connect(networkAccessManager,SIGNAL(finished(QNetworkReply*)), req,SLOT(requestFinished()));
with
connect(networkAccessManager,SIGNAL(finished(QNetworkReply*)), &req,SLOT(requestFinished()));
OR
2) Declare req as pointer
Replace
RequestFinishFunction req;
with
RequestFinishFunction *req;
Related
I wrote a simple threadpool server with qt. When i try to connect to server on win 32/64 all works good. But when I use linux centos 7 server is not responding. I use 127.0.0.1:8080 for server address. Also server uses database mysql. When I try to connect via telnet it connects but nothing happens. I checked for open ports with netstat. Maybe I missed something because of this the server is not working?
Here is my code for server. In fact, there is also an http request handler, but it does not reach it, I tried to output a string in the constructor - it is not called.
QthreadPoolServer.cpp
#include "QThreadPoolServer.h"
#include "QSocketRunnable.h"
#include "ConfigReader.h"
#include <memory>
QThreadPoolServer::QThreadPoolServer()
{
ConfigReader reader(config_file_path);
QHostAddress server_IP(reader.getServerAddress());
int port = reader.getServerPort();
listen(QHostAddress::localhost, 8080);
std:: cout << serverError() << errorString().toStdString();
m_threadPool = std::make_shared<QThreadPool>(this);
}
void QThreadPoolServer::incomingConnection(int handle)
{
std::shared_ptr<QSocketRunnable> runnable = std::make_shared<QSocketRunnable>(handle);
runnable->setAutoDelete(false);
m_threadPool->start(runnable.get());
}
QThreadPoolServer::~QThreadPoolServer()
{
m_threadPool->~QThreadPool();
}
QThreadPoolServer.h
#ifndef QTHREADPOOLSERVER_H
#define QTHREADPOOLSERVER_H
#include <QTcpServer>
#include <QThreadPool>
#include <memory>
class QThreadPoolServer : public QTcpServer
{
public:
explicit QThreadPoolServer();
void incomingConnection(int handle);
~QThreadPoolServer();
private:
std::shared_ptr<QThreadPool> m_threadPool;
};
#endif // QTHREADPOOLSERVER_H
QSocketRunnable.cpp
#include "QSocketRunnable.h"
#include <QString>
#include <memory>
#include <iostream>
QSocketRunnable::QSocketRunnable(int handle) : m_descriptor(handle) { }
void QSocketRunnable::run()
{
QTcpSocket* socket = new QTcpSocket();
socket->setSocketDescriptor(m_descriptor);
socket->waitForReadyRead();
QString request_data = QString(socket->readAll());
HttpRequestHandler handler(request_data);
handler.makeResponse();
QString http_response_result = handler.getHttpResponse();
std::cout << http_response_result.toStdString() << "\n";
socket->write(http_response_result.toUtf8());
socket->waitForBytesWritten(90000);
socket->disconnectFromHost();
socket->close();
socket->deleteLater();
}
QSocketRunnable.h
#ifndef QSOCKETRUNNABLE_H
#define QSOCKETRUNNABLE_H
#include <QRunnable>
#include <QTcpSocket>
#include <QtDebug>
#include <QString>
//#include "IDHelper.h"
//#include "JsonFormatter.h"
//#include "HttpRequestHandler.h"
class QSocketRunnable : public QRunnable
{
public:
QSocketRunnable(int handle);
void run() override;
private:
int m_descriptor;
};
#endif // QSOCKETRUNNABLE_H
main.cpp
#include <QCoreApplication>
#include "QThreadPoolServer.h"
#include "signal.h"
#include <sstream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QThreadPoolServer server;
return a.exec();
}
Also std:: cout << serverError() << errorString().toStdString(); returns "-1" that means QAbstractSocket::UnknownSocketError -1 An unidentified error occurred.
As #chehrlic correctly noted: I had an incorrectly overloaded function, so here is the ritht version of QThreadPoolServer.h
QThreadPoolServer.h
#ifndef QTHREADPOOLSERVER_H
#define QTHREADPOOLSERVER_H
#include <QTcpServer>
#include <QThreadPool>
#include <memory>
class QThreadPoolServer : public QTcpServer
{
public:
explicit QThreadPoolServer();
protected:
void incomingConnection(qintptr handle) override;
~QThreadPoolServer();
private:
std::shared_ptr<QThreadPool> m_threadPool;
};
#endif // QTHREADPOOLSERVER_H
My my implementation did not work correctly with a smart pointer to a runnable object:
QThreadPoolServer.cpp
void QThreadPoolServer::incomingConnection(qintptr handle)
{
QSocketRunnable* runnable = new QSocketRunnable(handle)
runnable->setAutoDelete(true);
m_threadPool->start(runnable);
}
I am currently trying to run a class member function in a separate thread which results in a call to implicitly deleted copy constructor , there are already multiple Questions to this answered here on StackOverflow but honestly a lot of this is 5+ years old or feels hacky thats why I am asking myself what would be the best practice to do this with modern c++?
So currently I have the following :
A ZMQWorker which should run in separate threads ( sock is not Thread save)
zmqwoker.cpp
#include "zmqworker.h"
#include <QDebug>
#include <iostream>
ZMQWorker::ZMQWorker()
: sock(ctx, zmq::socket_type::dealer)
{
qDebug() << "Dealer Socket created";
}
void ZMQWorker::connectSocket()
{
std::string origin="CPP_ZMQ";
sock.connect("tcp://127.0.0.1:5555");
qDebug() << "Dealer Socket connected";
}
void ZMQWorker::receiveMessage()
{
while (1){
std::vector<zmq::message_t> recv_msg;
auto res = zmq::recv_multipart(sock,
std::back_inserter(recv_msg));
for (auto&& msg : recv_msg) {
std::cout << msg.to_string_view() << std::endl;
}
}
}
zmq::socket_t &ZMQWorker::getSock()
{
return sock;
}
zmqworker.h
#ifndef ZMQWORKER_H
#define ZMQWORKER_H
#include <zmq_addon.hpp>
#include <thread>
class ZMQWorker
{
public:
ZMQWorker();
void connectSocket();
zmq::context_t ctx;
zmq::socket_t sock;
void receiveMessage();
zmq::socket_t &getSock();
};
#endif // ZMQWORKER_H
and a ZMQBridge which should act as Bridge between QT and the ZMQ socket , since receive and send both are blocking function these should work in different Threads.
zmqbridge.h
#ifndef ZMQBRIDGE_H
#define ZMQBRIDGE_H
#include <QObject>
#include <iostream>
#include "zmqworker.h"
class ZMQBridge : public QObject
{
Q_OBJECT
public:
explicit ZMQBridge(QObject *parent = nullptr);
void createMessageSocket();
ZMQWorker sendSocket;
Q_INVOKABLE void callCoro(QString msg);
signals:
private:
void spawnWorker();
};
#endif // ZMQBRIDGE_H
zmqbridge.cpp
#include "zmqbridge.h"
#include <stdio.h>
#include <QDebug>
#include "zmq_dealer.h"
#include <iostream>
#include <msgpack.hpp>
#include <thread>
#include <chrono>
#include <iostream>
#include <string>
#include <random>
#include <nlohmann/json.hpp>
ZMQBridge::ZMQBridge(QObject *parent)
: QObject{parent},
sendSocket()
{
sendSocket.connectSocket();
}
void ZMQBridge::createMessageSocket(){}
void ZMQBridge::spawnWorker(){}
void ZMQBridge::callCoro(QString msg)
{
std::cout << "Hello from c++";
ZMQWorker receiveSocket = ZMQWorker();
std::thread (&ZMQWorker::receiveMessage, receiveSocket).detach();
qDebug() << "Hello";
nlohmann::json jmsg;
jmsg["randvar"] = "Hello";
zmq::message_t z_out(jmsg.dump());
sendSocket.getSock().send(z_out, zmq::send_flags::none);
}
Error Message:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/type_traits:2596: error: call to implicitly-deleted copy constructor of 'typename decay<ZMQWorker &>::type' (aka 'ZMQWorker')
In file included from /Users/ahoehne/repos/ondoki-desktop/zmqbridge.cpp:1:
In file included from /Users/ahoehne/repos/ondoki-desktop/zmqbridge.h:4:
In file included from /Users/ahoehne/Qt/6.2.2/macos/lib/QtCore.framework/Headers/QObject:1:
In file included from /Users/ahoehne/Qt/6.2.2/macos/lib/QtCore.framework/Headers/qobject.h:46:
In file included from /Users/ahoehne/Qt/6.2.2/macos/lib/QtCore.framework/Headers/qobjectdefs.h:48:
In file included from /Users/ahoehne/Qt/6.2.2/macos/lib/QtCore.framework/Headers/qnamespace.h:44:
In file included from /Users/ahoehne/Qt/6.2.2/macos/lib/QtCore.framework/Headers/qglobal.h:45:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/type_traits:2596:12: error: call to implicitly-deleted copy constructor of 'typename decay<ZMQWorker &>::type' (aka 'ZMQWorker')
return _VSTD::forward<_Tp>(__t);
^~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/__config:856:15: note: expanded from macro '_VSTD'
#define _VSTD std::_LIBCPP_ABI_NAMESPACE
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/thread:312:28: note: in instantiation of function template specialization 'std::__decay_copy<ZMQWorker &>' requested here
_VSTD::__decay_copy(_VSTD::forward<_Args>(__args))...));
^
/Users/ahoehne/repos/ondoki-desktop/zmqbridge.cpp:31:1: note: in instantiation of function template specialization 'std::thread::thread<void (ZMQWorker::*)(), ZMQWorker &, void>' requested here
std::thread (&ZMQWorker::receiveMessage, receiveSocket).detach();
^
/Users/ahoehne/repos/ondoki-desktop/zmqworker.h:14:20: note: copy constructor of 'ZMQWorker' is implicitly deleted because field 'ctx' has a deleted copy constructor
zmq::context_t ctx;
^
/Users/ahoehne/repos/vcpkg/installed/arm64-osx/include/zmq.hpp:903:5: note: 'context_t' has been explicitly marked deleted here
context_t(const context_t &) ZMQ_DELETED_FUNCTION;
^
In other languages like Java or Python which I am used to , I would just inherit from Thread and overwrite the run method and maybe depending on how dynamic and many I need use this with a ThreadPool combined with async/await ...How would I do this in C++ .The informations seems to be mixed and it feels like following a rabbit into it's hole to a new magic land.
Thank you for your help ....
You're passing receiveSocket by value trying to call the deleted copy constructor.
You have to either pass a pointer to your ZMQWorker:
std::thread (&ZMQWorker::receiveMessage, &receiveSocket)
or a reference:
std::thread (&ZMQWorker::receiveMessage, std::ref(receiveSocket))
But then you have to solve another problem:
ZMQWorker receiveSocket;
goes out of scope at the end of this method and will be destroyed while your other thread might still be using it.
What I did now is this :
zmqbridge.cpp
#include "zmqbridge.h"
#include <stdio.h>
#include <QDebug>
#include "zmq_dealer.h"
#include <iostream>
#include <msgpack.hpp>
#include <thread>
#include <chrono>
#include <iostream>
#include <string>
#include <random>
#include <nlohmann/json.hpp>
ZMQBridge::ZMQBridge(QObject *parent)
: QObject{parent},
sendSocket("CPP_SEND"),
receiveSocket("CPP_RECV")
{
this->sendSocket.connectSocket();
this->receiveSocket.connectSocket();
std::thread (&ZMQWorker::receiveMessage, &receiveSocket).detach();
}
void ZMQBridge::createMessageSocket(){}
void ZMQBridge::spawnWorker(){}
void ZMQBridge::callCoro(QString msg)
{
std::cout << "Hello from c++";
qDebug() << "Hello";
nlohmann::json jmsg;
jmsg["name"] = "hello";
jmsg["dispatch_to"] = "caller";
zmq::message_t z_out(jmsg.dump());
sendSocket.getSock().send(z_out, zmq::send_flags::none);
}
zmqbridge.h
#ifndef ZMQBRIDGE_H
#define ZMQBRIDGE_H
#include <QObject>
#include <iostream>
#include "zmqworker.h"
class ZMQBridge : public QObject
{
Q_OBJECT
public:
explicit ZMQBridge(QObject *parent = nullptr);
void createMessageSocket();
ZMQWorker sendSocket;
ZMQWorker receiveSocket;
Q_INVOKABLE void callCoro(QString msg);
signals:
private:
void spawnWorker();
};
#endif // ZMQBRIDGE_H
This works but it does not feel like a ideal grade solution , if so I will accept the answer provided by Stefan Riedel
i know this question has been answered in this forum before, but I need more specific help.
Here's the code:
sessionwindow.cpp
#include "sessionwindow.h"
#include "ui_sessionwindow.h"
#include "session.h"
#include "utils.h"
#include <QStringList>
SessionWindow::SessionWindow(QWidget *parent) : QDialog(parent), ui(new Ui::SessionWindow)
{
ui->setupUi(this);
this->setFixedSize(this->size());
}
SessionWindow::~SessionWindow()
{
delete ui;
}
void SessionWindow::on_cancelBtn_clicked()
{
close();
}
void SessionWindow::on_createBtn_clicked()
{
QString min = (ui->isMin) ? "min" : "nomin";
QString sp = (ui->spHidd) ? "nosp" : "sp";
QString name = ui->sessionName->text();
QString user = ui->skUser->text();
QString pass = ui->skPass->text();
Utils u;
u.createSession(name, user, pass, min, sp);
}
utils.h (Where the function prototype is declared)
#ifndef UTILS_H
#define UTILS_H
#include <QString>
#include <QStringList>
class Utils
{
public:
Utils();
~Utils();
void startSkype(QString, QString, QStringList);
void createSession(QString , QString, QString, QString, QString);
};
#endif // UTILS_H
utils.cpp (Where the function is)
#include "utils.h"
#include "session.h"
#include <QString>
#include <QStringList>
#include <QVector>
#include <QDebug>
QVector<Session> sessions;
Utils::Utils()
{
}
Utils::~Utils()
{
}
void Utils::startSkype(QString user, QString pass, QStringList options)
{
}
void createSession(QString name, QString user, QString pass, QString isMin, QString spHid)
{
sessions.append(Session(name, user, pass, isMin, spHid));
}
The problem is tha I can't compile it, it just throws an error: undefined reference to `Utils::createSession(QString, QString, QString, QString, QString)'
Sorry if I explained bad and thanks for the help!! :D
You forgot to put Utils:: in front of your method definition:
void createSession(QString name, QString user, QString pass, QString isMin, QString spHid)
{
sessions.append(Session(name, user, pass, isMin, spHid));
}
should be
void Utils::createSession(QString name, QString user, QString pass, QString isMin, QString spHid)
{
sessions.append(Session(name, user, pass, isMin, spHid));
}
I'm doing my first project with c++ following the MVC pattern. I have a controller class, Session, which has all functions to manage the class "ClientTsFrm", a view. What I want to do it's to communicate to the view class all events that happen. To do that, I'm using the observer pattern, in fact this implementation 1
Session.h Session is also a singleton.
#pragma once
#include "User.h"
#include "Config.h"
#include "../data/message.h"
#include <list>
#include <cstring>
#include <stdio.h>
#include <cstdlib>
#include "../lib/eventtype.h"
#include "../lib/Subject.h"
#ifndef WX_PRECOMP
#include <wx/wx.h>
#include <wx/frame.h>
#else
#include <wx/wxprec.h>
#endif
#include <wx/richtext/richtextctrl.h>
#include <wx/grid.h>
typedef std::list<UserPTR> UserList;
typedef std::shared_ptr<UserList> UserListPTR;
/*
* Set and get functions
*/
class Session: public Subject<EventTS>{
public:
static Session* Instance();
private:
Session(); // Private so that it can not be called
Session(Session const&){}; // copy constructor is private
Session& operator=(Session const&){}; // assignment operator is private
static Session* m_pInstance;
public:
private:
Session* m_instance;
ConfigPTR m_config;
UserListPTR m_luser;
char* m_translationEngine;
MessageQueuePTR m_pending;
MessageQueuePTR m_queue;
};
Subject.h
//
// Copyright (c) 2013 Juan Palacios juan.palacios.puyana#gmail.com
// Subject to the BSD 2-Clause License
// - see < http://opensource.org/licenses/BSD-2-Clause>
//
#ifndef SUBJECT_H_
#define SUBJECT_H_
#include <functional>
#include <map>
#include <vector>
#include <utility> // for std::forward
template <typename Event>
class Subject
{
public:
Subject()=default;
template <typename Observer>
void registerObserver(const Event& event, Observer&& observer)
{
observers_[event].push_back(std::forward<Observer>(observer));
}
template <typename Observer>
void registerObserver(Event&& event, Observer&& observer)
{
observers_[std::move(event)].push_back(std::forward<Observer>(observer));
}
void notify(const Event& event) const
{
for (const auto& obs : observers_.at(event)) obs();
}
// disallow copying and assigning
Subject(const Subject&)=delete;
Subject& operator=(const Subject&)=delete;
private:
std::map<Event, std::vector<std::function<void()>>> observers_;
};
#endif // SUBJECT_H_
ClientTsFrm.h
#pragma once
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include <wx/wx.h>
#include <wx/frame.h>
#else
#include <wx/wxprec.h>
#endif
#include "../Data/config.h"
#include "../lib/ClientTS.h"
#include "../data/Session.h"
#include "../data/Message.h"
#include "FrmMailSending.h"
#include "FrmSettingMail.h"
#include "AudioWizard.h"
#include "NationList.h"
#include "LoginWarnings.h"
#include "../ArchiveLog.h"
#include "FrmSaveChat.h"
#include <wx/sizer.h>
#include <wx/wx.h>
#include <wx/timer.h>
#include <wx/stattext.h>
#include <wx/richtext/richtextctrl.h>
#include <wx/textctrl.h>
#include <wx/button.h>
#include <wx/grid.h>
#include "../GlobalVariables.h"
#include "../translateController/translateController.h"
#include "../translateController/translateVariable.h"
#include <list>
//#include "../lib/Observer.h"
#define MENU_ESCI 1800
#define MENU_OPZIONI 1801
#define MENU_SPEECH 1802
class ClientTsFrm : public wxFrame
{
ClientTsFrm(LoginWarnings *warn, wxWindow *parent, wxWindowID id = 1, const wxString &title = wxT("TeamTranslate"),
const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
long style = wxCAPTION | wxSYSTEM_MENU | wxCLOSE_BOX | wxRESIZE_BORDER);
virtual ~ClientTsFrm(){};
ClientTsFrm(const ClientTsFrm& tg) {
std::cout << "\tSimpleCat(SimpleCat&) called\n";
}
void updatePanelMsg();
private:
unsigned int curRow; //Initialize Row index
unsigned int curCol; //Initialize Column index
Session* session;
ConfigPTR config;
NationList *nations;
int REFRESHTIMER = 0;
uint64 _sclogID;
wxTimer *WxTimer2;
wxTimer *WxTimer1;
wxButton *btnspeech;
wxRichTextCtrl *txtclient;
wxTextCtrl *txtlingua;
wxStaticText *lbllingua;
wxStaticText *lblnick;
wxTextCtrl *txtnick;
wxRichTextCtrl *txtchat;
wxButton *btnsend;
wxTextCtrl *txtmsg;
wxGrid *gridchat;
wxGrid *gridclient;
wxBoxSizer *sizer;
wxGridSizer *gridsizer;
wxMenuBar *WxMenuBar1;
wxMenu *ID_MNU_FILE_1001_Mnu_Obj;
wxMenu *ID_MNU_OPZIONI_1004_Mnu_Obj;
wxBitmapButton *WxBitmapButton1;
ClientTS clientts;
};
ClientTS.cpp. This is how I register the object
...
session->registerObserver(EventTS::MSG_RCV, std::bind(notifyMSG, *this));
...
void notifyMSG(ClientTsFrm *fn)
{
fn->updatePanelMsg();
}
Basically, I'm registering ClientTSFrm object. It's stored in session class and when an event happens, session calls the "notify" function from the subject class. This function calls the function of ClientTSFrm (the observer) which is send also the object. At the end, this function calls a function of ClientTSFrm class in order to update the content.
If I compile, it shows this error:
Error 28 error C2664: 'void (ClientTsFrm *)' : cannot convert argument
1 from 'ClientTsFrm' to 'ClientTsFrm *' C:\Program Files
(x86)\Microsoft Visual Studio 12.0\VC\include\functional 1149 1
https://github.com/juanchopanza/cppblog/blob/master/Patterns/Observer/
I'm trying to change the user agent in Qt4.
My code:
main.cpp:
#include <QApplication>
#include <QDeclarativeContext>
#include <QDeclarativeEngine>
#include "qmlapplicationviewer.h"
#include "NetworkAccessManagerFactory.h"
#ifdef DEBUG
#include "logger.h"
#endif
Q_DECL_EXPORT int main(int argc, char *argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));
app->setOrganizationName("...");
app->setApplicationName("...");
QmlApplicationViewer viewer; /*and stuff related to it*/
QString userAgent("useragentstring");
NetworkAccessManagerFactory factory(userAgent);
viewer.engine()->setNetworkAccessManagerFactory(&factory);
/*showing*/
return app->exec();
}
NetworkAccessManagerFactory.h:
#ifndef NETWORKACCESSMANAGERFACTORY_H
#define NETWORKACCESSMANAGERFACTORY_H
#include <QDeclarativeNetworkAccessManagerFactory>
#include "CustomNetworkAccessManager.h"
class NetworkAccessManagerFactory : public QDeclarativeNetworkAccessManagerFactory
{
public:
explicit NetworkAccessManagerFactory(QString p_userAgent = "");
QNetworkAccessManager* create(QObject* parent)
{
CustomNetworkAccessManager* manager = new CustomNetworkAccessManager(__userAgent, parent);
return manager;
}
private:
QString __userAgent;
};
#endif // NETWORKACCESSMANAGERFACTORY_H
CustomNetworkAccessManager.h:
#ifndef CUSTOMNETWORKACCESSMANAGER_H
#define CUSTOMNETWORKACCESSMANAGER_H
#include <QNetworkAccessManager>
#include <QNetworkRequest>
class CustomNetworkAccessManager : public QNetworkAccessManager {
Q_OBJECT
public:
explicit CustomNetworkAccessManager(QString p_userAgent = "", QObject *parent = 0);
protected:
QNetworkReply *createRequest( Operation op, const QNetworkRequest &req, QIODevice * outgoingData=0 )
{
QNetworkRequest new_req(req);
new_req.setRawHeader("User-Agent", __userAgent.toAscii());
QNetworkReply *reply = QNetworkAccessManager::createRequest( op, new_req, outgoingData );
return reply;
}
private:
QString __userAgent;
};
#endif // CUSTOMNETWORKACCESSMANAGER_H
The errors are:
/home/marcin/proj/mobilitare/main.cpp:31: error: undefined reference to `NetworkAccessManagerFactory::NetworkAccessManagerFactory(QString)'
/home/marcin/proj/mobilitare/NetworkAccessManagerFactory.h:13: error: undefined reference to `CustomNetworkAccessManager::CustomNetworkAccessManager(QString, QObject*)'
What am I doing wrong? Thanks!