C++ (Qt)std::shared_ptr<int> foo = std::make_shared<int> (10);
C++ (Qt)private: std::shared_ptr<QNetworkReply> m_reply;
C++ (Qt) std::shared_ptr<QNetworkAccessManager> manager = std::make_shared<QNetworkAccessManager>( new QNetworkAccessManager( this ) ); m_reply.reset(manager->get( QNetworkRequest( QUrl( url ) ) ) );
C++ (Qt)std::shared_ptr<QNetworkAccessManager> manager( new QNetworkAccessManager( this ) );
C++ (Qt) std::shared_ptr<QNetworkAccessManager> manager = std::make_shared<QNetworkAccessManager>( new QNetworkAccessManager( this ) );
C++ (Qt)#ifndef DOWNLOADER_H#define DOWNLOADER_H #include <memory> #include <QObject>#include <QString>#include <QNetworkReply>#include <QNetworkRequest>#include <QNetWorkAccessManager> class Downloader : public QObject{ Q_OBJECT Downloader( ) {// connect( m_reply, SIGNAL( finished( ) ),// this, SLOT(replyFinished( ) ) ); } void fetch( const QString &url ) { std::shared_ptr<QNetworkAccessManager> manager = std::make_shared<QNetworkAccessManager>( new QNetworkAccessManager( this ) ); m_reply.reset(manager->get( QNetworkRequest( QUrl( url ) ) ) ); } signals: void signalWithContent( QString * ); private slots: void replyFinished( ) { QByteArray data = m_reply->readAll( ); QString content( data ); emit signalWithContent( &content ); } private: std::shared_ptr<QNetworkReply> m_reply;}; #endif // DOWNLOADER_H
C++ (Qt)#ifndef DOWNLOADER_H#define DOWNLOADER_H #include <memory> #include <QObject>#include <QString>#include <QNetworkReply>#include <QNetworkRequest>#include <QNetWorkAccessManager> class Downloader : public QObject{ Q_OBJECT Downloader( ) {// connect( m_reply, SIGNAL( finished( ) ),// this, SLOT(replyFinished( ) ) ); } void fetch( const QString &url ) { m_reply.reset(m_manager->get( QNetworkRequest( QUrl( url ) ) ) ); } signals: void signalWithContent( QString * ); private slots: void replyFinished( ) { QByteArray data = m_reply->readAll( ); QString content( data ); emit signalWithContent( &content ); } private: std::shared_ptr<QNetworkAccessManager> m_manager = std::make_shared<QNetworkAccessManager>( new QNetworkAccessManager( this ) ); std::shared_ptr<QNetworkReply> m_reply;}; #endif // DOWNLOADER_H
C++ (Qt)#include "VKAuth.h"#include <QNetworkRequest>#include <QMessageBox> VKAuth::VKAuth(QString app, QWidget* parent) : QWebView(parent){ QObject::connect(this, SIGNAL(urlChanged(QUrl)), SLOT(slotLinkChanged(QUrl)) ); m_app = app; loadLoginPage();} void VKAuth::loadLoginPage(){ QUrl url("https://oauth.vk.com/authorize"); url.addQueryItem("client_id", m_app); url.addQueryItem("layout", "https://oauth.vk.com/blank.html"); url.addQueryItem("display", "popup"); url.addQueryItem("scope", "8"); url.addQueryItem("response_type", "token"); load(url);} void VKAuth::slotLinkChanged(QUrl url){ if ("/blank.html" == url.path()) { QRegExp regexp("access_token=([^,]+)&expires_in=([^,]+)&user_id=([^,]+)"); QString str= url.fragment(); if( -1 != regexp.indexIn(str) ) { m_access_token = regexp.cap(1); m_expires_in = regexp.cap(2); m_user_id = regexp.cap(3); slotRequest(); } } else if("/api/login_failure.html" == url.path()){ emit unsuccess(); }} void VKAuth::slotRequest() { QUrl request(QString("https://api.vk.com/method/%1.xml?").arg("friends.get")); request.addQueryItem("user_id", m_user_id); request.addQueryItem("fields", "online"); request.addQueryItem("v", "5.2"); request.addQueryItem("access_token", m_access_token); QNetworkAccessManager *manager = new QNetworkAccessManager(this); m_http = manager->get(QNetworkRequest(request)); QObject::connect(m_http, SIGNAL(finished()), this, SLOT(slotDone()));} void VKAuth::slotDone() { QString russian = QString::fromUtf8(m_http->readAll()); QDomDocument dom; QByteArray aByteArray = russian.toUtf8(); if (!dom.setContent(aByteArray)) { QMessageBox::critical(this, tr("Error"), tr("Failed to parse the file into a DOM tree")); return; } emit success(dom); hide(); //hide this window m_http->close();}
C++ (Qt)#ifndef DOWNLOADER_H#define DOWNLOADER_H #include <memory> #include <QObject>#include <QString>#include <QNetworkReply>#include <QNetworkRequest>#include <QNetWorkAccessManager> class Downloader : public QObject{ Q_OBJECT Downloader( ) { connect( m_reply, SIGNAL( finished( ) ), this, SLOT( replyFinished( ) ) ); } void fetch( const QString &url ) { m_reply.reset(m_manager->get( QNetworkRequest( QUrl( url ) ) ) ); } signals: void signalWithContent( QString * ); private slots: void replyFinished( ) { QByteArray data = m_reply->readAll( ); QString content( data ); emit signalWithContent( &content ); } private: std::shared_ptr<QNetworkAccessManager> m_manager = std::make_shared<QNetworkAccessManager>( new QNetworkAccessManager( this ) ); std::shared_ptr<QNetworkReply> m_reply;}; #endif // DOWNLOADER_H
C++ (Qt)QNetworkReply *QNetworkAccessManager::get(const QNetworkRequest &request){ return d_func()->postProcess(createRequest(QNetworkAccessManager::GetOperation, request));}
Downloader( ) { connect( m_reply.get(), SIGNAL( finished( ) ), this, SLOT( replyFinished( ) ) ); }
C++ (Qt)#ifndef DOWNLOADER_H#define DOWNLOADER_H #include <memory> #include <QObject>#include <QString>#include <QNetworkReply>#include <QNetworkRequest>#include <QNetWorkAccessManager> class Downloader : public QObject{ Q_OBJECT public: Downloader( ) { connect( m_reply.get( ), SIGNAL( finished( ) ), this, SLOT( replyFinished( ) ) ); connect( m_reply.get( ), SIGNAL( QNetworkReply::downloadProgress( qint64 bytesReceived, qint64 bytesTotal ) ), this, SLOT( slotDownloadProgress(qint64 bytesReceived, qint64 bytesTotal ) ) ); } void fetch( const QString &url ) { m_reply.reset(m_manager->get( QNetworkRequest( QUrl( url ) ) ) ); } signals: void signalWithContent( QString * ); void downloadProgress(qint64 bytesReceived, qint64 bytesTotal); private slots: void replyFinished( ) { QByteArray data = m_reply->readAll( ); QString content( data ); emit signalWithContent( &content ); } void slotDownloadProgress( qint64 bytesReceived, qint64 bytesTotal ) { emit downloadProgress( bytesReceived, bytesTotal ); } private: std::shared_ptr<QNetworkAccessManager> m_manager = std::make_shared<QNetworkAccessManager>( new QNetworkAccessManager( this ) ); std::shared_ptr<QNetworkReply> m_reply;}; #endif // DOWNLOADER_H
C++ (Qt) void fetch( const QString &url ) { m_reply.reset(m_manager->get( QNetworkRequest( QUrl( url ) ) ) ); connect( m_reply.get( ), SIGNAL( finished( ) ), this, SLOT( replyFinished( ) ) ); connect( m_reply.get( ), SIGNAL( QNetworkReply::downloadProgress( qint64 bytesReceived, qint64 bytesTotal ) ), this, SLOT( slotDownloadProgress(qint64 bytesReceived, qint64 bytesTotal ) ) ); }
C++ (Qt)connect( m_reply.get( ), SIGNAL( QNetworkReply::downloadProgress( qint64 bytesReceived, qint64 bytesTotal ) ), this, SLOT( slotDownloadProgress(qint64 bytesReceived, qint64 bytesTotal ) ) );
C++ (Qt)connect( m_reply.get( ), SIGNAL( downloadProgress( qint64, qint64 ) ), this, SLOT( slotDownloadProgress(qint64, qint64 ) ) );
std::shared_ptr<QNetworkAccessManager> manager = std::make_shared<QNetworkAccessManager>( new QNetworkAccessManager( this ) );
std::shared_ptr<QNetworkAccessManager> manager = std::make_shared<QNetworkAccessManager>(this);
C++ (Qt)private: QNetworkReply * m_reply;};