dj8000
dj8000

Reputation: 209

QT: unresolved external symbol while trying to get webpage source code

I started developing with Qt and I'm trying to get webpage source code. Here's what I have :

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QString>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkAccessManager>
#include <QUrl>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

/**/

class GetHTMLSource : public QObject
{
Q_OBJECT

public:
    GetHTMLSource();
    void GetSource();

public slots:
    void GetDone(QNetworkReply*);

private:
    QNetworkAccessManager* NetManager;
};


GetHTMLSource::GetHTMLSource()
{
    NetManager = new QNetworkAccessManager(this);

    connect(NetManager, SIGNAL(finished(QNetworkReply*)),
         this, SLOT(GetDone(QNetworkReply*)));

}

void GetHTMLSource::GetSource()
{
    NetManager->get(QNetworkRequest(QUrl("http://stackoverflow.com")));
}

void GetHTMLSource::GetDone(QNetworkReply* ReplyIn)
{

    QByteArray DataIn=ReplyIn->readAll();
    QString DataString(DataIn);

    //process str any way you like!

}

/**/

void MainWindow::on_pushButton_clicked()
{
    GetHTMLSource Test;
    Test.GetSource();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

.pro

QT       += core gui

TARGET = TestGetPageSource
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

QT += network

Errors are :

mainwindow.obj:-1: error:LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall GetHTMLSource::metaObject(void)const " (?metaObject@GetHTMLSource@@UBEPBUQMetaObject@@XZ)

mainwindow.obj:-1: error:LNK2001: unresolved external symbol "public: virtual void * __thiscall GetHTMLSource::qt_metacast(char const *)" (?qt_metacast@GetHTMLSource@@UAEPAXPBD@Z)

mainwindow.obj:-1: error:LNK2001: unresolved external symbol "public: virtual int __thiscall GetHTMLSource::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@GetHTMLSource@@UAEHW4Call@QMetaObject@@HPAPAX@Z)

Upvotes: 3

Views: 1370

Answers (2)

dj8000
dj8000

Reputation: 209

Fixed it on my own. Of course it was my stupid mistake.

In class definition :

public slots:
    void GetDone(QNetworkReply*);

was not matching actual function :

void GetHTMLSource::GetDone(QNetworkReply* ReplyIn)
{ ... }

However moving it to seperated .h and .cpp files probably helped.

Upvotes: 1

MichK
MichK

Reputation: 3252

Generally if you create classes derived from QObject you have to put them in separate header and source files.

So one .h and one .cpp file for one class.

Upvotes: 1

Related Questions