hayati
22nd March 2007, 15:15
I want to get a php page via QUrlOperator, but url should be entered by end users. So url may belong to unreachable host. QT Docs states that QUrlOperator always - whether operation succeeds or fails - emits a finished signal. But amazingly i don't get always. Actually after a while i don't get anymore.
More info to repliers:
1- qInitNetwork... function is in the main so it's called.
2- I get finished signals at least once.
i'll also give you some source code to test it or find what's wrong with it.
the program works as; one widget (which is mainwidget of application) opens another widget with a timeout continuously, then the opened widget tries to reach a url which is unreachable. When it gets finished signal it closes itself.
So it works for five or more times, but after an unknown times of timer timeout program does not get finished signals.
Notes:
FileReader_ and MainUI_ are subclasses of QWidget it doesn't have any other subwidgets or operations.
// header which uses qurloperator
#ifndef FILEREADER_H
#define FILEREADER_H
#include "FileReader_.h"
class QUrlOperator;
class QNetworkOperation;
class FileReader : public FileReader_
{
Q_OBJECT
public:
FileReader(QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
~FileReader();
public slots:
void stateChangedSlot(int state,const QString&);
void finishedSlot(QNetworkOperation *operation);
private:
QUrlOperator* m_op;
};
#endif
// qurloperator using class's imp.
#include <qurloperator.h>
#include <qnetwork.h>
#include <iostream>
#include "filereader.h"
using namespace std;
FileReader::FileReader(QWidget* parent, const char* name, WFlags fl)
: FileReader_(parent,name,fl)
{
// host part of url, which is here asdfasdf, should be unreachable. So host should not exist on network
m_op = new QUrlOperator("http://asdfasdf/asdf/abcd.php");
connect(m_op, SIGNAL(connectionStateChanged(int,const QString&)), SLOT(stateChangedSlot(int,const QString&)));
connect( m_op, SIGNAL(finished(QNetworkOperation*)), SLOT(finishedSlot(QNetworkOperation*)));
m_op->get();
}
void FileReader::stateChangedSlot(int state,const QString& data)
{
cout << "conn state changed to: " << data.ascii() << endl;
switch( state ){
case QNetworkProtocol::ConConnected:
cout << "CONNECTED" << endl;
break;
case QNetworkProtocol::ConClosed:
cout << "CONNECTION CLOSED" << endl;
break;
}
}
void FileReader::finishedSlot(QNetworkOperation *operation)
{
switch( operation->operation() ){
case QNetworkProtocol::OpGet:
if ( operation->state() == QNetworkProtocol::StFailed ) {
cerr << "finished but there is an error..." << endl;
}
else if ( operation->state() == QNetworkProtocol::StDone ) {
cout << "connection successfully finished..." << endl;
}
}
close(true);
}
FileReader::~FileReader()
{
delete m_op;
}
// header of opening widget
#ifndef MAINUI_H
#define MAINUI_H
#include "MainUI_.h"
class QTimer;
class FileReader;
class MainUI : public Form1
{
Q_OBJECT
public:
MainUI(QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
public slots:
void refreshInfo();
protected:
QTimer* m_timer;
FileReader* fileReader;
};
#endif
// imp.
#include <qtimer.h>
#include "mainui.h"
#include "filereader.h"
MainUI::MainUI(QWidget* parent, const char* name, WFlags fl)
: Form1(parent,name,fl)
{
m_timer = new QTimer(this);
connect(m_timer, SIGNAL(timeout()), SLOT(refreshInfo()));
m_timer->start( 30000 );
fileReader = new FileReader();
fileReader->show();
}
void MainUI::refreshInfo()
{
fileReader = new FileReader();
fileReader->show();
}
// main.cpp
#include <qapplication.h>
#include <qnetwork.h>
#include <stdio.h>
#include <stdlib.h>
#include "mainui.h"
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
qInitNetworkProtocols();
MainUI fr;
app.setMainWidget( &fr );
fr.show();
app.exec();
}
thanks for all replys...
More info to repliers:
1- qInitNetwork... function is in the main so it's called.
2- I get finished signals at least once.
i'll also give you some source code to test it or find what's wrong with it.
the program works as; one widget (which is mainwidget of application) opens another widget with a timeout continuously, then the opened widget tries to reach a url which is unreachable. When it gets finished signal it closes itself.
So it works for five or more times, but after an unknown times of timer timeout program does not get finished signals.
Notes:
FileReader_ and MainUI_ are subclasses of QWidget it doesn't have any other subwidgets or operations.
// header which uses qurloperator
#ifndef FILEREADER_H
#define FILEREADER_H
#include "FileReader_.h"
class QUrlOperator;
class QNetworkOperation;
class FileReader : public FileReader_
{
Q_OBJECT
public:
FileReader(QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
~FileReader();
public slots:
void stateChangedSlot(int state,const QString&);
void finishedSlot(QNetworkOperation *operation);
private:
QUrlOperator* m_op;
};
#endif
// qurloperator using class's imp.
#include <qurloperator.h>
#include <qnetwork.h>
#include <iostream>
#include "filereader.h"
using namespace std;
FileReader::FileReader(QWidget* parent, const char* name, WFlags fl)
: FileReader_(parent,name,fl)
{
// host part of url, which is here asdfasdf, should be unreachable. So host should not exist on network
m_op = new QUrlOperator("http://asdfasdf/asdf/abcd.php");
connect(m_op, SIGNAL(connectionStateChanged(int,const QString&)), SLOT(stateChangedSlot(int,const QString&)));
connect( m_op, SIGNAL(finished(QNetworkOperation*)), SLOT(finishedSlot(QNetworkOperation*)));
m_op->get();
}
void FileReader::stateChangedSlot(int state,const QString& data)
{
cout << "conn state changed to: " << data.ascii() << endl;
switch( state ){
case QNetworkProtocol::ConConnected:
cout << "CONNECTED" << endl;
break;
case QNetworkProtocol::ConClosed:
cout << "CONNECTION CLOSED" << endl;
break;
}
}
void FileReader::finishedSlot(QNetworkOperation *operation)
{
switch( operation->operation() ){
case QNetworkProtocol::OpGet:
if ( operation->state() == QNetworkProtocol::StFailed ) {
cerr << "finished but there is an error..." << endl;
}
else if ( operation->state() == QNetworkProtocol::StDone ) {
cout << "connection successfully finished..." << endl;
}
}
close(true);
}
FileReader::~FileReader()
{
delete m_op;
}
// header of opening widget
#ifndef MAINUI_H
#define MAINUI_H
#include "MainUI_.h"
class QTimer;
class FileReader;
class MainUI : public Form1
{
Q_OBJECT
public:
MainUI(QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
public slots:
void refreshInfo();
protected:
QTimer* m_timer;
FileReader* fileReader;
};
#endif
// imp.
#include <qtimer.h>
#include "mainui.h"
#include "filereader.h"
MainUI::MainUI(QWidget* parent, const char* name, WFlags fl)
: Form1(parent,name,fl)
{
m_timer = new QTimer(this);
connect(m_timer, SIGNAL(timeout()), SLOT(refreshInfo()));
m_timer->start( 30000 );
fileReader = new FileReader();
fileReader->show();
}
void MainUI::refreshInfo()
{
fileReader = new FileReader();
fileReader->show();
}
// main.cpp
#include <qapplication.h>
#include <qnetwork.h>
#include <stdio.h>
#include <stdlib.h>
#include "mainui.h"
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
qInitNetworkProtocols();
MainUI fr;
app.setMainWidget( &fr );
fr.show();
app.exec();
}
thanks for all replys...