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));
}
Related
I am busy with my university assignment, i'm new to Qt, C++ and i am required to:
Define and implement the Address class in separate header and source files
I am getting confused with Qt4 and Qt5 as the prescribed text book gives all examples in Qt4 yet we are required to code with Qt5.
I keep getting errors and the latest error is :
error: undefined reference to
Dialog7getTextEP7QWidgetRK7QStringS4_N9QLineEdit8EchoModeES4_
Pb6QFlagsIN2Qt10WindowTypeEES8_INS9_15InputMethodHintEE'
error: undefined reference to
MessageBox11informationEP7QWidgetRK7QStringS4_6QFlagsINS_
14StandardButtonEES6
collect2.exe:-1: error: error: ld returned 1 exit status
I am very confused and stuck, please if anyone can steer me in the right direction i would greatly appreciate it, This is my code so far:
Header File:
#ifndef ADDRESS_H_
#define ADDRESS_H_
#include <QString>
#include <QFile>
#include <QStringList>
#include <QtCore>
QTextStream cout(stdout);
QTextStream cerr(stderr);
class Address{
public:
Address();
void setLines(QString ad, QString sep);
QString getPostalCode() const;
QString toString(QString sep) const;
static bool isPostalCode(QString pc);
private:
static QStringList lines;
};
#endif // ADDRESS_H_
Main File:
#include "address.h"
#include <iostream>
#include <QFile>
#include <sstream>
#include <fstream>
#include <QStringList>
#include <QString>
#include <QTextStream>
#include <QCoreApplication>
#include <QtWidgets/QInputDialog>
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QLineEdit>
Address::Address(){}
void Address::setLines(QString ad, QString sep){
QStringList lines;
lines = ad.split(sep, QString::SkipEmptyParts);
}
QString Address::getPostalCode() const{
QStringList lines;
return lines.last();
}
QString Address::toString(QString sep) const{
QStringList lines;
return lines.join(sep);
}
bool Address::isPostalCode(QString pc){
if (pc >= "0" && pc <= "9999")
return true;
else
return false;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
while(true)
{
QString userInput = QInputDialog::getText(0,"Address Input","Please
enter your address:");
QLineEdit::EchoMode ok = QLineEdit::Normal;
QString();
if(ok && !userInput.isEmpty())
{
Address ad;
QMessageBox::information(0, "User Address",ad.toString(userInput));
}
}
return 0;
}
I got the same error. Basically, there's an issue with your compiler MingW and qMake. The best suggestion I have is to uninstall QTcreator and everything that references it. And re-install it using the new version: https://www.qt.io/download
This will install everything you need and all the right directories. Hope this helps.
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!
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.
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;
I'm in desperate need of help and direction. Been trying to get this to compile, but battling due to the fact that there are 3 classes and not hundreds on how the includes/forward declarations should work here.
The error is marked in person.h.
In addition to the error, there is a warning I've marked in main.cpp.
This is a console app.
Thank you in advance.
person.h
#ifndef PERSON_H
#define PERSON_H
#include <QString>
class Employer;
class Person
{
public:
Person(QString name);
QString toString() const;
void setPosition(Employer newE, Position newP);
Position getPosition() const;
private:
QString m_Name;
bool m_Employed;
Position m_Position;
Employer m_Employer; //--> ERROR: field `m_Employer' has incomplete type.
};
#endif // PERSON_H
person.cpp
#include "employer.h"
#include "position.h"
#include "person.h"
Person::Person(QString name) : m_Name(name), m_Employed(false), m_Position(""), m_Employer("")
{
}
void Person::setPosition(Employer newE, Position newP)
{
m_Position = newP;
m_Employed = true;
m_Employer = newE;
}
Position Person::getPosition()const
{
return (m_Employed ? m_Position : Position("Professional nose-picker"));
}
QString Person::toString()const
{
return m_Name + ", Employed: " + (m_Employed ? "Yes" : "No") + ", Position: " + getPosition().toString();
}
employer.h
#ifndef EMPLOYER_H
#define EMPLOYER_H
#include <QString>
//class Position;
//class Person;
class Employer
{
public:
Employer(QString name, QString market = "");
void hire(Person &newHire, Position pos);
QString toString() const;
private:
QString m_Name;
QString m_Market;
};
#endif // EMPLOYER_H
employer.cpp
#include "employer.h"
#include "person.h"
#include "position.h"
Employer::Employer(QString name, QString market) : m_Name(name), m_Market(market)
{
}
QString Employer::toString()const
{
return m_Name + (m_Market != "" ? ", Market: " + m_Market : "");
}
void Employer::hire(Person &newHire, Position pos)
{
newHire.setPosition(*this, pos);
}
position.h
#ifndef POSITION_H
#define POSITION_H
#include <QString>
class Position
{
public:
Position(QString name, QString desc = "");
QString toString() const;
private:
QString m_Name;
QString m_Desc;
};
#endif // POSITION_H
position.cpp
#include "position.h"
Position::Position(QString name, QString desc)
:m_Name(name), m_Desc(desc)
{}
QString Position::toString()const
{
return m_Name + (m_Desc != "" ? ", Description: " + m_Desc : "");
}
main.cpp
#include <QtCore/QCoreApplication>
#include <QTextStream>
#include "position.h"
#include "person.h" //--WARNING -> In file included from main.cpp:4:
#include "employer.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextStream cout(stdout);
Person bob("Bob");
Employer devCo("Dev Co");
cout << bob.toString() << endl;
bob.setPosition(is, Position("Developer", "Software Eng"));
cout << bob.toString() << endl;
cout << devCo.toString() << endl;
return a.exec();
}
The Person class needs to know all about Employer and so needs Employer.h included in it rather than the class forward declaration.
In this case the person needs to know the size of the Employee class as you have an Employee stored inside the class.
If you wanted to decouple the classes and just use a forward declaration then Person would have to contain a pointer (raw or smart) to the employer.
You need to take a look at things to consider when using forward declaration. By using forward declaration, you not only break dependencies, but also speed up the compilation by not having to include lots of headers.
The solution for your specific problem is like this :
#ifndef PERSON_H
#define PERSON_H
#include <QString>
class Employer;
class Person
{
public:
Person(QString name);
QString toString() const;
void setPosition(Employer &newE, Position newP);
Position getPosition() const;
private:
QString m_Name;
bool m_Employed;
Position m_Position;
Employer *m_Employer;
};
#endif // PERSON_H
Thanks Guys! I ended up moving #include "employer.h" and
include "position.h" to the person.h and from the person.cpp.
And then fixed a few other errors that then came up.
Looks like I'm battling with when and where to use includes or forward directives. Need to read up on it some more.
I like where you guys are going with the pointer idea.