×
Namespaces

Variants
Actions
(Difference between revisions)

Connection state aware applications for Maemo 5 with Qt Mobility

From Nokia Developer Wiki
Jump to: navigation, search
divanov (Talk | contribs)
(Created page with 'Category:Qt for MaemoCategory:Code ExamplesCategory:How To == Introduction == Currently, at the moment Maemo 5 Select connection dialog appears on every attempt to …')
 
divanov (Talk | contribs)
Line 1: Line 1:
[[Category:Qt for Maemo]][[Category:Code Examples]][[Category:How To]]
+
[[Category:Qt for Maemo]][[Category:Qt Mobility]][[Category:Code Examples]][[Category:How To]]
  
 
== Introduction ==
 
== Introduction ==

Revision as of 17:31, 12 July 2010


Introduction

Currently, at the moment Maemo 5 Select connection dialog appears on every attempt to connect to the network User decisions are not remembered thus application easily may became unusable. There are bugs filed to Qt on this issues http://bugreports.qt.nokia.com/browse/QTBUG-10343 http://bugreports.qt.nokia.com/browse/QTBUG-10503

Before Qt 4.7 is released Qt applications have to workaround this bug.

Solution

One of the possible solutions is to use Qt Mobility Bearer Management. It's available in PR 1.2 release.

Project file should enable mobility

TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
 
QT += network
 
CONFIG += mobility
MOBILITY = bearer
 
# Input
HEADERS += netmanager.h
SOURCES += main.cpp netmanager.cpp

A very basic main.cpp

#include <QApplication>
 
#include "netmanager.h"
 
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
 
NetManager netManager;
netManager.show();
 
return a.exec();
}

NetManager header file:

#ifndef NET_MANAGER_H
#define NET_MANAGER_H
 
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QLabel>
#include <QPushButton>
#include <QWidget>
#include <QNetworkConfigurationManager>
#include <QNetworkSession>
using namespace QtMobility;
 
class NetManager : public QWidget
{
Q_OBJECT
 
public:
NetManager(QWidget *parent=0);
 
private slots:
void started();
void onlineStateChanged(bool isOnline);
void clicked(bool checked = false);
void finished(QNetworkReply* reply);
 
private:
QPushButton *button;
QLabel *label;
QNetworkAccessManager *nam;
QNetworkConfigurationManager *configManager;
QNetworkSession *session;
};
 
#endif //NET_MANAGER_H

Implementation of NetManager

#include <QDebug>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QCoreApplication>
#include <QTimer>
#include <QNetworkConfiguration>
 
#include "netmanager.h"
 
NetManager::NetManager(QWidget *parent)
: QWidget(parent), nam(0), session(0)
{
QVBoxLayout *layout = new QVBoxLayout;
button = new QPushButton("Get page");
connect(button, SIGNAL(clicked(bool)), this, SLOT(clicked(bool)));
layout->addWidget(button);
label = new QLabel("");
layout->addWidget(label);
setLayout(layout);
 
configManager = new QNetworkConfigurationManager(this);
QTimer::singleShot(0, this, SLOT(started()));
}

QTimer::singleShot is used to make sure that slot <started()> is invoked when event loop is running.

void NetManager::started()
{
QCoreApplication::processEvents();
onlineStateChanged(configManager->isOnline());
connect(configManager, SIGNAL(onlineStateChanged(bool)),
this, SLOT(onlineStateChanged(bool)));
}

We have to wait until all events are processed, to make sure that QNetworkConfiguationManager is properly initialised, then we test for connection.

void NetManager::onlineStateChanged(bool isOnline)
{
if (!isOnline) {
QMessageBox::StandardButton reply = QMessageBox::question(this,
"Connection",
"This application requires connection to the Internet. "
"Do you want to connect or quit an application?",
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if (reply == QMessageBox::No)
QCoreApplication::quit();
else {
if(session) {
delete session;
session = 0;
}
QNetworkConfiguration config = configManager->defaultConfiguration();
session = new QNetworkSession(config, this);
session->open();
session->waitForOpened(-1);
onlineStateChanged(configManager->isOnline());
}
}
}

In case connection is not available user is proposed to quit application or choose a valid connection.

The following code is used to initiate network request and process a network reply.

void NetManager::clicked(bool checked)
{
Q_UNUSED(checked);
 
if(!nam) {
nam = new QNetworkAccessManager(this);
connect(nam, SIGNAL(finished(QNetworkReply*)),
this, SLOT(finished(QNetworkReply*)));
}
nam->get(QNetworkRequest(QUrl("http://tycho.usno.navy.mil/cgi-bin/timer.pl")));
}
 
void NetManager::finished(QNetworkReply *reply)
{
if (reply->error() == QNetworkReply::NoError) {
label->setText(reply->readAll());
} else {
label->setText(reply->errorString());
}
 
reply->deleteLater();
}
171 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×