×
Namespaces

Variants
Actions
Revision as of 14:31, 1 April 2011 by kiran10182 (Talk | contribs)

How to do HTTP Get and Post using Qt

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Nokia 5800 XpressMusic,Nokia N97, Nokia E72
Compatibility
Platform(s): S60 3rd Edition, FP1, FP2
S60 5th Edition
Symbian
Article
Keywords: QNetworkAccessManager,QNetworkReply,QNetworkRequest,QUrl,QString,QLabel,QByteArray
Created: (25 Mar 2010)
Last edited: kiran10182 (01 Apr 2011)


Contents

Overview

This article will help the developer to write an Qt program which performs the action of HTTP Get and Post. These two terms are very important when an web based application needs to be developed.

This is a very basic article this can be extended to suite the developers need.

Prerequisite

Note.pngNote: 

  • This code snippet is developed using carbide C++ v2.4.
  • One can use Qt creator for developing this sort of application please go through this artcile for more details.


capability required

  • NetworkServices

UI design (.ui file)

The below screenshot shows the ui design file.

QtGetPost3.JPG

Header File

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QMainWindow>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QMessageBox>
#include<QByteArray>
 
 
namespace Ui {
class MainWindow;
}
 
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
 
protected:
void changeEvent(QEvent *e);
 
private slots:
void finished(QNetworkReply *reply);
void DoHttpGet();
void activateGetWidgets();
void HideWidgets();
void clearWidgets();
void activatePostWidgets();
 
private:
Ui::MainWindow *ui;
 
QNetworkAccessManager *nam;
};
 
#endif // MAINWINDOW_H

Source File

#include "mainwindow.h"
#include "ui_mainwindow.h"
 
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
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()));
}
 
MainWindow::~MainWindow()
{
delete ui;
}
 
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
 
void MainWindow::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 MainWindow::activatePostWidgets()
{
ui->dataLabel->setHidden(false);
ui->dataLine->setHidden(false);
activateGetWidgets();
 
}
 
 
 
void MainWindow::finished(QNetworkReply *reply)
{
if(reply->error() == QNetworkReply::NoError)
{
ui->textBrowser->setText(reply->readAll());
 
}
else
{
ui->textBrowser->setText(reply->errorString());
}
}
 
void MainWindow::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 MainWindow::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 MainWindow::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);
 
}

Output Screenshots

  • Initial Screen Asking the user to select and http get or post.


QtGetPost1.jpg

  • Screenshot after user has clicked the get button.


QtGetPost2.jpg

  • Screenshot after user has clicked the post button.


QtGetPost4.jpg


Sample Application


Reference Links

QNetworkAccessManager

598 page views in the last 30 days.