Namespaces
Variants
Actions
(Difference between revisions)

How to do HTTP Get and Post using Qt

Jump to: navigation, search
m
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Qt]]
+
[[Category:Networking]][[Category:Qt]]
{{CodeSnippet
+
{{ArticleMetaData <!-- v1.2 -->
|id=...
+
|sourcecode= [[Media:HttpGet&Post.zip]]
|platform=Qt
+
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|devices= Nokia 5800 XpressMusic,Nokia N97, Nokia E72
+
|devices= Nokia 5800 XpressMusic,Nokia N97, Nokia E72, Nokia N8
|category=Qt
+
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 +
|platform= Qt (tested Qt 4.7 Symbian^3)
 +
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 +
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= QNetworkAccessManager,QNetworkReply,QNetworkRequest,QUrl
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20100325
 +
|author= [[User:Gaba88]]
 +
<!-- The following are not in current metadata -->
 
|subcategory= Networking
 
|subcategory= Networking
|creationdate=March 25, 2010
 
|keywords= QNetworkAccessManager,QNetworkReply,QNetworkRequest,QUrl,QString,QLabel,QByteArray
 
 
}}
 
}}
  
Line 13: Line 28:
 
=Overview=
 
=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.  
+
{{Abstract|This article is a basic [http://doc.qt.nokia.com/4.7/qwidget.html QWidget] application which performs '''HTTP GET''' and '''POST''' for a user specified URL. }}
  
This is a very basic article this can be extended to suite the developers need.
+
This is a very basic article this can be extended to suite the developers need. Note that it can only be used in portrait mode - it does not scale properly when rotated.
  
 
=Prerequisite=
 
=Prerequisite=
  
* Install Qt for Symbian:[[Installing Qt on Symbian]]
+
* Install the [[Qt SDK]]
 
+
{{Note|<b></b>
+
* 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 [http://wiki.forum.nokia.com/index.php/Using_Qt_Creator_With_Symbian article] for more details.
+
}}
+
 
+
  
 
=capability required=
 
=capability required=
Line 39: Line 48:
 
= Header File =
 
= Header File =
  
<code cpp>
+
<code cpp-qt>
 
#ifndef MAINWINDOW_H
 
#ifndef MAINWINDOW_H
 
#define MAINWINDOW_H
 
#define MAINWINDOW_H
Line 82: Line 91:
 
= Source File =
 
= Source File =
  
<code cpp>
+
<code cpp-qt>
 
#include "mainwindow.h"
 
#include "mainwindow.h"
 
#include "ui_mainwindow.h"
 
#include "ui_mainwindow.h"
Line 231: Line 240:
 
=Reference Links=
 
=Reference Links=
  
[http://doc.trolltech.com/4.6/qnetworkaccessmanager.html QNetworkAccessManager]
+
*[http://doc.trolltech.com/4.6/qnetworkaccessmanager.html QNetworkAccessManager]
 +
*See also [http://doc.qt.nokia.com/4.7/network-http.html HTTP Example][[Category:MeeGo Harmattan]] [[Category:Symbian]]
 +
[[Category:Code Examples]]

Latest revision as of 04:17, 11 October 2012

Article Metadata

Code Example
Tested with
Devices(s): Nokia 5800 XpressMusic,Nokia N97, Nokia E72, Nokia N8

Compatibility
Platform(s): Qt (tested Qt 4.7 Symbian^3)

Article
Keywords: QNetworkAccessManager,QNetworkReply,QNetworkRequest,QUrl
Created: gaba88 (25 Mar 2010)
Last edited: hamishwillee (11 Oct 2012)


Contents

Overview

This article is a basic QWidget application which performs HTTP GET and POST for a user specified URL.

This is a very basic article this can be extended to suite the developers need. Note that it can only be used in portrait mode - it does not scale properly when rotated.

Prerequisite

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

This page was last modified on 11 October 2012, at 04:17.
629 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved