I have 2 Qt programs that are basically identical, I copied the code from one of them to the other to modify it and save it as a new project, now the second one doesn't run and gives a bunch of errors which don't make sense because the other one runs and they are the same where the errors are coming up. Here is the code:
#ifndef FILM_H
#define FILM_H
#include <QWidget>
#include <QString>
#include <QDate>
class Film: public QObject{
Q_OBJECT
Q_PROPERTY( QString title READ getTitle WRITE setTitle);
Q_PROPERTY( int duration READ getDuration WRITE setDuration);
Q_PROPERTY( QString director READ getDirector WRITE setDirector);
Q_PROPERTY( QDate releaseDate READ getReleaseDate WRITE setReleaseDate);
public:
Film(QString t,int dur,QString dir,QDate r);
Film();
void setTitle(QString t);
void setDuration(int dur);
void setDirector(QString dir);
void setReleaseDate(QDate r);
QString getTitle() const;
int getDuration() const;
QString getDirector() const;
QDate getReleaseDate() const;
QString toString();
private:
QString m_title;
int m_duration;
QString m_director;
QDate m_releaseDate;
};
#endif // FILM_H
#ifndef FILMWRITER_H
#define FILMWRITER_H
#include "Film.h"
#include <QtGui>
#include <QFile>
class FilmWriter{
public:
void accessFilm(Film& f);
};
#endif // FILMWRITER_H
#ifndef FILMINPUT_H
#define FILMINPUT_H
#include <QMainWindow>
#include "Film.h"
#include "FilmWriter.h"
#include <QLabel>
#include <QTextEdit>
#include <QPushButton>
namespace Ui {
class FilmInput;
}
class FilmInput : public QMainWindow
{
Q_OBJECT
public:
explicit FilmInput(QWidget *parent = 0);
~FilmInput();
void obtainFilmData(Film& f);
void saveFilm(Film& f);
public slots:
void getFilm();
private:
Ui::FilmInput *ui;
//widgets
QMainWindow* window;
QLabel* infoLabel;
QLabel* titleLabel;
QLabel* durationLabel;
QLabel* directorLabel;
QLabel* relDateLabel;
QTextEdit* titleEdit;
QTextEdit* durationEdit;
QTextEdit* directorEdit;
QTextEdit* relDateEdit;
QPushButton* saveBtn;
QPushButton* cancelBtn;
Film f;
//sets up gui and connects signals and slots
void setUpGui();
};
#endif // FILMINPUT_H
#include "Film.h"
#include <QDate>
#include <QString>
Film::Film(QString t,int dur,QString dir,QDate r):m_title(t),m_duration(dur),m_director(dir),m_releaseDate(r){
}
Film::Film(){
}
void Film::setTitle(QString t){
m_title = t;
}
void Film::setDuration(int dur){
m_duration = dur;
}
void Film::setDirector(QString dir){
m_director = dir;
}
void Film::setReleaseDate(QDate r){
m_releaseDate = r;
}
QString Film::getTitle() const{
return QString("%1").arg(m_title);
}
int Film::getDuration() const{
return m_duration;
}
QString Film::getDirector() const{
return QString("%1").arg(m_director);
}
QDate Film::getReleaseDate() const{
return m_releaseDate;
}
QString Film::toString()
{
return m_title + " " + m_duration + " " + m_director + " " + m_releaseDate.toString();
}
#include "FilmWriter.h"
#include <QtGui>
#include <QFileDialog>
#include <QFile>
#include <QMessageBox>
#include <QObject>
#include <QTextStream>
void FilmWriter::accessFilm(Film& f){
QVariant v1 = f.property("title");
QVariant v2 = f.property("duration");
QVariant v3 = f.property("director");
QVariant v4 = f.property("releaseDate");
QString str = v1.toString() +" "+ v2.toString() +" "+ v3.toString() +" "+ v4.toString();
QMessageBox msgBox;
msgBox.setText(str);
msgBox.exec();
}
#include "filminput.h"
#include "ui_filminput.h"
#include <QtGui>
#include "Film.h"
#include "FilmWriter.h"
#include <QTextEdit>
#include <QDate>
#include <QString>
FilmInput::FilmInput(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::FilmInput)
{
ui->setupUi(this);
setUpGui();
}
FilmInput::~FilmInput()
{
delete ui;
}
void FilmInput::setUpGui(){
//initialise widgets
infoLabel = new QLabel("Please enter film data which will be saved to a file",this);
titleLabel = new QLabel("Film Title",this);
durationLabel = new QLabel("Film Duration",this);
directorLabel = new QLabel("Film Director",this);
relDateLabel = new QLabel("Film Release Date",this);
titleEdit = new QTextEdit(this);
durationEdit = new QTextEdit(this);
directorEdit = new QTextEdit(this);
relDateEdit = new QTextEdit(this);
saveBtn = new QPushButton("Save Film",this);
cancelBtn = new QPushButton("Cancel",this);
//set layout
QFormLayout* layout = new QFormLayout();
layout->addWidget(infoLabel);
layout->addWidget(titleLabel);
layout->addWidget(titleEdit);
layout->addWidget(durationLabel);
layout->addWidget(durationEdit);
layout->addWidget(directorLabel);
layout->addWidget(directorEdit);
layout->addWidget(relDateLabel);
layout->addWidget(relDateEdit);
layout->addWidget(saveBtn);
layout->addWidget(cancelBtn);
this->ui->widget->setLayout(layout);
this->setWindowTitle("Film Archive");
connect(saveBtn,SIGNAL(clicked()),this, SLOT(getFilm()));
connect(cancelBtn,SIGNAL(clicked()),this,SLOT(close()));
}
void FilmInput::getFilm(){
Film f1(titleEdit->toPlainText(),durationEdit->toPlainText().toInt() ,directorEdit->toPlainText(),
QDate::fromString(relDateEdit->toPlainText(),"dd/MM/YYYY"));;
obtainFilmData(f1);
}
void FilmInput::obtainFilmData(Film &f){
FilmWriter f2;
f2.accessFilm(f);
}
#include <QtGui/QApplication>
#include "filminput.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FilmInput w;
w.show();
return a.exec();
}
the error dump:
..\Ass1Q2\filminput.cpp: In constructor 'FilmInput::FilmInput(QWidget*)':
..\Ass1Q2\filminput.cpp:12: error: invalid use of incomplete type 'struct Ui::FilmInput'
..\Ass1Q2\/filminput.h:12: error: forward declaration of 'struct Ui::FilmInput'
..\Ass1Q2\filminput.cpp:14: error: invalid use of incomplete type 'struct Ui::FilmInput'
..\Ass1Q2\/filminput.h:12: error: forward declaration of 'struct Ui::FilmInput'
..\Ass1Q2\filminput.cpp: In destructor 'virtual FilmInput::~FilmInput()':
..\Ass1Q2\filminput.cpp:20: warning: possible problem detected in invocation of delete operator:
..\Ass1Q2\filminput.cpp:20: warning: invalid use of incomplete type 'struct Ui::FilmInput'
..\Ass1Q2\/filminput.h:12: warning: forward declaration of 'struct Ui::FilmInput'
..\Ass1Q2\filminput.cpp:20: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined.
..\Ass1Q2\filminput.cpp: In member function 'void FilmInput::setUpGui()':
..\Ass1Q2\filminput.cpp:50: error: invalid use of incomplete type 'struct Ui::FilmInput'
..\Ass1Q2\/filminput.h:12: error: forward declaration of 'struct Ui::FilmInput'
mingw32-make[1]: Leaving directory `C:/Unisa/COS3711/assignments/Ass1Q2-build-desktop'
mingw32-make: Leaving directory `C:/Unisa/COS3711/assignments/Ass1Q2-build-desktop'
mingw32-make[1]: *** [debug/filminput.o] Error 1
mingw32-make: *** [debug] Error 2
The process "C:/Qt/2010.04/mingw/bin/mingw32-make.exe" exited with code %2.
Error while building project Ass1Q2 (target: Desktop)
When executing build step 'Make'
You copied the code, but not pro file. So, you may miss to add UI file to profile, to invoc UI compiler to generate "ui_FileInput.h" source. Please copy UI file and update .pro file with FORMS += FileInput.ui may solve your problem. To know better you can comment "ui_FileInput.h" and see the behaviour.
Related
I created a new class and added a function to it which is supposed to retrieve data on the current euro exchange rate. I used QNetworkAccessManager. When I try to build project I got errors. After I comment QNetworkAccessManager logic errors disapeard.
Currencies.h
#ifndef CURRENCIES_H
#define CURRENCIES_H
#include <QObject>
#include <QFile>
#include <QTextStream>
#include <QtDebug>
#include <QList>
#include <QProcess>
#include <QRegularExpression>
#include <QSettings>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>
#include <QtXml/QtXml>
#include <QDebug>
#include <string.h>
#include <QFile>
#include <QByteArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <QObject>
#include <algorithm>
#include "Connections.h"
class Euro : public QObject
{
Q_OBJECT
private:
//Ui::MainWindow *ui;
float euroRate=0;
float finalPriceEuro = 0;
float finalPrice = 0;
QString const urlEuroString = "http://api.nbp.pl/api/exchangerates/rates/a/eur/?format=json";
Connection::Status connectStatus;
QSettings settings;
public:
Euro();
~Euro();
float GetEuroRate() const;
void SetEuroRate(double);
void SetFinalPriceEuro(float);
float GetFinalPriceEuro() const;
void SetFinalPrice(float);
float GetFinalPrice() const;
QString GetEuroUrl() const;
void SaveEuroRate();
void LoadEuroRate();
};
#endif // CURRENCIES_H
Currencies.cpp
#include "Currencies.h"
Euro::Euro()
{
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
connect(manager,&QNetworkAccessManager::finished,this,[&](QNetworkReply* reply)
{
if(reply->bytesAvailable())
{
connectStatus.SetConnectionStatus(true);
}
else
{
connectStatus.SetConnectionStatus(false);
}
});
qDebug()<<connectStatus.GetConnectionStatus();
}
Euro::~Euro()
{
}
float Euro::GetEuroRate() const{
return euroRate;
}
void Euro::SetEuroRate(double rateValue){
euroRate = rateValue;
}
QString Euro::GetEuroUrl() const{
return urlEuroString;
}
void Euro::SetFinalPriceEuro(float price)
{
finalPriceEuro = price;
}
float Euro::GetFinalPriceEuro() const
{
return finalPriceEuro;
}
void Euro::SetFinalPrice(float price)
{
finalPrice = price;
}
float Euro::GetFinalPrice() const
{
return finalPrice;
}
void Euro::SaveEuroRate(){
//settings.setValue("euroRate",currencyRate.GetEuroRate());
qDebug()<<"Zapisano!";
}
void Euro::LoadEuroRate()
{
}
Errors
:-1: error: CMakeFiles/ATLAS.dir/Currencies.cpp.obj:Currencies.cpp:(.text+0x2b0): undefined reference to `__imp__ZN21QNetworkAccessManagerC1EP7QObject'
:-1: error: CMakeFiles/ATLAS.dir/Currencies.cpp.obj:Currencies.cpp:(.text+0x2b7): undefined reference to `__imp__ZN21QNetworkAccessManager8finishedEP13QNetworkReply'
:-1: error: CMakeFiles/ATLAS.dir/Currencies.cpp.obj:Currencies.cpp:(.text+0x2d6): undefined reference to `__imp__ZN21QNetworkAccessManager16staticMetaObjectE'
:-1: error: collect2.exe: error: ld returned 1 exit status
Any idea how to solve this? Thanks a lot !
I am going to save a struct in a binary file and load it later on. I found that one way is to use QVariant. Here is a simplified Qt Widget Application example that I have created. But when I run it the binary file remains empty. Could you please help me with that. Also, Is there a better method to do such a thing?
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFile>
#include <QFileDialog>
#include <QDataStream>
#include <QString>
#include "mystruct.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
/* create an object of the struct */
myStruct * aStruct = new myStruct;
aStruct->myStringVar = QString("aaaaa");
/* put the object in QVariant */
QVariant aVariant;
aVariant.setValue(aStruct);
/* save the QVariant to binary file */
QFile myFile("myFile");
QDataStream save(&myFile);
save.setVersion(QDataStream::Qt_4_6);
save << aVariant;
myFile.close();
/* load the QVariant from binary file */
QDataStream load(&myFile);
load.setVersion(QDataStream::Qt_4_6);
QVariant theVariant;
load >> theVariant;
QString theVar = theVariant.value<myStruct*>()->myStringVar;
myFile.close();
}
MainWindow::~MainWindow()
{
delete ui;
}
mystruct.h:
#ifndef MYSTRUCT_H
#define MYSTRUCT_H
#include <QMetaType>
#include <QString>
struct myStruct
{
QString myStringVar;
};
Q_DECLARE_METATYPE(myStruct*)
#endif // MYSTRUCT_H
Comment: As a reference, this link shows a similar problem, but I could not understand/implement the suggestions correctly. For example, I tried to define a new operator, as it is explained here, but it did not work.
First of all you should not save the pointer since this is only the memory address of the variable, what should be saved is the value. On the other hand if you want to use QDataStream you must overwrite stream operators, finally if you want to save as QVariant you must use qRegisterMetaTypeStreamOperators. In addition to the above, your code has several errors such as not opening the QFile.
mystruct.h
#ifndef MYSTRUCT_H
#define MYSTRUCT_H
#include <QString>
#include <QMetaType>
struct myStruct
{
QString myStringVar;
bool operator==(const myStruct & o){
return o.myStringVar == this->myStringVar;
}
friend QDataStream &operator<<(QDataStream &out, const myStruct &rhs){
out << rhs.myStringVar;
return out;
}
friend QDataStream &operator>>(QDataStream &in, myStruct &rhs){
in >> rhs.myStringVar;
return in;
}
};
Q_DECLARE_METATYPE(myStruct)
#endif // MYSTRUCT_H
main.cpp
#include "mystruct.h"
#include <QDataStream>
#include <QFile>
#include <QVariant>
int main(int argc, char *argv[])
{
qRegisterMetaTypeStreamOperators<myStruct>("myStruct");
// create struct
myStruct aStructIn{"aaa"};
// myStruct to QVariant
QVariant aVariant;
aVariant.setValue(aStructIn);
//open file
QFile myFile("myFile");
if(!myFile.open(QIODevice::WriteOnly))
return -1;
// save QVariant
QDataStream save(&myFile);
save.setVersion(QDataStream::Qt_4_6);
save << aVariant;
myFile.close();
//open file
if(!myFile.open(QIODevice::ReadOnly))
return -1;
// read QVariant
QDataStream load(&myFile);
load.setVersion(QDataStream::Qt_4_6);
QVariant theVariant;
load >> theVariant;
myFile.close();
// QVariant to myStruct
myStruct aStructOut = theVariant.value<myStruct>();
Q_ASSERT(aStructOut == aStructIn);
return 0;
}
Here is the example that worked for me after doing all the corrections. This example shows how I save/load a struct, or a class, into a file using QDataStream. The struct, i.e. myStruct, has three members: an int, a string, and a struct, i.e. mySubStruct.
For each struct/class, the << and >> operators need to be overloaded.
In each overloaded operator, save/load of the struct is accomplished by breaking up the struct into its primitive members. The primitive members are the types that are supported by QDataStream.
comment 1: The following example uses overloading the operators to a struct. To see how to overload the operators to a class see this link.
comment 2: This link is a complete example.
mysubstruct.h
#ifndef MYSUBSTRUCT_H
#define MYSUBSTRUCT_H
#include <QString>
#include <QDataStream>
struct mySubStruct
{
int intVar;
QString strVar;
/* overload the operators */
friend QDataStream &operator<< (QDataStream &out, const mySubStruct &rhs)
{
out << rhs.intVar << rhs.strVar;
return out;
}
friend QDataStream &operator>> (QDataStream &in, mySubStruct &rhs)
{
in >> rhs.intVar >> rhs.strVar;
return in;
}
};
#endif // MYSUBSTRUCT_H
mystruct.h
#ifndef MYSTRUCT_H
#define MYSTRUCT_H
#include <QString>
#include <QDataStream>
#include "mysubstruct.h"
struct myStruct
{
int intVar;
QString strVar;
mySubStruct subStructObj;
/* overload the operators */
friend QDataStream &operator<< (QDataStream &out, const myStruct &rhs)
{
out << rhs.intVar << rhs.strVar << rhs.subStructObj;
return out;
}
friend QDataStream &operator>> (QDataStream &in, myStruct &rhs)
{
in >> rhs.intVar >> rhs.strVar >> rhs.subStructObj;
return in;
}
};
#endif // MYSTRUCT_H
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFile>
#include <QString>
#include <QDebug>
#include "mystruct.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp shows how the operators are implemented.
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
/* create struct */
mySubStruct subStructIn;
subStructIn.intVar = 1;
subStructIn.strVar = "a";
myStruct structIn;
structIn.intVar = 11;
structIn.strVar = "aa";
structIn.subStructObj = subStructIn;
/* open file */
QFile myFile("myFile");
/* save */
if(myFile.open(QIODevice::WriteOnly))
{
QDataStream save(&myFile);
save.setVersion(QDataStream::Qt_4_6);
save << structIn;
myFile.close();
}
/* load */
myStruct structOut;
if(myFile.open(QIODevice::ReadOnly))
{
QDataStream load(&myFile);
load.setVersion(QDataStream::Qt_4_6);
load >> structOut;
myFile.close();
}
qDebug() << structOut.intVar;
qDebug() << structOut.strVar;
qDebug() << structOut.subStructObj.strVar;
qDebug() << structOut.subStructObj.intVar;
}
MainWindow::~MainWindow()
{
delete ui;
}
I have a problem in instantiating a new class with another parameter.
My problem is to add something in a QList Asteroids Object. In Qt, I have this error message:
cannot convert 'SettingsAsteroid' to 'SettingsAsteroid*' in assignment
this->settingsAsteroid = SettingsAsteroid();
Below are the relevant files of the class doing it, and I think other classes are not relevant.
Data in GameView.h :
#ifndef GAMEVIEW_H
#define GAMEVIEW_H
#include <QGraphicsView>
#include <QGraphicsItem>
#include <QApplication>
#include <QPushButton>
#include <QList>
#include <QObject>
#include <QString>
#include "Asteroid.h"
#include "SettingsAsteroid.h"
class GameView : public QGraphicsView
{
Q_OBJECT
// Data
int nbAsteroids;
int nbAsteroidsAlive;
SettingsAsteroid* settingsAsteroid;
QList<Asteroid> asteroids;
// Menu
QPushButton *startgame;
// Scène
QGraphicsScene* grfxScene;
public:
GameView();
~GameView();
private slots:
void start();
};
#endif // GAMEVIEW_H
Source code in GameView.c :
#include "GameView.h"
#include <iostream>
GameView::GameView()
{
int nbAsteroids = 0;
int nbAsteroidsAlive = 0;
// data de jeu
this->settingsAsteroid = SettingsAsteroid();
//Scene de debut
this->grfxScene = new QGraphicsScene();
grfxScene->setSceneRect(0,0,800,600);
this->grfxScene->addPixmap(QPixmap(":/images/armageddon.jpg"));
setScene(this->grfxScene);
}
GameView::~GameView(){ }
void GameView::start()
{
this->grfxScene->clear();
int nbAsteroids = 4;
int nbAsteroidsAlive = 4;
int i;
for(i=0;i<nbAsteroids;i++) {
asteroids.append(new Asteroid(settingsAsteroid));
}
}
Constructor of Asteroid.c :
Asteroid::Asteroid(SettingsAsteroid settingsAsteroid)
Based on your error
cannot convert 'SettingsAsteroid' to 'SettingsAsteroid*' in assignment this->settingsAsteroid = SettingsAsteroid();
On the code:
this->settingsAsteroid = SettingsAsteroid();
You are attempting to convert a SettingsAsteroid into a SettingsAsteroid*, that is: a pointer to a SettingsAsteroid object.
Because GameView has a member settingsAsteroid which is a SettingsAsteroid*, you need to give it a pointer to a SettingsAsteroid, not a SettingsObject itself. You can do one of the following:
this->settingsAsteroid = new SettingsAsteroid();
Calling new will allocate memory for the required object (your SettingsAsteroid) and return a pointer to that memory, of type SettingsAsteroid*. Alternatively, if you already have some SettingsAsteroid object you could assign it instead:
SettingsAsteroid sa;
...
this->settingsAsteroid = &sa;
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 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!