#include "httpui.h"
HttpUi
::HttpUi(QWidget *parent, Qt
::WFlags flags
){
ui.setupUi(this);
nam = new QNetworkAccessManager(this);
HideWidgets();
connect(ui.getButton,SIGNAL(clicked()),this,SLOT(activateGetWidgets()));
connect(ui.submitButton,SIGNAL(clicked()),this,SLOT(DoHttpGet()));
connect(ui.resetButton,SIGNAL(clicked()),this,SLOT(clearWidgets()));
connect(nam,SIGNAL(finished(QNetworkReply*)),this,SLOT(finished(QNetworkReply*)));
connect(ui.postButton,SIGNAL(clicked()),this,SLOT(activatePostWidgets()));
connect(nam, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)), this, SLOT(onProxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)));
}
HttpUi::~HttpUi()
{
}
void HttpUi
::onProxyAuthenticationRequired(const QNetworkProxy &prox, QAuthenticator
*auth
) {
auth->setUser("myuser");
auth->setPassword("mypassword");
}
void HttpUi
::changeEvent(QEvent *e
) {
switch (e->type()) {
ui.retranslateUi(this);
break;
default:
break;
}
}
void HttpUi::activateGetWidgets()
{
ui.urlLabel->setHidden(false);
ui.urlLine->setHidden(false);
ui.submitButton->setHidden(false);
ui.textBrowser->setHidden(false);
ui.responseTitleLabel->setHidden(false);
ui.getButton->setHidden(true);
ui.postButton->setHidden(true);
}
void HttpUi::activatePostWidgets()
{
ui.dataLabel->setHidden(false);
ui.dataLine->setHidden(false);
activateGetWidgets();
}
void HttpUi::finished(QNetworkReply *reply)
{
if(reply->error() == QNetworkReply::NoError)
{
ui.textBrowser->setText(reply->readAll());
}
else
{
ui.textBrowser->setText(reply->errorString());
}
}
void HttpUi::DoHttpGet()
{
ui.resetButton->setHidden(false);
QString data
= ui.
dataLine->text
();
postData.append(data.toAscii());
if(postData.isEmpty() == true)
{
nam
->get
(QNetworkRequest
(QUrl(url
)));
}
else
{
nam
->post
(QNetworkRequest
(QUrl(url
)),postData
);
}
}
void HttpUi::HideWidgets()
{
ui.urlLabel->setHidden(true);
ui.urlLine->setHidden(true);
ui.dataLabel->setHidden(true);
ui.dataLine->setHidden(true);
ui.submitButton->setHidden(true);
ui.responseTitleLabel->setHidden(true);
ui.textBrowser->setHidden(true);
ui.resetButton->setHidden(true);
}
void HttpUi::clearWidgets()
{
ui.urlLabel->setHidden(true);
ui.urlLine->setHidden(true);
ui.dataLabel->setHidden(true);
ui.dataLine->setHidden(true);
ui.submitButton->setHidden(true);
ui.responseTitleLabel->setHidden(true);
ui.textBrowser->setHidden(true);
ui.resetButton->setHidden(true);
ui.urlLine->clear();
ui.textBrowser->clear();
ui.dataLine->clear();
ui.getButton->setHidden(false);
ui.postButton->setHidden(false);
}
#include "httpui.h"
HttpUi::HttpUi(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
nam = new QNetworkAccessManager(this);
HideWidgets();
connect(ui.getButton,SIGNAL(clicked()),this,SLOT(activateGetWidgets()));
connect(ui.submitButton,SIGNAL(clicked()),this,SLOT(DoHttpGet()));
connect(ui.resetButton,SIGNAL(clicked()),this,SLOT(clearWidgets()));
connect(nam,SIGNAL(finished(QNetworkReply*)),this,SLOT(finished(QNetworkReply*)));
connect(ui.postButton,SIGNAL(clicked()),this,SLOT(activatePostWidgets()));
connect(nam, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)), this, SLOT(onProxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)));
}
HttpUi::~HttpUi()
{
}
void HttpUi::onProxyAuthenticationRequired(const QNetworkProxy &prox, QAuthenticator *auth)
{
auth->setUser("myuser");
auth->setPassword("mypassword");
}
void HttpUi::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui.retranslateUi(this);
break;
default:
break;
}
}
void HttpUi::activateGetWidgets()
{
ui.urlLabel->setHidden(false);
ui.urlLine->setHidden(false);
ui.submitButton->setHidden(false);
ui.textBrowser->setHidden(false);
ui.responseTitleLabel->setHidden(false);
ui.getButton->setHidden(true);
ui.postButton->setHidden(true);
}
void HttpUi::activatePostWidgets()
{
ui.dataLabel->setHidden(false);
ui.dataLine->setHidden(false);
activateGetWidgets();
}
void HttpUi::finished(QNetworkReply *reply)
{
if(reply->error() == QNetworkReply::NoError)
{
ui.textBrowser->setText(reply->readAll());
}
else
{
ui.textBrowser->setText(reply->errorString());
}
}
void HttpUi::DoHttpGet()
{
ui.resetButton->setHidden(false);
QString url = ui.urlLine->text();
QString data = ui.dataLine->text();
QByteArray postData;
postData.append(data.toAscii());
if(postData.isEmpty() == true)
{
nam->get(QNetworkRequest(QUrl(url)));
}
else
{
nam->post(QNetworkRequest(QUrl(url)),postData);
}
}
void HttpUi::HideWidgets()
{
ui.urlLabel->setHidden(true);
ui.urlLine->setHidden(true);
ui.dataLabel->setHidden(true);
ui.dataLine->setHidden(true);
ui.submitButton->setHidden(true);
ui.responseTitleLabel->setHidden(true);
ui.textBrowser->setHidden(true);
ui.resetButton->setHidden(true);
}
void HttpUi::clearWidgets()
{
ui.urlLabel->setHidden(true);
ui.urlLine->setHidden(true);
ui.dataLabel->setHidden(true);
ui.dataLine->setHidden(true);
ui.submitButton->setHidden(true);
ui.responseTitleLabel->setHidden(true);
ui.textBrowser->setHidden(true);
ui.resetButton->setHidden(true);
ui.urlLine->clear();
ui.textBrowser->clear();
ui.dataLine->clear();
ui.getButton->setHidden(false);
ui.postButton->setHidden(false);
}
To copy to clipboard, switch view to plain text mode
The code is from
Bookmarks