#include <QtGui/QApplication>#include <QtGui/QMainWindow>#include <QtGui/QFrame>#include <QtGui/QListView>#include <QtGui/QStringListModel>#include <QStringList>#include "network.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); QMainWindow * main = new QMainWindow(); QFrame * mainframe = new QFrame(); mainframe->setFrameShape(QFrame::Box); mainframe->setParent(main); mainframe->show(); QStringListModel * mdl = new QStringListModel(); QStringList * slist = new QStringList(); network * nw = new network(); nw->getserveraddr(&*slist); mdl->setStringList(*slist); QListView * list = new QListView(); list->setModel(mdl); list->setGeometry(10,10,620,460); list->setParent(mainframe); list->show(); list->setViewMode(QListView::ListMode); main->setFixedSize(640, 480); main->setWindowTitle("IM"); main->setCentralWidget(mainframe); main->show(); return a.exec();}
#ifndef NETWORK_H#define NETWORK_H#include <QtNetwork/QHttp>#include <QStringList>using namespace std;class network : public QHttp{ Q_OBJECTpublic: network(); void getserveraddr(QStringList *slist);public slots: void returnserveraddr();private: QStringList * rlist; QString defserver_addr; qint16 defserer_port;};#endif // NETWORK_H
#include "network.h"network::network(){ this->defserver_addr = "mrim.mail.ru"; this->defserer_port = 2042;}void network::getserveraddr(QStringList *slist){ this->rlist = &*slist; *(this->rlist) << "foo"; this->connect(this, SIGNAL(done(bool)), this, SLOT(returnserveraddr()), Qt::AutoConnection); this->setHost(this->defserver_addr, this->defserer_port); this->get("/");}void network::returnserveraddr(){ qDebug() << this->readAll(); *(this->rlist) << "bar";}
#ifndef NETWORK_H#define NETWORK_H#include <QtNetwork>#include <QStringList>#include <QBuffer>class Network : QObject{ Q_OBJECTpublic: Network(QObject *parent=0); void getserveraddr(QStringList *slist);public slots: void on_requestFinished(int requestId, bool error); void on_readyRead(QHttpResponseHeader responseHeader); void on_done(bool error); void on_requestStarted(int id); void on_stateChanged(int state); void on_responseHeaderReceived(QHttpResponseHeader responseHeader); void on_sslErrors(QList<QSslError> errors); void on_authenticationRequired(QString hostname, quint16 port, QAuthenticator * authenticator);private: int getId; QHttp * http; QStringList * rlist; QString defserver_addr; qint16 defserer_port;};#endif // NETWORK_H
#include "network.h"Network::Network(QObject *parent) : QObject(parent){ this->defserver_addr = "mrim.mail.ru"; this->defserer_port = 2042; this->http = new QHttp();}void Network::getserveraddr(QStringList *slist){ connect(this->http, SIGNAL(requestFinished(int, bool)), this, SLOT(on_requestFinished(int, bool))); connect(this->http, SIGNAL(readyRead(QHttpResponseHeader)), this, SLOT(on_readyRead(QHttpResponseHeader))); connect(this->http, SIGNAL(done(bool)), this, SLOT(on_done(bool))); connect(this->http, SIGNAL(requestStarted(int)), this, SLOT(on_requestStarted(int))); connect(this->http, SIGNAL(stateChanged(int)), this, SLOT(on_stateChanged(int))); connect(this->http, SIGNAL(responseHeaderReceived(QHttpResponseHeader)), this, SLOT(on_responseHeaderReceived(QHttpResponseHeader))); connect(this->http, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(on_sslErrors(QList<QSslError>))); connect(this->http, SIGNAL(authenticationRequired(QString,quint16,QAuthenticator*)), this, SLOT(on_authenticationRequired(QString,quint16,QAuthenticator*))); this->http->setHost(this->defserver_addr, QHttp::ConnectionModeHttp, this->defserer_port); this->getId = this->http->get("/"); qDebug() << "Get id: " << this->getId;}void Network::on_requestFinished(int requestId, bool error){ qDebug() << "Request " << requestId << " finished with error: " << error; if(requestId == this->getId) qDebug() << "Data: " << this->http->readAll();}void Network::on_readyRead(QHttpResponseHeader responseHeader){ qDebug() << "Ready read"; qDebug() << "Response status: " << responseHeader.statusCode();}void Network::on_done(bool error){ qDebug() << "Done";}void Network::on_requestStarted(int id){ qDebug() << "Request " << id << " started";}void Network::on_stateChanged(int state){ switch(state) { case 0: qDebug() << "State changed to: Unconnected"; return; case 1: qDebug() << "State changed to: HostLookup"; return; case 2: qDebug() << "State changed to: Connecting"; return; case 3: qDebug() << "State changed to: Sending"; return; case 4: qDebug() << "State changed to: Reading"; return; case 5: qDebug() << "State changed to: Connected"; return; case 6: qDebug() << "State changed to: Closing"; return; }}void Network::on_responseHeaderReceived(QHttpResponseHeader responseHeader){ qDebug() << "Response header received"; qDebug() << "Status code: " << responseHeader.statusCode();}void Network::on_sslErrors(QList<QSslError> errors){ qDebug() << "SSL Errors occured";}void Network::on_authenticationRequired(QString hostname, quint16 port, QAuthenticator * authenticator){ qDebug() << "Auth required";}
void TCPNetwork::on_readyread(){ qDebug() << "Ready to read"; qDebug() << "Data: " << this->socket.readAll();}
void TCPNetwork::on_readyread(){ qDebug() << "Ready to read"; QDataStream in(&(this->socket)); in.setVersion(QDataStream::Qt_4_1); QString data; in >> data; qDebug() << "Data: " << data;}