PDA

View Full Version : QNetworkAccessManager problem. It send's nothing



stepchik
1st April 2011, 11:41
Hello!!! I have a trouble with QNetworkAccessManager.
I created new Qt project and use Sample code. It works perfect. But when i try to past my code into existing project. "manager->get(request)" send nothing and finished slot's dosn't fire. I use QT4.6 and Visual Studio 2008. May be i forget to include some directive or debugger option? Another not network slot's work's perfect. All network slots(slotError,slotReadyRead) dosn't fire. Checkpoint.php add's visit record to text file. May be QNetworkAccessManager conflict with existing project?

;)I will be very happy if someone could help.


const QString url= "http://127.0.0.6/checkpoint.php";
QNetworkRequest request;
request.setUrl(QUrl(url));
request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
qDebug("running");

manager = new QNetworkAccessManager(this);
QNetworkReply *reply = manager->get(request);

QObject::connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
QObject::connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyHasFinished()));
QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),this, SLOT(slotError(QNetworkReply::NetworkError)));

QObject::connect(reply, SIGNAL(sslErrors(QList<QSslError>)),this, SLOT(slotSslErrors(QList<QSslError>)));

....
void MyClass::replyHasFinished(){
AfxMessageBox(_T("replyHasFinished WORKS"));
}

stampede
1st April 2011, 15:35
Try to declare the replyHasFinished slot with QNetworkReply* parameter:


MyClass{
...
public slots:
void replyHasFinished( QNetworkReply* );
...

Show the header for MyClass, maybe replyHasFinished is not declared as slot at all ?
Maybe you forgot the Q_OBJECT macro ?

stepchik
1st April 2011, 16:05
I try to add ( QNetworkReply* ). It did not solved my problem. Request dos't send (the record of checkpoint file is empty) and replyHasFinished(QNetworkReply*) Slot has not been fired.

Now MyClass is:



class MyClass : public QMainWindow
{
Q_OBJECT

public:
OptiroamLogic(QCommandLinkButton *commandLinkButton,QLCDNumber *lcdnumber,QWidget *parent = 0, Qt::WFlags flags = 0);
QNetworkReply *reply;
QNetworkAccessManager *manager;

private:
// QNetworkAccessManager *manager;
QCommandLinkButton *topUp;
QLCDNumber *displayBalance;

private:
QString parseXML(QString str);

public slots:
void btnRunBrowser_clicked();
void replyHasFinished(QNetworkReply*);
void sendBalanceRequest();
void slotReadyRead();
void slotError(QNetworkReply::NetworkError);
};
}; // namespace Tel


May be the reason is in preprocessor Difinitions:

In work project they are : "UNICODE;WIN32;QT_LARGEFILE_SUPPORT;QT_NO_DEBUG;NDE BUG;QT_CORE_LIB;QT_GUI_LIB;QT_XMLPATTERNS_LIB"

In "enemy" project was:
"WIN32;_DEBUG;_WINDOWS;_USRDLL;LIBYQT4_EXPORTS;UNIC ODE"

I try to play with them. Now:
"WIN32;_DEBUG;_WINDOWS;_USRDLL;LIBYQT4_EXPORTS;UNIC ODE;QT_CORE_LIB;QT_GUI_LIB;QT_XMLPATTERNS_LIB"

As i see there are not something "QT_NETWORK_LIB". But in "work" project it works. The head of class.h file is


#ifdef _WINDOWS

#ifdef LIBYQT4_EXPORTS
#define YQT4_API __declspec(dllexport)
#else
#ifndef LIBYQT4_STATIC
//#define YQT4_API __declspec(dllimport)
#endif
#endif

#endif /* _WINDOWS */

#ifndef YQT4_API
#define YQT4_API
#endif

#undef open
#undef read
#undef close
#undef write
#undef mkdir
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

//#define QT_NO_DEBUG
//#define QT_DLL
//#define QT_GUI_LIB
//#define QT_CORE_LIB
//#define QT_THREAD_SUPPORT

#include <QtGui>
#include <QSound>
#include <QNetworkReply>


And in code i saw something like "one thread supply" - may be this is the reason?

I gonna try to send request throw InternetOpenUrl() and see "checkpoint" file.

wysota
1st April 2011, 18:57
Do you have a Qt event loop running?

stepchik
4th April 2011, 08:28
I haven't event loop. it looks like I understood my mistake. :confused:
QNetworkReply *reply = manager->get(request);
When i go to "get" definition "std::num_get<Elem,Init::get(...)>". Wrong path.

ChrisW67
4th April 2011, 08:57
If you have no Qt event loop running then QNetworkAccessManager is going to have a hard time processing your queued requests and sending the notification of completion. If you haven't got the connections between signal and slot right, especially the QNetworkReply* argument, then you should receive a warning to that effect in your console when the program runs.

The stuff regarding std::num_get has nothing to do with this.

stepchik
5th April 2011, 08:52
going to have a hard time processing your queued requests and sending the notification of completion

Hard time? How mach 5 minutes? May be a half of an our? Does it mean that finally it sends request?

I'll try to move "request send code" to another process. It does't work yet. Can you explain where should i place event loop? Should i place in run() function?

Something like that?

void MyThread::run(){
QEventLoop loop;
AfxMessageBox(_T("My Thread"));
QNetworkAccessManager *_manager = new QNetworkAccessManager();
QNetworkRequest *_request = new
QNetworkRequest(QUrl("http://127.0.0.6/checkpoint.php"));
QNetworkReply *_reply = _manager->QNetworkAccessManager::get(*_request);
QObject::connect(_reply, SIGNAL(readyRead()), this, SLOT(slot_finished()));
QObject::connect(_reply, SIGNAL(finished()), this, SLOT(slot_finished()));
QObject::connect(_reply, SIGNAL(error()), this, SLOT(slot_finished()));
loop.exec();
}

stepchik
11th April 2011, 12:44
As i understood the reason is fact that my code compiling as DLL. Before i try to use QEventLoop, Qtime with timeout(0). QEventLoop dosn't work. Qtime timeout_slot works, but QNetworkAccessManager send's nothing.

stepchik
19th April 2011, 08:17
Finally the problem has not been solved. I had to use CURL.