QT:尝试获取网页源代码时未解析的外部符号

时间:2012-11-07 20:45:20

标签: qt webpage symbols unresolved-external

我开始使用Qt开发,我正在尝试获取网页源代码。这就是我所拥有的:

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

错误是:

  

mainwindow.obj:-1:错误:LNK2001:未解析的外部符号“public:   virtual struct QMetaObject const * __thiscall   GetHTMLSource :: metaObject(void)const“   (?元对象@ @@ GetHTMLSource @@ UBEPBUQMetaObject XZ)

     

mainwindow.obj:-1:错误:LNK2001:未解析的外部符号“public:   virtual void * __thiscall GetHTMLSource :: qt_metacast(char const *)“   (?qt_metacast @ @@ GetHTMLSource @ UAEPAXPBD Z)

     

mainwindow.obj:-1:错误:LNK2001:未解析的外部符号“public:   virtual int __thiscall GetHTMLSource :: qt_metacall(enum   QMetaObject :: Call,int,void * *)“   (?qt_metacall @ @@ GetHTMLSource @ UAEHW4Call @@ QMetaObject @ HPAPAX Z)

2 个答案:

答案 0 :(得分:1)

通常,如果您创建派生自QObject的类,则必须将它们放在单独的头文件和源文件中。

一个类的.h和一个.cpp文件。

答案 1 :(得分:1)

我自己修复了它。当然这是我的愚蠢错误。

在课堂定义中:

public slots:
    void GetDone(QNetworkReply*);

与实际功能不匹配:

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

然而将它移动到分离的.h和.cpp文件可能会有所帮助。