Пишу бота для online игры. Застрял на работе с QWebView и QHttp. Подскажите, как можно, например, отправить запрос на yandex.ru и увидеть ответ в QWebView. .h: | Код | #ifndef SHPONJA_H #define SHPONJA_H
#include <QtGui/QMainWindow> #include <QWebView> #include <QHBoxLayout> #include <QVBoxLayout> #include <QPushButton> #include <QHttp> #include <QUrl> #include <QMessageBox> #include <QProgressBar> #include "ui_shponja.h"
class shponja : public QMainWindow { Q_OBJECT
public: shponja(QWidget *parent = 0); ~shponja();
private slots: void openPage(); void openPage(QString url); void httpRequestFinished(int id, bool error); void httpRequestStarted(int id); void httpRequestDone(bool error);
private: Ui::shponjaClass ui;
QHBoxLayout *hLayout; QVBoxLayout *vLayout; QWebView *webView; QPushButton *btnLogin; QHttp *http; QByteArray data; QProgressBar *progress; };
#endif // SHPONJA_H
|
.cpp: | Код | #include "shponja.h"
shponja::shponja(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this);
webView = new QWebView; hLayout = new QHBoxLayout; vLayout = new QVBoxLayout; btnLogin = new QPushButton("Login"); progress = new QProgressBar;
hLayout->addWidget(btnLogin, 0, Qt::AlignRight); hLayout->addWidget(progress, 1, Qt::AlignRight);
vLayout->addLayout(hLayout); vLayout->addWidget(webView);
ui.centralwidget->setLayout(vLayout);
http = new QHttp;
webView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks); //webView->setUrl(QUrl("http://ya.ru"));
connect(btnLogin, SIGNAL(clicked()), this, SLOT(openPage())); connect(webView,SIGNAL(loadProgressChanged(int)),progress,SLOT(setValue(int))); connect(http, SIGNAL(done(bool)),this, SLOT(httpRequestDone(bool))); connect(http, SIGNAL(requestFinished(int, bool)),this, SLOT(httpRequestFinished(int, bool))); connect(http, SIGNAL(requestStarted(int)),this, SLOT(httpRequestStarted(int))); }
shponja::~shponja() {
}
void shponja::openPage(QString url) { webView->setUrl(QUrl(url)); }
void shponja::openPage() { http = new QHttp;
QHttpRequestHeader header("GET", "/yandsearch");
header.setValue("Host", "yandex.ru"); header.setContentType("application/x-www-form-urlencoded"); http->setHost("yandex.ru"); QString s = "text=hjkj"; http->request(header, s.toUtf8()); }
void shponja::httpRequestStarted(int id) { setWindowTitle("Start"); }
void shponja::httpRequestFinished(int id, bool error) { setWindowTitle("End"); }
void shponja::httpRequestDone(bool error) { if(error) { QMessageBox::information(this, "22", http->error() + "\n" + http->errorString(), QMessageBox::Ok); } else { data = http->readAll(); QMessageBox::information(this, "11", data, QMessageBox::Ok); webView->setHtml(data); } }
|
В самой игрушке нужен POST, но, думаю, замена GET на POST будет работать... httpRequestFinished и httpRequestStarted - не срабатывают.
--------------------
shadowmoon
|