void Pict_grab::press_btn_load(){ QNetworkAccessManager *manager = new QNetworkAccessManager(this); connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*))); QNetworkReply* rep1 = manager->get(QNetworkRequest(QUrl("www.google.com"))); connect(rep1, SIGNAL(finished()),this, SLOT(repfinish())); connect(rep1, SIGNAL(readyRead()), this, SLOT(repfinish()));}void Pict_grab::replyFinished(QNetworkReply* fin){ QImage img; QImageReader reader(fin); if (fin->error() == QNetworkReply::NoError) reader.read(&img); else qWarning() << QString("[NetworkingPrivate] Reply error: %1").arg(fin->error()); img.save("c:\\img1.jpg","JPG");}void Pict_grab::repfinish(){int i=0;}
QNetworkAccessManager m;QNetworkReply * reply = m.get( QNetworkRequest( url ) );QEventLoop loop;connect( reply, SIGNAL(finished()), &loop, SLOT(quit()) );loop.exec();if ( reply->error() == QNetworkReply::NoError ){ QFile image( "image.jpg" ); image.open( QIODevice::WriteOnly ); image.write( reply->readAll() );}
QNetworkAccessManager m; QNetworkReply * reply = m.get( QNetworkRequest( QUrl("http://ru.fishki.net/picsw/022013/20/kino/tn.jpg") ) ); QEventLoop loop; connect( reply, SIGNAL(finished()), &loop, SLOT(quit()) ); connect( reply, SIGNAL(error(QNetworkReply::NetworkError )), this, SLOT(load_error(QNetworkReply::NetworkError ))); connect( reply, SIGNAL(uploadProgress(qint64, qint64)), this, SLOT(load_progress( qint64 , qint64 ))); loop.exec(); if ( reply->error() == QNetworkReply::NoError ) { QFile image( "c:\\image.jpg" ); image.open( QIODevice::WriteOnly ); image.write( reply->readAll() ); }
C++ (Qt)#include <QApplication>#include <QImage>#include <QMessageBox>#include <QNetworkAccessManager>#include <QNetworkRequest>#include <QNetworkReply>#include <QNetworkProxy>#include <QPainter>#include <QPaintEvent>#include <QProgressDialog> class ImgViewer : public QWidget{public: ImgViewer( const QImage& img, QWidget* p = 0 ) : QWidget( p ) , bg( img ) { br.setTextureImage( bg ); } protected: void paintEvent( QPaintEvent* e ) { QPainter p( this ); br.setMatrix( QMatrix( qreal( width() ) / qreal( bg.width() ), 0, 0, qreal( height() ) / qreal( bg.height() ), 0, 0 ) ); p.setPen( Qt::NoPen ); p.setBrush( br ); p.drawRect( e->rect() ); } QImage bg; QBrush br;}; class UrlImage : public QObject, public QImage, public QNetworkRequest{ Q_OBJECTpublic: UrlImage( const QString& url, QObject* parent = 0 ) : QObject( parent ) , QNetworkRequest( url ) { QNetworkAccessManager am; QNetworkReply* reply = am.get( *this ); connect( reply, SIGNAL(finished()), this, SLOT(requestFinished()) ); QEventLoop loop; QProgressDialog pd( "Loading image...", "Interrupt", 0, 0 ); connect( &pd, SIGNAL( canceled() ), &loop, SLOT(quit()) ); pd.show(); connect( reply, SIGNAL(finished()), &loop, SLOT(quit()) ); connect( reply, SIGNAL(error(QNetworkReply::NetworkError)), &loop, SLOT(quit()) ); loop.exec(); } private: UrlImage( const UrlImage& ) {} protected Q_SLOTS: void requestFinished() { QNetworkReply* reply = qobject_cast< QNetworkReply* >( sender() ); QNetworkReply::NetworkError err = reply->error(); if( err != QNetworkReply::NoError ) { QMessageBox::warning( 0, "Error", reply->errorString() ); } else { QString path( reply->url().path() ); QString suffix( path.mid( path.lastIndexOf( '.' ) + 1 ) ); QImage::load( reply, suffix.toLatin1().constData() ); } reply->deleteLater(); }}; int main( int argc, char** argv ){ QApplication app( argc, argv ); if( argc < 2 ) { QMessageBox::warning( 0, "Error", "No URL specified." ); return 1; } QString proxy = qgetenv( "http_proxy" ); if( !proxy.isEmpty() ) { QRegExp rx( "http://(\\S+):(\\d+)" ); if( rx.indexIn( proxy ) != -1 ) { QNetworkProxy::setApplicationProxy( QNetworkProxy( QNetworkProxy::HttpProxy, rx.cap( 1 ), rx.cap( 2 ).toInt() ) ); } } UrlImage img( argv[1] ); if( img.isNull() ) return 1; ImgViewer w( img ); w.show(); return app.exec();}