Slot and Signals in Windows CE/Mobile
-
Hi all,
I'm trying to check the Internet connection with this code, but it doesn't work:
connectivity_check.h
@#ifndef CONNECTIVITY_CHECK_H
#define CONNECTIVITY_CHECK_H////////////////////////
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
///////////////////////class ConnectivityCheck :public QObject
{
Q_OBJECT
public:
void checkConnection();
private slots:
void finishedSlot(QNetworkReply* reply);
};#endif // CONNECTIVITY_CHECK_H@
connectivity_check.cpp
@#include "connectivity_check.h"
#include <iostream>
using namespace std;
#include "offlineaccesscontrol.h"
#include <QProcess>void ConnectivityCheck::checkConnection(){
QNetworkAccessManager* nam;
nam = new QNetworkAccessManager(this);
QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),this, SLOT(finishedSlot(QNetworkReply*)));//QUrl url(qstr_url.toLatin1());
QUrl url("http://www.google.com");
QNetworkReply* reply = nam -> get(QNetworkRequest(url));
}void ConnectivityCheck::finishedSlot(QNetworkReply* reply)
{//////////////////////////////////////////////////
if (reply->error() == QNetworkReply::NoError){
QByteArray ba = reply->url().toString().toLatin1();
QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
const char *c_str2 = ba.data();
const char *status = statusCodeV.value<QString>().toLatin1();
cout << "no error " << c_str2 << " status: " << status << "\n";
QProcess process;
QString program = QString::fromLatin1("\Program Files\OfflineAccessControl\OfflineAccessControl.exe");
process.startDetached(program);
}else{
QByteArray ba = reply->url().toString().toLatin1();
QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
const char *c_str2 = ba.data();
const char *status = statusCodeV.value<QString>().toLatin1();
cout << "error " << c_str2 << " status: " << status << "\n";QProcess process;
QString program = QString::fromLatin1("\Program Files\OfflineAccessControl\OfflineAccessControl.exe");
process.startDetached(program);
}
//////////////////////////////////////////////////
QVariant statusCodeV;
bool conversion_status = false;
// Reading attributes of the reply
// e.g., the HTTP status code
int status_code = (statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute)).toInt(&conversion_status);
//print the status code of the operation
if (conversion_status)
{
switch (status_code)
{
case 200:
cout << "Http status code : 200 : OK\n";
break;
case 304:
cout << "Http status code : 300 : Redirection\n";
break;
case 404:
cout << "Http status code : 404 : Not Found\n";
break;
case 401:
cout << "Http status code : 401 : Unauthorized\n";
break;
default:
const char *status = QString::number(status_code).toLatin1();
cout << "Http status code : " << status << "\n";
break;
}
}
else //error on conversion.
{
cout << "Error: conversion of status code.\n";
return;
}
// if no error received
if (reply->error() == QNetworkReply::NoError)
{
QByteArray data = reply->readAll();
const char *c_str2 = data.data();
cout << "No error: " << c_str2 << "\n";
}
// Some http error received
else //handle errors here
{
const char *error = reply->errorString().toLatin1();
cout << "Error: " << error << "\n";
}delete reply;
}@And I call it from main.cpp:
@ConnectivityCheck check;
check.checkConnection();@Any idea?
Thanks!!
-
I don't know if this will solve your problem, but from the docs of QNetworkReply:
Note: Do not delete the object in the slot connected to the error() or finished() signal. Use deleteLater().
-
It looks fine from the code, however did u tried same code from ur desktop ? does it worked ?
-
Yes, I tried it in a dummy project (desktop test and mobile test) and it worked, but I "pasted" in my mobile project and it doesn't work. Why???
Permissions? Security? Windows Mobile 6.5 special feature?
-
Do u get timeout at least from windows mobile?
@ConnectivityCheck check;
check.checkConnection();@This is ur main, what is after and before this ? i mean if checkConnection() call is not blocked then ConnectivityCheck might go out of scope.
Also try
@ void finishedSlot(QNetworkReply*); @
instead
@ void finishedSlot(QNetworkReply* reply);@ -
I achieved get a request putting in my main.cpp this:
@ConnectivityCheck check;
check.checkConnection();
app.exec();@But now I can't do this because I must to retry n attempts if connexion is unsuccessfull and doing something like this:
@while((char_coreat > count) && (tempMode != MODE_ONLINE)){
checkConnection(qstr_url);
count = count + 1;
}@It checks the connection "char_coreat" attempts if tempMode is not MODE_ONLINE. tempMode is changed to this state if connection is successful into the finishedslot function. So,
@app.exec();@ is not called until while loop ends.Also I tried writting @void finishedSlot(QNetworkReply*);@ in header file with the same results. I have to do something else?
I hope I explained well. Any idea?
Thanks!
-
Vishwajeet,
You said:
@ i mean if checkConnection() call is not blocked then ConnectivityCheck might go out of scope.@
How can I block the call?
Thank you
-
Block in the sense calling @app.exec();@ that will start your event loop, that also mean signal and slot will start working. which i see now you are calling but after some loop. which is not required here. you can make use of signal slots only.
Could also check if the application from Qt example examples\network\http works ?
-
I'm until in the same situation... slots from @nam -> get(QNetworkRequest(url));@ execute after loop ends...
I've seen this method:
@http://doc.trolltech.com/4.7/qcoreapplication.html#processEvents@I thought that it'd be useful for me, but no...I put it in many places of my code (after connect:... line for example)
Any idea? Thanks
-
I did it!!
I re-thought the algorithm of my piece of code and now it works. But I have another problem. The url which I try to connect is with my localhost server and works fine if server is running, but if I shutdown the server the slot is not launched....why?
My "connect" line is:
@connect(reply, SIGNAL(finished()), this, SLOT(readySlot())); @And I tried too:
@connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(readySlot()));@I mean, readySlot is only launched if my localhost server is running. So, I don't catch if connection fail.
Any idea? Thank you!