做一个qt网络请求

时间:2014-09-27 16:54:04

标签: c++ qt networking

我试图连接到一个网站并用qt执行请求,到目前为止我得到了这个代码,但它告诉我以下错误:

#include <QCoreApplication>
#include <QDebug>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QUrlQuery>

void sendRequest();

int main(int argc, char *argv[]) { 
    QCoreApplication a(argc, argv);
    sendRequest();
    return a.exec();
}

void sendRequest() {    
QNetworkAccessManager *manager = new QNetworkAccessManager(this);   connect(manager, SIGNAL(finished(QNetworkReply*)),      this, SLOT(replyFinished(QNetworkReply*)));

manager->get(QNetworkRequest(QUrl("http://qt-project.org")));
}

它说:

  

错误1错误C2355:&#39;此&#39; :只能在非静态内部引用   成员函数或非静态数据成员   初始化程序c:\ Users \ Synturas \ documents \ visual studio   2013 \ Projects \ qt_request \ qt_request \ main.cpp 19 1 qt_request

     

错误2错误C2355:&#39;此&#39; :只能在非静态内部引用   成员函数或非静态数据成员   初始化程序c:\ Users \ Synturas \ documents \ visual studio   2013 \ Projects \ qt_request \ qt_request \ main.cpp 21 1 qt_request

     

错误3错误C3861:&#39; connect&#39;:标识符不是   找到c:\ Users \ Synturas \ documents \ visual studio   2013 \ Projects \ qt_request \ qt_request \ main.cpp 20 1 qt_request     4智能感知:&#39;这个&#39;只能在非静态成员中使用   function c:\ Users \ Synturas \ Documents \ Visual Studio   2013 \ Projects \ qt_request \ qt_request \ main.cpp 19 61 qt_request

     

5智能感知:标识符&#34;连接&#34;是   undefined c:\ Users \ Synturas \ Documents \ Visual Studio   2013 \ Projects \ qt_request \ qt_request \ main.cpp 20 2 qt_request

     

6智能感知:&#39;这个&#39;只能在非静态成员中使用   function c:\ Users \ Synturas \ Documents \ Visual Studio   2013 \ Projects \ qt_request \ qt_request \ main.cpp 21 3 qt_request

(我正在使用VS2013进行此项目,如果这是相关的)

1 个答案:

答案 0 :(得分:1)

试试这个例子:

解释:你创建了一个新类,它将为你完成所有工作,在main.cpp中创建这个类的实例并调用方法。

*的.h

#ifndef NET_H
#define NET_H

#include <QObject>

#include <QtCore>
#include <QNetworkAccessManager>
#include <QNetworkReply>

class Net : public QObject
{
Q_OBJECT
  QNetworkAccessManager *manager;
private slots:
  void replyFinished(QNetworkReply *);//slot, we want catch responce
public:
  void CheckSite(QString url);//our method
};



#endif // NET_H

*。CPP

#include "net.h"

void Net::replyFinished(QNetworkReply *reply) //this slot called when we have responce
{ 
qDebug() << reply->readAll(); //we read result and print it(also you can save it in some variable and use in code
}

void Net::CheckSite(QString url) 
{
  QUrl qrl(url);//QUrl has network adress
  manager = new QNetworkAccessManager(this);//create manager
  connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));//do connection with nededed signal and slot which we alredy wrote
  manager->get(QNetworkRequest(qrl));//send get request
}

#include "net.h"


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    Net handler;//create instance
    handler.CheckSite("http://qt-project.org");//set path and wait for responce

    return a.exec();
}

结果:Qt网站的HTML代码