我正在尝试使用以下使用 Qt 库的代码读取 html 页面
#include <QtWidgets/QApplication>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkAccessManager>
#include <QtCore/QString>
#include <iostream>
class SillyRequest : public QNetworkAccessManager
{
Q_OBJECT
public:
SillyRequest();
QString getData();
public slots:
void replyFinished(QNetworkReply *);
private:
QString collectedData;
};
QString SillyRequest :: getData()
{
return collectedData;
}
SillyRequest :: SillyRequest()
{
connect(this, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
}
void SillyRequest :: replyFinished(QNetworkReply *reply)
{
collectedData = reply->readAll();
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
SillyRequest *network = new SillyRequest;
QNetworkRequest request;
request.setUrl(QUrl("http://qt.nokia.com"));
request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
QNetworkReply *reply = network->get(request);
std :: cout << network->getData().toStdString() << std :: endl;
return app.exec();
}
编译时出现以下错误。我不确定我没有以正确的方式做些什么。
testing.cpp:(.text+0x73): undefined reference to `vtable for SillyRequest'
collect2: error: ld returned 1 exit status
Makefile:192: recipe for target 'network' failed
make: *** [network] Error 1
我继承自 QNetworkAccessManager 以实现“replyFinished”插槽。信号“完成”已经是“QNetworkAccessManager”的一部分。